[PR #10101/678993a4 backport][3.11] Fix race in FileResponse
if file is replaced during prepare
#10105
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 a backport of PR #10101 as merged into master (678993a).
There was a race in
FileResponse
where thestat
would be incorrect if the file was changed out between thestat
andopen
syscalls. This would lead to various unexpected behaviors such as trying to read beyond the length of the file or sending a partial file. This problem is likely to occour when files are being renamed/linked into place.An example of how this can happen with a system that provides weather data every 60s:
An external process writes
.weather.txt
at the top of each minute, and than renames it into place asweather.txt
. In this caseFileResponse.prepare
maystat
the oldweather.txt
, and thanopen
the newweather.txt
, and use theos.stat_result
from the original file.To fix this we now
fstat
the open file on operating systems wherefstat
is available.This change looks much larger than it actually is because most of the function now needs to be wrapped in the
try
/finally
to ensure that we close the file cleanly as we open it a lot sooner now. Its best to compare without white space as well.fixes #8013