gh-152754: Fix crash when an os.scandir iterator is shared between threads#153462
Draft
nascheme wants to merge 3 commits into
Draft
gh-152754: Fix crash when an os.scandir iterator is shared between threads#153462nascheme wants to merge 3 commits into
nascheme wants to merge 3 commits into
Conversation
…een threads Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 revision of GH-153402.
The issue was a race between
ScandirIterator_iternext()andScandirIterator.close(): one thread could be inside readdir() / FindNextFileW() while another thread closed the underlying DIR * / Windows handle.Changes
Notes
This change is intended to prevent crashes and memory corruption. It does not document or guarantee os.scandir() iterators as generally thread-safe.
A PyMutex is used rather than critical sections because critical sections may be suspended across Py_BEGIN_ALLOW_THREADS; the native iterator must remain protected while the OS directory iteration call runs without the GIL.
os.scandiriterator'sclose()and__next__()on thedirphandle #152754