Repository navigation
bpo-31976: Fix race condition when flushing a file is slow. #4331
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Requested changes from review.
- Loading branch information
commit 6f3ca752dc71c9eb29cb18eb209247485df8c0af
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2017-11-09-21-36-32.bpo-31976.EOA7qY.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fixed race condition when flushing a file is slow, which can cause a | ||
| segfault. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -347,10 +347,10 @@ _enter_buffered_busy(buffered *self) | |
| } | ||
|
|
||
| #define IS_CLOSED(self) \ | ||
| !self->buffer || \ | ||
| (!self->buffer || \ | ||
| (self->fast_closed_checks \ | ||
| ? _PyFileIO_closed(self->raw) \ | ||
| : buffered_closed(self)) | ||
| : buffered_closed(self))) | ||
|
|
||
| #define CHECK_CLOSED(self, error_msg) \ | ||
| if (IS_CLOSED(self)) { \ | ||
|
|
@@ -1976,6 +1976,8 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer) | |
| if (!ENTER_BUFFERED(self)) | ||
| return NULL; | ||
|
|
||
| /* Issue #31976: Check for closed file after acquiring the lock. Another | ||
| thread could be holding the lock while closing the file. */ | ||
| if (IS_CLOSED(self)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment and a reference to the bug entry here? |
||
| PyErr_SetString(PyExc_ValueError, "write to closed file"); | ||
| goto error; | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look right. The
close()method should take the lock once, not take it three times in a row (one of them implicit inflush()). Note_flush_unlocked()can help you here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That made a bunch of tests (
test_flush_error_on_close, etc.) fail sinceclose()no longer callsflush()if the file object is subclassed orfileobj.flushis set to something.This is actually the same behavior as
buffered_closein the C implementation. I have added a comment to that effect.