Skip to content

Commit 8d863cd

Browse files
Yhg1sPranjal095
authored andcommitted
pythongh-132917: Fix data race detected by tsan (python#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.
1 parent aaf42dd commit 8d863cd

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

Python/gc_free_threading.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,10 +2074,9 @@ gc_should_collect_mem_usage(GCState *gcstate)
20742074
// clear the young object count so we don't check memory usage again
20752075
// on the next call to gc_should_collect().
20762076
PyMutex_Lock(&gcstate->mutex);
2077+
int young_count = _Py_atomic_exchange_int(&gcstate->young.count, 0);
20772078
_Py_atomic_store_ssize_relaxed(&gcstate->deferred_count,
2078-
gcstate->deferred_count +
2079-
gcstate->young.count);
2080-
_Py_atomic_store_int(&gcstate->young.count, 0);
2079+
gcstate->deferred_count + young_count);
20812080
PyMutex_Unlock(&gcstate->mutex);
20822081
return false;
20832082
}

0 commit comments

Comments
 (0)