-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Frequent cache access error warnings on Win32 #169
Comments
@schlenk What's the output of |
It should not matter, as it also happens with --cache option. The pip cache dir is in the typical user profile location: The traceback was captured with --cache c:\temp\audit given. |
Interesting that this is a problem -- we shouldn't be doing any parallel access, although I guess we do technically have nested serial access by virtue of running |
The parallel access is probably from the outside (e.g. virus scanner), but as Windows Defender is default on by now, the code should handle that. I would suspect that this code is vulnerable to a race condition: with NamedTemporaryFile(delete=False, dir=os.path.dirname(name)) as io:
io.write(value)
# NOTE(ww): Similar to what `pip` does in `adjacent_tmp_file`.
io.flush()
os.fsync(io.fileno())
os.replace(io.name, name) The os.fsync() might take long enough so the virus scanner notices the new file, then it races against the os.replace(). If any other process holds a file handle for the named temporary file, the os.replace() will fail and should be retried with a tiny delay like 50ms or so. |
That makes sense, thanks for pointing that out. I'm a little bit hesitant to go with a hardcoded delay + retry loop, since it turns a failure mode into a potential deadlock (in the unlikely event that Windows Defender or whatever other AV refuses to give up the file handle.) I'll see if there's a way we can introduce additional creation/access flags here to prevent Defender from trying to obtain access. |
The retry loop should probably just retry 1 or 2 times and simply run in the error path afterwards. And yes, the deadlock happens otherwise. |
Yeah, in an ideal mode we'd use We'll try the retry-with-error-path approach for now. I'll have a PR ready in a moment. |
Yes, sadly https://bugs.python.org/issue15244 is still in limbo. And the SHARE_MODE seems to be the problem here, as the code tries to rename the file held open by the same process, which only works on Windows when the file is opened with SHARE_MODE_DELETE. |
Bug description
The cache writing code frequently runs into WinError 32 warnings due to parallel access by different processes.
This might be due to the typical Windows Defender antivirus interfering with a create & rename operation. Files in use may not be renamed on windows.
Reproduction steps
Run pip-audit --local on a Windows machine with the default Defender Antivirus enabled.
Expected behavior
No warnings, at least one retry.
Screenshots and logs
Platform information
pip-audit
version (pip-audit -V
): pip-audit 1.0.1python -V
orpython3 -V
): Python 3.9.7pip
version (pip -V
orpip3 -V
): pip 21.3.1Additional context
Maybe using https://pypi.org/project/atomicwrites/ would help?
The text was updated successfully, but these errors were encountered: