gh-153908: Fix datarace in itertools.count().repr()#153917
Conversation
Uses atomic load to ensure protected read to internal `cnt` variable that can be incremented on `next()`
|
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 |
| if (lz->long_cnt == NULL) { | ||
| /* Match the atomic access in count_next(); a concurrent next() may |
There was a problem hiding this comment.
Comment likely too verbose?
ByteFlowing1337
left a comment
There was a problem hiding this comment.
Bug fixes deserve a news entry, please add one :)
| 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); |
There was a problem hiding this comment.
We should use FT_ATOMIC_LOAD_SSIZE_RELAXED macro to avoid the possible perf overhead under the default build.
|
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 |
ByteFlowing1337
left a comment
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
| 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`. |
|
Thanks @johng for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-153951 is a backport of this pull request to the 3.15 branch. |
|
GH-153952 is a backport of this pull request to the 3.14 branch. |
Uses atomic load to ensure protected read to internal
cntvariable that can be incremented onnext()When using
repro_tsan_0006_count.pyas a temporary script to expose the issue we not longer have TSAN flags