]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.15] gh-153062: Fix a crash iterating itertools.tee on the free-threaded build...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 10 Jul 2026 16:46:45 +0000 (18:46 +0200)
committerGitHub <noreply@github.com>
Fri, 10 Jul 2026 16:46:45 +0000 (09:46 -0700)
commitdc448f3d1a2bfc6ea18e3b5a87f5bd9eb1023964
tree0de44e198795d6c08956dd3b05e41997d81ec54f
parentda999e638c03a05ed2b0f8a828f4013c7f9179bd
[3.15] gh-153062: Fix a crash iterating itertools.tee on the free-threaded build (gh-153063) (gh-153475)

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>
Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
Lib/test/test_free_threading/test_itertools.py
Misc/NEWS.d/next/Library/2026-07-05-14-30-00.gh-issue-153062.teeFT1.rst [new file with mode: 0644]
Modules/itertoolsmodule.c