gh-152754: Fix crash when an os.scandir iterator is shared between threads - #153462
Open
nascheme wants to merge 8 commits into
Open
gh-152754: Fix crash when an os.scandir iterator is shared between threads#153462nascheme wants to merge 8 commits into
nascheme wants to merge 8 commits into
Conversation
…een threads Co-authored-by: Neil Schemenauer <[email protected]>
This race is not free-threading specific.
This makes it not safe to share iterators between multiple threads so document that.
Documentation build overview
|
nascheme
marked this pull request as ready for review
August 13, 2026 00:39
Member
Author
|
Note, I revised this PR to no longer try to be thread-safe with multiple threads using the same iterator. That seems a pretty unlikely usage scenario. Instead, this avoids crashing and/or memory corruption. |
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.
The issue was a race between
ScandirIterator_iternext()andScandirIterator.close(): one thread could be insidereaddir()orFindNextFileW()while another closed the underlying directory stream or Windows handle.Changes
PyMutexand atomic closed state toScandirIteratorin all builds.close()mark the iterator closed immediately without waiting for an in-progress directory read. The reading thread performs the native close afterward when necessary.Py_BEGIN_ALLOW_THREADSaroundreaddir(),FindNextFileW(),closedir(), andFindClose().ResourceWarningwhen opening the directory fails.close(), and failed iterator creation.scandir()iterator between threads.Threading semantics
Sharing an
os.scandir()iterator between threads will no longer corrupt the iterator or crash. Concurrent calls may consume entries in any order, so which thread receives each entry remains unspecified. Callingclose()while another thread is iterating marks the iterator closed immediately and ends iteration early.A
PyMutexis used rather than a critical section because the protection must remain active acrossPy_BEGIN_ALLOW_THREADSwhile the native directory operation runs without the GIL.os.scandiriterator'sclose()and__next__()on thedirphandle #152754