FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-150411: fix `gc_generation.count` race by LindaSummer · Pull Request #150413 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
29 changes: 29 additions & 0 deletions Lib/test/test_free_threading/test_gc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@ def setter():
finally:
gc.set_threshold(*current_threshold)

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 = 1000

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()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.
2 changes: 1 addition & 1 deletion Modules/gcmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ gc_get_count_impl(PyObject *module)
gcstate->generations[2].count);
#else
return Py_BuildValue("(iii)",
gcstate->young.count,
_Py_atomic_load_int_relaxed(&gcstate->young.count),
gcstate->old[0].count,
gcstate->old[1].count);
#endif
Expand Down
Loading

Back | FazBrowse Home | New Git URL