]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.14] gh-153062: Fix a crash iterating itertools.tee on the free-threaded build...
authorNeil Schemenauer <nas-github@arctrix.com>
Fri, 10 Jul 2026 16:47:33 +0000 (09:47 -0700)
committerGitHub <noreply@github.com>
Fri, 10 Jul 2026 16:47:33 +0000 (09:47 -0700)
commit8e648a9c6b29c65ac6e290d6453cd69cedb3b1b4
treed8a654ba504959cd22b766921ccf1cf05a1027f3
parent8c5f13c8e00fa7963c2822fc955e2910ee7e1b89
[3.14] gh-153062: Fix a crash iterating itertools.tee on the free-threaded build (gh-153063) (gh-153476)

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 6c389c446e487bcb9d09e85de30aa42052990f1c)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Lib/test/test_free_threading/test_itertools.py [new file with mode: 0644]
Misc/NEWS.d/next/Library/2026-07-05-14-30-00.gh-issue-153062.teeFT1.rst [new file with mode: 0644]
Modules/itertoolsmodule.c