GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — itertools.tee: concurrent iteration crashes the free-threaded build · Issue #153062 · python/cpython

itertools.tee: concurrent iteration crashes the free-threaded build #153062

New issue
Copy link
New issue
Copy link
Closed
Closed
itertools.tee: concurrent iteration crashes the free-threaded build#153062
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

Description

@tonghuaroot
tonghuaroot
opened on Jul 5, 2026
Issue body actions

Bug description

On the free-threaded build, iterating an :func:itertools.tee iterator concurrently crashes the interpreter (a segfault from corrupted reference counts).

This is a crash-safety bug, not a request to make tee thread-safe. itertools.tee iterators are documented as not thread-safe, and concurrent use returning undefined results or raising RuntimeError is by design and is preserved. But a data race that corrupts reference counts and segfaults the interpreter is memory-unsafety, which the free-threaded build must not permit regardless of the (undefined) logical result.

It is a sibling gap in the free-threading hardening of itertools: the other iterators (chain, product, combinations, permutations, accumulate, count, zip_longest, and so on) already wrap their __next__ in Py_BEGIN_CRITICAL_SECTION and so do not crash under concurrent iteration. tee was the one that was missed.

Root cause

tee branches share a linked list of internal teedataobject cells. Three paths touch that shared state without synchronization:

  • iteration reads/extends a cell (teedataobject_getitem: numread, values[], running) and the link chain (teedataobject_jumplink: nextlink);
  • each branch advances its own position (teeobject.index, teeobject.dataobj);
  • teardown clears cells and walks the chain (teedataobject_clear / teedataobject_safe_decref).

Iterating one branch from multiple threads, or iterating sibling branches that share the same cells, races on these fields, corrupting reference counts and crashing (observed as a SEGV in teedataobject_clear).

Reproduction

On a free-threaded build with ThreadSanitizer, iterating one tee branch from several threads, or consuming each sibling branch of a tee in its own thread, reliably reports data races and then segfaults. Both patterns are covered by a regression test under Lib/test/test_free_threading/.

Fix

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 (a nested critical section can be suspended under contention, which would break the atomicity the fetch relies on). Concurrent iteration of one tee remains undefined and may still raise RuntimeError, as documented, but no longer crashes.

Linked PRs

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

Activity

  1. tonghuaroot
    added a commit that references this issue on Jul 5, 2026

    pythongh-153062: Remove files added by mistake

    c7f1112
  2. picnixz
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
    topic-free-threading
    on Jul 5, 2026
  3. tonghuaroot
    added 2 commits that reference this issue on Jul 5, 2026

    pythongh-153062: Keep the default build on the simple tee iteration path

    0563ec9

    pythongh-153062: Add a critical section to tee_copy_impl

    3dab9e0
  4. nascheme
    added 3 commits that reference this issue on Jul 10, 2026

    gh-153062: Fix a crash iterating itertools.tee on the free-threaded b…

    6c389c4

    [3.15] gh-153062: Fix a crash iterating itertools.tee on the free-thr…

    dc448f3

    [3.14] gh-153062: Fix a crash iterating itertools.tee on the free-thr…

    8e648a9
  5. nascheme commented on Jul 10, 2026

    @nascheme
    nascheme
    on Jul 10, 2026
    Member
    More actions

    Should be fixed. Thank you for the PR!

  6. nascheme
    closed this as completedon Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.