Skip to content
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

Attempt to overwrite without race condition #335

Merged
merged 3 commits into from
Nov 12, 2024
Merged

Conversation

thatch
Copy link
Contributor

@thatch thatch commented May 8, 2024

As explained in #332, there was previously a small window of time where the file is deleted before its new contents get written. Because reads don't happen with the lock held, this resulted in an empty-body cache hit when it shouldn't.

Fixes #332

As explained in psf#332, there was previously a small window of time where the
file is deleted before its new contents get written.  Because reads don't
happen with the lock held, this resulted in an empty-body cache hit when it
shouldn't.

Fixes psf#332
@woodruffw woodruffw self-requested a review May 8, 2024 22:09
@woodruffw
Copy link
Member

Thanks @thatch! I'll try and give this a review in the coming days.

@thatch
Copy link
Contributor Author

thatch commented Jun 6, 2024

Any luck with testing?

@woodruffw
Copy link
Member

Any luck with testing?

Sorry for the delay -- I was out for a bit, and I'm catching up now. I'll do some testing tonight.

Copy link
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some local testing (on Linux) and didn't hit any issues with this.

I also went through the old _secure_open_write flags and confirmed that we shouldn't be missing anything important here -- the old flags should be irrelevant for our mkstemp use (since it defaults to binary mode and O_NOFOLLOW shouldn't be relevant for fresh temporary files).

As such, this LGTM! I'd like @frostming to also do a comb over it though 🙂

(I didn't test this locally on Windows, but my understanding is that os.replace is equally atomic there.)

@thatch
Copy link
Contributor Author

thatch commented Nov 8, 2024

Ping @frostming

@woodruffw
Copy link
Member

@thatch I'll give @frostming another week or so to chime in, otherwise I'll merge and release this next week.

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Copy link
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me -- I think _write could potentially bail out harder if the underlying write fails rather than complete the rename, but that's worth thinking about separately.

@woodruffw woodruffw merged commit 34564d8 into psf:master Nov 12, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition in FileCache that can result in empty body 200 response
3 participants