Skip to content

gh-153908: Fix datarace in itertools.count().repr() - #153917

Merged
ZeroIntensity merged 4 commits into
python:mainfrom
johng:tsan-0006-itertools-count-repr-ft
Jul 18, 2026
Merged

gh-153908: Fix datarace in itertools.count().repr()#153917
ZeroIntensity merged 4 commits into
python:mainfrom
johng:tsan-0006-itertools-count-repr-ft

Conversation

@johng

@johngjohng commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Uses atomic load to ensure protected read to internal cnt variable that can be incremented on next()

When using repro_tsan_0006_count.py as a temporary script to expose the issue we not longer have TSAN flags

➜ cpython git:(tsan-0006-itertools-count-repr-ft) ✗ PYTHON_GIL=0 ./python.exe repro_tsan_0006_count.py
done (no race detected)

Uses atomic load to ensure protected read to internal
`cnt` variable that can be incremented on `next()`
@johng
johng requested a review from rhettinger as a code ownerJuly 18, 2026 10:43
@bedevere-app

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-botBot commented Jul 18, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@picnixzpicnixz changed the title [GH-153908] Fix datarace in itertools.count().repr()gh-153908: Fix datarace in itertools.count().repr()Jul 18, 2026
Comment threadModules/itertoolsmodule.c Outdated
Comment on lines +3678 to +3679
if (lz->long_cnt == NULL) {
/* Match the atomic access in count_next(); a concurrent next() may

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment likely too verbose?

@ByteFlowing1337ByteFlowing1337 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fixes deserve a news entry, please add one :)

Comment threadModules/itertoolsmodule.c Outdated
if (lz->long_cnt == NULL) {
/* Match the atomic access in count_next(); a concurrent next() may
be advancing lz->cnt via _Py_atomic_compare_exchange_ssize(). */
Py_ssize_t cnt = _Py_atomic_load_ssize_relaxed(&lz->cnt);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use FT_ATOMIC_LOAD_SSIZE_RELAXED macro to avoid the possible perf overhead under the default build.

@bedevere-app

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@ByteFlowing1337ByteFlowing1337 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The news entry should be in the Library dir, not Core and Builtins.

@@ -0,0 +1 @@
Fix data race in repr in itertools.counter() for freethreading builds

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix data race in repr in itertools.counter() for freethreading builds
Fix data race when calling :func:`repr` on :class:`itertools.count` under the :term:`free-threaded build`.

@ZeroIntensityZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes and removed needs backport to 3.13 bugs and security fixes labels Jul 18, 2026
@ZeroIntensity
ZeroIntensity merged commit 5200f11 into python:mainJul 18, 2026
66 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @johng for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

Copy link
Copy Markdown

GH-153951 is a backport of this pull request to the 3.15 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 18, 2026
@bedevere-app

Copy link
Copy Markdown

GH-153952 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label Jul 18, 2026
ZeroIntensity pushed a commit that referenced this pull request Jul 18, 2026
) (GH-153952)
(cherry picked from commit 5200f11)
Co-authored-by: John <me@joh.ng>
ZeroIntensity pushed a commit that referenced this pull request Jul 18, 2026
) (GH-153951)
(cherry picked from commit 5200f11)
Co-authored-by: John <me@joh.ng>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@johng@ZeroIntensity@ByteFlowing1337