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 — tsan-006: itertoolsmodule.c: count.__repr__ plain-reads cnt · Issue #153908 · python/cpython

tsan-006: itertoolsmodule.c: count.__repr__ plain-reads cnt #153908

New issue
Copy link
New issue
Copy link
Closed
Closed
tsan-006: itertoolsmodule.c: count.__repr__ plain-reads cnt#153908
Copy link
Labels
3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@johng
johng
opened on Jul 18, 2026
Issue body actions

Bug description:

Follows from #153852 for data race on plain read of cnt in repr

itertoolsmodule.c: count.repr plain-reads cnt while count_next writes it with an atomic CAS — the writer was hardened, the reader missed

import itertools
import threading

STOP = False


def advance(it):
    while not STOP:
        next(it)


def read_repr(it):
    while not STOP:
        repr(it)


def main():
    global STOP
    it = itertools.count()
    threads = [threading.Thread(target=advance, args=(it,)) for _ in range(2)]
    threads += [threading.Thread(target=read_repr, args=(it,)) for _ in range(4)]
    for t in threads:
        t.start()
    # Let the race run briefly; TSan reports on the first conflicting pair.
    threading.Event().wait(2.0)
    STOP = True
    for t in threads:
        t.join()
    print("done (no race detected)")


if __name__ == "__main__":
    main()
PYTHON_GIL=0 ./python.exe repro_tsan_0006_count.py
SUMMARY: ThreadSanitizer: data race itertoolsmodule.c:3680 in count_repr
==================
done (no race detected)
ThreadSanitizer: reported 2 warnings
[1]    55630 abort      PYTHON_GIL=0 ./python.exe repro_tsan_0006_count.py

CPython versions tested on:

CPython main branch

Operating systems tested on:

Macos 26.3.2

Linked PRs

  • gh-153908: Fix datarace in itertools.count().repr() #153917
  • [3.15] gh-153908: Fix data race in itertools.count.__repr__ (GH-153917) #153951
  • [3.14] gh-153908: Fix data race in itertools.count.__repr__ (GH-153917) #153952

Activity

  1. johng
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jul 18, 2026
  2. picnixz
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Jul 18, 2026
  3. ZeroIntensity
    added
    topic-free-threading
    3.14bugs and security fixesbugs and security fixes
    3.15bugs and security fixesbugs and security fixes
    3.16new features, bugs and security fixesnew features, bugs and security fixes
    on Jul 18, 2026
  4. ZeroIntensity
    added a commit that references this issue on Jul 18, 2026

    gh-153908: Fix data race in `itertools.count.__repr__` (GH-153917)

    5200f11
  5. ZeroIntensity commented on Jul 18, 2026

    @ZeroIntensity
    ZeroIntensity
    on Jul 18, 2026
    Member
    More actions

    Backports on automerge

  6. ZeroIntensity
    closed this as completedon Jul 18, 2026
  7. devdanzin
    added a parent issue on Jul 18, 2026
    • Free-threading data races in CPython: 15 findings (9 new + 6 residual of existing FT work) #153852
  8. devdanzin
    added a commit that references this issue on Jul 18, 2026

    Umbrella #153852: mark the first picked-up findings in the table

    db14b87
  9. ZeroIntensity
    added 2 commits that reference this issue on Jul 18, 2026

    [3.14] gh-153908: Fix data race in `itertools.count.__repr__` (GH-153917

    950e7fb

    [3.15] gh-153908: Fix data race in `itertools.count.__repr__` (GH-153917

    536d18b
  10. devdanzin commented on Jul 19, 2026

    @devdanzin
    devdanzin
    on Jul 19, 2026
    Member
    More actions

    After GH-153917, itertools.count.__repr__ still data-races in slow (big-int) mode.

    The fix hardened count_next (fast mode: atomic CAS on lz->cnt; slow mode: count_nextlong under Py_BEGIN_CRITICAL_SECTION(lz)) and the fast-mode read in count_repr. But count_repr's slow-mode path reads lz->long_cnt with no synchronization — starting at the if (lz->long_cnt == NULL) mode check and again in the PyUnicode_FromFormat("%R", ..., lz->long_cnt) — while count_nextlong does lz->long_cnt = stepped_up under the critical section. A critical section only serializes against other critical sections, so count_repr (which never takes it) still races the write. It's also a use-after-free: count_repr borrows lz->long_cnt and repr()s it, while count_nextlong replaces it and the old value returned by next() is later decref'd and freed.

    Repro on a free-threaded --with-thread-sanitizer build (PYTHON_GIL=0):

    import itertools, threading
    NT = 8
    for _ in range(3000):
        it = itertools.count(10**18, 2)          # big-int -> slow (long_cnt) mode
        bar = threading.Barrier(NT)
        def work(advance, it=it, bar=bar):
            bar.wait()
            for _ in range(400):
                next(it) if advance else repr(it)
        ts = [threading.Thread(target=work, args=(i % 2 == 0,)) for i in range(NT)]
        for t in ts: t.start()
        for t in ts: t.join()

    TSan reports count_nextlong (lz->long_cnt = stepped_up) vs count_repr (lz->long_cnt read). Fast-mode count() is clean.

    The fix would mirror count_next's slow path: take Py_BEGIN_CRITICAL_SECTION(lz) around count_repr's long_cnt access (or read long_cnt atomically).

    Should I open a new issue for this, or is it fine to handle it here?

  11. johng commented on Jul 19, 2026

    @johng
    johng
    on Jul 19, 2026
    ContributorAuthor
    More actions

    @devdanzin I had already raised #153983 shortly after working on the fast mode race

  12. devdanzin commented on Jul 19, 2026

    @devdanzin
    devdanzin
    on Jul 19, 2026
    Member
    More actions

    @johng Ah, sorry, missed that! Your fix fully addresses the issues described above.

    BTW, great work finding interesting bugs, much appreciated!

    Reacted by John
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

    3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — 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.