Skip to content

[3.14] gh-153062: Fix a crash iterating itertools.tee on the free-threaded build (gh-153063)#153476

Merged
nascheme merged 1 commit into
python:3.14from
nascheme:backport-6c389c4-3.14
Jul 10, 2026
Merged

[3.14] gh-153062: Fix a crash iterating itertools.tee on the free-threaded build (gh-153063)#153476
nascheme merged 1 commit into
python:3.14from
nascheme:backport-6c389c4-3.14

Conversation

@nascheme

@nascheme nascheme commented Jul 10, 2026

Copy link
Copy Markdown
Member

itertools.tee branches share a linked list of teedataobject cells. On the free-threaded build, iterating one branch from multiple threads, or iterating sibling branches concurrently, raced on the shared cells and each branch's position, corrupting refcounts and crashing.

Lock each teedataobject while reading, extending, or clearing it, and snapshot each branch's position under the tee object's own lock, revalidating before advancing, so the two locks are never nested. Concurrent iteration of one tee is undefined and may raise RuntimeError as documented, but no longer crashes.

The free-threading snapshot and revalidation add per-element overhead on the default build, where the GIL already serializes access. Guard that path under Py_GIL_DISABLED so the default build keeps the original iteration and the free-threaded path is unchanged.

(cherry picked from commit 6c389c4)

…ee-threaded build (pythongh-153063)

itertools.tee branches share a linked list of teedataobject cells.  On the free-threaded build, iterating one branch from multiple threads, or iterating sibling branches concurrently, raced on the shared cells and each branch's position, corrupting refcounts and crashing.

Lock each teedataobject while reading, extending, or clearing it, and snapshot each branch's position under the tee object's own lock, revalidating before advancing, so the two locks are never nested.  Concurrent iteration of one tee is undefined and may raise RuntimeError as documented, but no longer crashes.

The free-threading snapshot and revalidation add per-element overhead on the default build, where the GIL already serializes access. Guard that path under Py_GIL_DISABLED so the default build keeps the original iteration and the free-threaded path is unchanged.
(cherry picked from commit 6c389c4)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
@bedevere-app bedevere-app Bot added topic-free-threading type-crash A hard crash of the interpreter, possibly with a core dump labels Jul 10, 2026
@nascheme nascheme marked this pull request as ready for review July 10, 2026 03:08
@nascheme nascheme requested a review from rhettinger as a code owner July 10, 2026 03:08
@nascheme nascheme merged commit 8e648a9 into python:3.14 Jul 10, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-free-threading type-crash A hard crash of the interpreter, possibly with a core dump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants