GeezerVison is showing github.com — gh-153296: Fix thread-safety data race in io.StringIO iterator by Twix1288 · Pull Request #153368 · python/cpython
Conversation
Twix1288
commented
Jul 8, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Summary
This PR fixes a TSAN-reported data race and segmentation fault when iterating over an io.StringIO object while it is being concurrently mutated (e.g., via write()).
Details
In the free-threaded build, io.StringIO memory safety is typically enforced automatically via Argument Clinic wrappers that acquire Py_BEGIN_CRITICAL_SECTION(self) for operations like readline and write.
However, the __next__ method triggered during loop iteration bypasses these wrappers. It is implemented in C as stringio_iternext, directly assigned to the tp_iternext slot. Previously, this slot accessed the string buffer (self->buf) and advanced self->pos without acquiring the object lock.
If another thread concurrently called write(), it would execute with the lock held but with its underlying self->pos mutating asynchronously. This led to out-of-bounds memset writes and use-after-free conditions.
This fix conceptually mirrors the implementation used for io.BytesIO: it splits the internal logic into stringio_iternext_lock_held and adds a wrapper stringio_iternext that properly secures the object's critical section.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.
If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
PR is done and tests are complete, should be good to go. Thanks!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I don't think I'm the best person to review this - I suggest you find someone who has more familiarity with this area of the code.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
All commit authors signed the Contributor License Agreement.

|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|