diff options
author | T. Wouters <thomas@python.org> | 2025-05-06 13:23:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-06 11:23:10 +0000 |
commit | 53e6d76aa30eb760fb8ff788815f22a0e6c101cd (patch) | |
tree | c770e46f0a2064c9a3874c791fa419a4f5d436df | |
parent | 296cd128bf433b8d3b8d9387d7a8ca2de430fbad (diff) | |
download | cpython-53e6d76aa30eb760fb8ff788815f22a0e6c101cd.tar.gz cpython-53e6d76aa30eb760fb8ff788815f22a0e6c101cd.zip |
gh-132917: Fix data race detected by tsan (#133508)
Fix data race detected by tsan
(https://github.com/python/cpython/actions/runs/14857021107/job/41712717208?pr=133502):
young.count can be modified by other threads even while the gcstate is
locked.
This is the simplest fix to (potentially) unblock beta 1, although this
particular code path seems like it could just be an atomic swap followed by
an atomic add, without having the lock at all.
-rw-r--r-- | Python/gc_free_threading.c | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index f1ba5de5170..757e9cb3227 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -2074,10 +2074,9 @@ gc_should_collect_mem_usage(GCState *gcstate) // clear the young object count so we don't check memory usage again // on the next call to gc_should_collect(). PyMutex_Lock(&gcstate->mutex); + int young_count = _Py_atomic_exchange_int(&gcstate->young.count, 0); _Py_atomic_store_ssize_relaxed(&gcstate->deferred_count, - gcstate->deferred_count + - gcstate->young.count); - _Py_atomic_store_int(&gcstate->young.count, 0); + gcstate->deferred_count + young_count); PyMutex_Unlock(&gcstate->mutex); return false; } |