Close a potentially unclosed body file before it can interfere with writing a new one #343
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is based on debugging in python-trio/trio#3127 and some comments from @graingert, so see that PR for specific information on what was failing (I minimized a failing example) and testing this fix for it (latest commit). Just look at the relevant github actions logs.
This problem only manifests on the combination of PyPy and Windows. Here's how:
CacheControlAdapter#send
callsconditional_headers
conditional_headers
calls_load_from_cache
_load_from_cache
callsbody_file = self.cache.get_body(cache_url)
and saves it to the responseget_body
returns anopen
ed fileconditional_headers
uses the response (which holds the body file) to get headersconditional_headers
returns, but doesn't explicitly finish the requestCacheControlAdapter#send
now runssuper().send(request, stream, timeout, verify, cert, proxies)
os.replace
itThus an annoying caching bug happens. Hopefully I've described it well enough.
Note that this might only happen with
pip
'sSafeFileCache
(which is a subclass ofSeparateBodyBaseCache
). Some of the details rely onos.replace
etc which doesn't happen here. But the fix applies here so...