]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-153062: Fix a crash iterating itertools.tee on the free-threaded build (gh-153063)
authortonghuaroot (童话) <tonghuaroot@gmail.com>
Fri, 10 Jul 2026 02:38:58 +0000 (10:38 +0800)
committerGitHub <noreply@github.com>
Fri, 10 Jul 2026 02:38:58 +0000 (19:38 -0700)
commit6c389c446e487bcb9d09e85de30aa42052990f1c
tree034544625e04dc7c551042a5ee133c023680e6ce
parentd83d26724b8c3be5f535e56ded88593cc6803b86
gh-153062: Fix a crash iterating itertools.tee on the free-threaded build (gh-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.

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