Skip to content

gh-153296: Fix thread-safety data race in io.StringIO iterator#153368

Open
Twix1288 wants to merge 3 commits into
python:mainfrom
Twix1288:feature/new-work
Open

gh-153296: Fix thread-safety data race in io.StringIO iterator#153368
Twix1288 wants to merge 3 commits into
python:mainfrom
Twix1288:feature/new-work

Conversation

@Twix1288

@Twix1288 Twix1288 commented Jul 8, 2026

Copy link
Copy Markdown

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.

@bedevere-app

bedevere-app Bot commented Jul 8, 2026

Copy link
Copy Markdown

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.

@Twix1288 Twix1288 force-pushed the feature/new-work branch from 3851a6a to 21fafe9 Compare July 8, 2026 22:32
@Twix1288 Twix1288 requested a review from vsajip as a code owner July 8, 2026 23:04
@Twix1288

Twix1288 commented Jul 8, 2026

Copy link
Copy Markdown
Author

PR is done and tests are complete, should be good to go. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant