Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Lib/test/test_free_threading/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,35 @@ def evil():
thread.start()
thread.join()

def test_get_count(self):
class CyclicReference:
def __init__(self):
self.ref = self

NUM_ALLOCATORS = 7
NUM_READERS = 1
NUM_THREADS = NUM_ALLOCATORS + NUM_READERS
NUM_ITERS = 200_000

barrier = threading.Barrier(NUM_THREADS)

def allocator():
barrier.wait()
for _ in range(NUM_ITERS):
CyclicReference()


def reader():
barrier.wait()
for _ in range(NUM_ITERS):
gc.get_count()

threads = [Thread(target=allocator) for _ in range(NUM_ALLOCATORS)]
threads.extend(Thread(target=reader) for _ in range(NUM_READERS))

with threading_helper.start_threads(threads):
pass


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a data race in the free-threaded build when :func:`gc.get_count` reads
the young generation allocation count while another thread updates it.
6 changes: 3 additions & 3 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ gc_get_count_impl(PyObject *module)
gcstate->generations[2].count);
#else
return Py_BuildValue("(iii)",
gcstate->young.count,
gcstate->old[0].count,
gcstate->old[1].count);
_Py_atomic_load_int_relaxed(&gcstate->young.count),
_Py_atomic_load_int_relaxed(&gcstate->old[0].count),
_Py_atomic_load_int_relaxed(&gcstate->old[1].count));
#endif
}

Expand Down
Loading