Title: Data race between `record_allocation` and `gc_get_count_impl` · Issue #150411 · python/cpython · GitHub
Open Graph Title: Data race between `record_allocation` and `gc_get_count_impl` · Issue #150411 · python/cpython
X Title: Data race between `record_allocation` and `gc_get_count_impl` · Issue #150411 · python/cpython
Description: Bug report Bug description: I found this during solve #148613. The gc_generation.count has one read not protected by atomic operation. import gc from threading import Barrier, Thread class CyclicReference: def __init__(self): self.ref = ...
Open Graph Description: Bug report Bug description: I found this during solve #148613. The gc_generation.count has one read not protected by atomic operation. import gc from threading import Barrier, Thread class CyclicRe...
X Description: Bug report Bug description: I found this during solve #148613. The gc_generation.count has one read not protected by atomic operation. import gc from threading import Barrier, Thread class CyclicRe...
Opengraph URL: https://github.com/python/cpython/issues/150411
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data race between `record_allocation` and `gc_get_count_impl`","articleBody":"# Bug report\n\n### Bug description:\n\nI found this during solve #148613.\n\nThe `gc_generation.count` has one read not protected by atomic operation.\n\n```python\nimport gc\nfrom threading import Barrier, Thread\n\nclass CyclicReference:\n def __init__(self):\n self.ref = self\n\nN_ALLOCATORS = 7\nN_READERS = 1\nN_THREADS = N_ALLOCATORS + N_READERS\nN_ITERS = 200_000\n\nbarrier = Barrier(N_THREADS)\n\ndef allocator():\n barrier.wait()\n for _ in range(N_ITERS):\n CyclicReference()\n\n\ndef reader():\n barrier.wait()\n for _ in range(N_ITERS):\n gc.get_count()\n\nthreads = [Thread(target=allocator) for _ in range(N_ALLOCATORS)]\nthreads.extend(Thread(target=reader) for _ in range(N_READERS))\n\nfor thread in threads:\n thread.start()\nfor thread in threads:\n thread.join()\n```\n\nHere is tsan report.\n\n```\n==================\nWARNING: ThreadSanitizer: data race (pid=2274087)\n Atomic write of size 4 at 0x55a842f6ab04 by thread T2:\n #0 _Py_atomic_add_int Include/cpython/pyatomic_gcc.h:15 (python3.16+0x45a310)\n #1 record_allocation Python/gc_free_threading.c:2028 (python3.16+0x45a310)\n #2 _PyObject_GC_Link Python/gc_free_threading.c:2707 (python3.16+0x45a310)\n #3 _PyType_AllocNoTrack Objects/typeobject.c:2533 (python3.16+0x299867)\n #4 PyType_GenericAlloc Objects/typeobject.c:2554 (python3.16+0x2998d4)\n #5 _PyEval_EvalFrameDefault Python/generated_cases.c.h:1955 (python3.16+0x3e0047)\n #6 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee7b7)\n #7 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee7b7)\n #8 _PyFunction_Vectorcall Objects/call.c:413 (python3.16+0x152190)\n #9 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x15379f)\n #10 _PyObject_VectorcallPrepend Objects/call.c:855 (python3.16+0x15379f)\n #11 method_vectorcall Objects/classobject.c:55 (python3.16+0x158676)\n #12 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x42927b)\n #13 context_run Python/context.c:728 (python3.16+0x42927b)\n #14 method_vectorcall_FASTCALL_KEYWORDS Objects/descrobject.c:421 (python3.16+0x174575)\n #15 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x151d7c)\n #16 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151d7c)\n #17 _Py_VectorCallInstrumentation_StackRefSteal Python/ceval.c:775 (python3.16+0x3c13f5)\n #18 _PyEval_EvalFrameDefault Python/generated_cases.c.h:1846 (python3.16+0x3cee3f)\n #19 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee7b7)\n #20 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee7b7)\n #21 _PyFunction_Vectorcall Objects/call.c:413 (python3.16+0x152190)\n #22 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x15379f)\n #23 _PyObject_VectorcallPrepend Objects/call.c:855 (python3.16+0x15379f)\n #24 method_vectorcall Objects/classobject.c:55 (python3.16+0x158676)\n #25 _PyVectorcall_Call Objects/call.c:273 (python3.16+0x154c78)\n #26 _PyObject_Call Objects/call.c:348 (python3.16+0x1550b1)\n #27 PyObject_Call Objects/call.c:373 (python3.16+0x1550b1)\n #28 thread_run Modules/_threadmodule.c:388 (python3.16+0x609d30)\n #29 pythread_wrapper Python/thread_pthread.h:234 (python3.16+0x508c52)\n #30 \u003cnull\u003e \u003cnull\u003e (libtsan.so.2+0x4cf1a)\n\n Previous read of size 4 at 0x55a842f6ab04 by thread T8:\n #0 gc_get_count_impl Modules/gcmodule.c:232 (python3.16+0x52aef1)\n #1 gc_get_count Modules/clinic/gcmodule.c.h:305 (python3.16+0x52aef1)\n #2 cfunction_vectorcall_NOARGS Objects/methodobject.c:508 (python3.16+0x21a586)\n #3 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x151d7c)\n #4 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151d7c)\n #5 _Py_VectorCall_StackRefSteal Python/ceval.c:733 (python3.16+0x3c0fc5)\n #6 _PyEval_EvalFrameDefault Python/generated_cases.c.h:4362 (python3.16+0x3cf022)\n #7 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee7b7)\n #8 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee7b7)\n #9 _PyFunction_Vectorcall Objects/call.c:413 (python3.16+0x152190)\n #10 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x15379f)\n #11 _PyObject_VectorcallPrepend Objects/call.c:855 (python3.16+0x15379f)\n #12 method_vectorcall Objects/classobject.c:55 (python3.16+0x158676)\n #13 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x42927b)\n #14 context_run Python/context.c:728 (python3.16+0x42927b)\n #15 method_vectorcall_FASTCALL_KEYWORDS Objects/descrobject.c:421 (python3.16+0x174575)\n #16 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x151d7c)\n #17 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151d7c)\n #18 _Py_VectorCallInstrumentation_StackRefSteal Python/ceval.c:775 (python3.16+0x3c13f5)\n #19 _PyEval_EvalFrameDefault Python/generated_cases.c.h:1846 (python3.16+0x3cee3f)\n #20 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee7b7)\n #21 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee7b7)\n #22 _PyFunction_Vectorcall Objects/call.c:413 (python3.16+0x152190)\n #23 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x15379f)\n #24 _PyObject_VectorcallPrepend Objects/call.c:855 (python3.16+0x15379f)\n #25 method_vectorcall Objects/classobject.c:55 (python3.16+0x158676)\n #26 _PyVectorcall_Call Objects/call.c:273 (python3.16+0x154c78)\n #27 _PyObject_Call Objects/call.c:348 (python3.16+0x1550b1)\n #28 PyObject_Call Objects/call.c:373 (python3.16+0x1550b1)\n #29 thread_run Modules/_threadmodule.c:388 (python3.16+0x609d30)\n #30 pythread_wrapper Python/thread_pthread.h:234 (python3.16+0x508c52)\n #31 \u003cnull\u003e \u003cnull\u003e (libtsan.so.2+0x4cf1a)\n\n Location is global '_PyRuntime' of size 405696 at 0x55a842f42240 (python3.16+0x89fb04)\n\n Thread T2 'Thread-2 (alloc' (tid=2274090, running) created by main thread at:\n #0 pthread_create \u003cnull\u003e (libtsan.so.2+0x58491)\n #1 do_start_joinable_thread Python/thread_pthread.h:281 (python3.16+0x508d67)\n #2 PyThread_start_joinable_thread Python/thread_pthread.h:323 (python3.16+0x5090ab)\n #3 ThreadHandle_start Modules/_threadmodule.c:475 (python3.16+0x60aca9)\n #4 do_start_new_thread Modules/_threadmodule.c:1919 (python3.16+0x60aca9)\n #5 thread_PyThread_start_joinable_thread Modules/_threadmodule.c:2042 (python3.16+0x60b5b9)\n #6 cfunction_call Objects/methodobject.c:564 (python3.16+0x2192fd)\n #7 _PyObject_MakeTpCall Objects/call.c:242 (python3.16+0x150e5b)\n #8 _PyObject_VectorcallTstate Include/internal/pycore_call.h:142 (python3.16+0x151de1)\n #9 _PyObject_VectorcallTstate Include/internal/pycore_call.h:129 (python3.16+0x151de1)\n #10 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151de1)\n #11 _Py_VectorCall_StackRefSteal Python/ceval.c:733 (python3.16+0x3c0fc5)\n #12 _PyEval_EvalFrameDefault Python/generated_cases.c.h:3528 (python3.16+0x3d8f55)\n #13 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee06c)\n #14 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee06c)\n #15 PyEval_EvalCode Python/ceval.c:686 (python3.16+0x3ee06c)\n #16 run_eval_code_obj Python/pythonrun.c:1369 (python3.16+0x4d55c2)\n #17 run_mod Python/pythonrun.c:1472 (python3.16+0x4d5b88)\n #18 pyrun_file Python/pythonrun.c:1296 (python3.16+0x4d9913)\n #19 _PyRun_SimpleFileObject Python/pythonrun.c:518 (python3.16+0x4d9913)\n #20 _PyRun_AnyFileObject Python/pythonrun.c:81 (python3.16+0x4da3c5)\n #21 pymain_run_file_obj Modules/main.c:411 (python3.16+0x52940d)\n #22 pymain_run_file Modules/main.c:430 (python3.16+0x52940d)\n #23 pymain_run_python Modules/main.c:715 (python3.16+0x52940d)\n #24 Py_RunMain Modules/main.c:796 (python3.16+0x52a2fd)\n #25 pymain_main Modules/main.c:826 (python3.16+0x52a2fd)\n #26 Py_BytesMain Modules/main.c:850 (python3.16+0x52a2fd)\n #27 main Programs/python.c:15 (python3.16+0xa0786)\n\n Thread T8 'Thread-8 (reade' (tid=2274096, running) created by main thread at:\n #0 pthread_create \u003cnull\u003e (libtsan.so.2+0x58491)\n #1 do_start_joinable_thread Python/thread_pthread.h:281 (python3.16+0x508d67)\n #2 PyThread_start_joinable_thread Python/thread_pthread.h:323 (python3.16+0x5090ab)\n #3 ThreadHandle_start Modules/_threadmodule.c:475 (python3.16+0x60aca9)\n #4 do_start_new_thread Modules/_threadmodule.c:1919 (python3.16+0x60aca9)\n #5 thread_PyThread_start_joinable_thread Modules/_threadmodule.c:2042 (python3.16+0x60b5b9)\n #6 cfunction_call Objects/methodobject.c:564 (python3.16+0x2192fd)\n #7 _PyObject_MakeTpCall Objects/call.c:242 (python3.16+0x150e5b)\n #8 _PyObject_VectorcallTstate Include/internal/pycore_call.h:142 (python3.16+0x151de1)\n #9 _PyObject_VectorcallTstate Include/internal/pycore_call.h:129 (python3.16+0x151de1)\n #10 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151de1)\n #11 _Py_VectorCall_StackRefSteal Python/ceval.c:733 (python3.16+0x3c0fc5)\n #12 _PyEval_EvalFrameDefault Python/generated_cases.c.h:3528 (python3.16+0x3d8f55)\n #13 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee06c)\n #14 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee06c)\n #15 PyEval_EvalCode Python/ceval.c:686 (python3.16+0x3ee06c)\n #16 run_eval_code_obj Python/pythonrun.c:1369 (python3.16+0x4d55c2)\n #17 run_mod Python/pythonrun.c:1472 (python3.16+0x4d5b88)\n #18 pyrun_file Python/pythonrun.c:1296 (python3.16+0x4d9913)\n #19 _PyRun_SimpleFileObject Python/pythonrun.c:518 (python3.16+0x4d9913)\n #20 _PyRun_AnyFileObject Python/pythonrun.c:81 (python3.16+0x4da3c5)\n #21 pymain_run_file_obj Modules/main.c:411 (python3.16+0x52940d)\n #22 pymain_run_file Modules/main.c:430 (python3.16+0x52940d)\n #23 pymain_run_python Modules/main.c:715 (python3.16+0x52940d)\n #24 Py_RunMain Modules/main.c:796 (python3.16+0x52a2fd)\n #25 pymain_main Modules/main.c:826 (python3.16+0x52a2fd)\n #26 Py_BytesMain Modules/main.c:850 (python3.16+0x52a2fd)\n #27 main Programs/python.c:15 (python3.16+0xa0786)\n\nSUMMARY: ThreadSanitizer: data race Include/cpython/pyatomic_gcc.h:15 in _Py_atomic_add_int\n==================\n==================\nWARNING: ThreadSanitizer: data race (pid=2274087)\n Atomic write of size 4 at 0x55a842f6ab04 by thread T3:\n #0 _Py_atomic_compare_exchange_int Include/cpython/pyatomic_gcc.h:70 (python3.16+0x45b47f)\n #1 record_deallocation Python/gc_free_threading.c:2057 (python3.16+0x45b47f)\n #2 PyObject_GC_Del Python/gc_free_threading.c:2831 (python3.16+0x45b47f)\n #3 object_dealloc Objects/typeobject.c:7463 (python3.16+0x286bab)\n #4 subtype_dealloc Objects/typeobject.c:2871 (python3.16+0x28e8b7)\n #5 _Py_Dealloc Objects/object.c:3312 (python3.16+0x22599b)\n #6 _Py_DecRefSharedDebug Objects/object.c:426 (python3.16+0x225d5d)\n #7 _Py_DecRefShared Objects/object.c:433 (python3.16+0x225d5d)\n #8 Py_DECREF Include/refcount.h:385 (python3.16+0x45ca4c)\n #9 delete_garbage Python/gc_free_threading.c:1769 (python3.16+0x45ca4c)\n #10 gc_collect_internal Python/gc_free_threading.c:2176 (python3.16+0x45ca4c)\n #11 gc_collect_main Python/gc_free_threading.c:2257 (python3.16+0x45d5d6)\n #12 _Py_RunGC Python/gc_free_threading.c:2716 (python3.16+0x45dbb6)\n #13 _Py_HandlePending Python/ceval_gil.c:1399 (python3.16+0x46d393)\n #14 check_periodics Python/ceval_macros.h:524 (python3.16+0x3e9e88)\n #15 _PyEval_EvalFrameDefault Python/generated_cases.c.h:10876 (python3.16+0x3e9e88)\n #16 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee7b7)\n #17 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee7b7)\n #18 _PyFunction_Vectorcall Objects/call.c:413 (python3.16+0x152190)\n #19 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x15379f)\n #20 _PyObject_VectorcallPrepend Objects/call.c:855 (python3.16+0x15379f)\n #21 method_vectorcall Objects/classobject.c:55 (python3.16+0x158676)\n #22 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x42927b)\n #23 context_run Python/context.c:728 (python3.16+0x42927b)\n #24 method_vectorcall_FASTCALL_KEYWORDS Objects/descrobject.c:421 (python3.16+0x174575)\n #25 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x151d7c)\n #26 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151d7c)\n #27 _Py_VectorCallInstrumentation_StackRefSteal Python/ceval.c:775 (python3.16+0x3c13f5)\n #28 _PyEval_EvalFrameDefault Python/generated_cases.c.h:1846 (python3.16+0x3cee3f)\n #29 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee7b7)\n #30 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee7b7)\n #31 _PyFunction_Vectorcall Objects/call.c:413 (python3.16+0x152190)\n #32 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x15379f)\n #33 _PyObject_VectorcallPrepend Objects/call.c:855 (python3.16+0x15379f)\n #34 method_vectorcall Objects/classobject.c:55 (python3.16+0x158676)\n #35 _PyVectorcall_Call Objects/call.c:273 (python3.16+0x154c78)\n #36 _PyObject_Call Objects/call.c:348 (python3.16+0x1550b1)\n #37 PyObject_Call Objects/call.c:373 (python3.16+0x1550b1)\n #38 thread_run Modules/_threadmodule.c:388 (python3.16+0x609d30)\n #39 pythread_wrapper Python/thread_pthread.h:234 (python3.16+0x508c52)\n #40 \u003cnull\u003e \u003cnull\u003e (libtsan.so.2+0x4cf1a)\n\n Previous read of size 4 at 0x55a842f6ab04 by thread T8:\n #0 gc_get_count_impl Modules/gcmodule.c:232 (python3.16+0x52aef1)\n #1 gc_get_count Modules/clinic/gcmodule.c.h:305 (python3.16+0x52aef1)\n #2 cfunction_vectorcall_NOARGS Objects/methodobject.c:508 (python3.16+0x21a586)\n #3 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x151d7c)\n #4 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151d7c)\n #5 _Py_VectorCall_StackRefSteal Python/ceval.c:733 (python3.16+0x3c0fc5)\n #6 _PyEval_EvalFrameDefault Python/generated_cases.c.h:4362 (python3.16+0x3cf022)\n #7 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee7b7)\n #8 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee7b7)\n #9 _PyFunction_Vectorcall Objects/call.c:413 (python3.16+0x152190)\n #10 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x15379f)\n #11 _PyObject_VectorcallPrepend Objects/call.c:855 (python3.16+0x15379f)\n #12 method_vectorcall Objects/classobject.c:55 (python3.16+0x158676)\n #13 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x42927b)\n #14 context_run Python/context.c:728 (python3.16+0x42927b)\n #15 method_vectorcall_FASTCALL_KEYWORDS Objects/descrobject.c:421 (python3.16+0x174575)\n #16 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x151d7c)\n #17 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151d7c)\n #18 _Py_VectorCallInstrumentation_StackRefSteal Python/ceval.c:775 (python3.16+0x3c13f5)\n #19 _PyEval_EvalFrameDefault Python/generated_cases.c.h:1846 (python3.16+0x3cee3f)\n #20 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee7b7)\n #21 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee7b7)\n #22 _PyFunction_Vectorcall Objects/call.c:413 (python3.16+0x152190)\n #23 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144 (python3.16+0x15379f)\n #24 _PyObject_VectorcallPrepend Objects/call.c:855 (python3.16+0x15379f)\n #25 method_vectorcall Objects/classobject.c:55 (python3.16+0x158676)\n #26 _PyVectorcall_Call Objects/call.c:273 (python3.16+0x154c78)\n #27 _PyObject_Call Objects/call.c:348 (python3.16+0x1550b1)\n #28 PyObject_Call Objects/call.c:373 (python3.16+0x1550b1)\n #29 thread_run Modules/_threadmodule.c:388 (python3.16+0x609d30)\n #30 pythread_wrapper Python/thread_pthread.h:234 (python3.16+0x508c52)\n #31 \u003cnull\u003e \u003cnull\u003e (libtsan.so.2+0x4cf1a)\n\n Location is global '_PyRuntime' of size 405696 at 0x55a842f42240 (python3.16+0x89fb04)\n\n Thread T3 'Thread-3 (alloc' (tid=2274091, running) created by main thread at:\n #0 pthread_create \u003cnull\u003e (libtsan.so.2+0x58491)\n #1 do_start_joinable_thread Python/thread_pthread.h:281 (python3.16+0x508d67)\n #2 PyThread_start_joinable_thread Python/thread_pthread.h:323 (python3.16+0x5090ab)\n #3 ThreadHandle_start Modules/_threadmodule.c:475 (python3.16+0x60aca9)\n #4 do_start_new_thread Modules/_threadmodule.c:1919 (python3.16+0x60aca9)\n #5 thread_PyThread_start_joinable_thread Modules/_threadmodule.c:2042 (python3.16+0x60b5b9)\n #6 cfunction_call Objects/methodobject.c:564 (python3.16+0x2192fd)\n #7 _PyObject_MakeTpCall Objects/call.c:242 (python3.16+0x150e5b)\n #8 _PyObject_VectorcallTstate Include/internal/pycore_call.h:142 (python3.16+0x151de1)\n #9 _PyObject_VectorcallTstate Include/internal/pycore_call.h:129 (python3.16+0x151de1)\n #10 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151de1)\n #11 _Py_VectorCall_StackRefSteal Python/ceval.c:733 (python3.16+0x3c0fc5)\n #12 _PyEval_EvalFrameDefault Python/generated_cases.c.h:3528 (python3.16+0x3d8f55)\n #13 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee06c)\n #14 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee06c)\n #15 PyEval_EvalCode Python/ceval.c:686 (python3.16+0x3ee06c)\n #16 run_eval_code_obj Python/pythonrun.c:1369 (python3.16+0x4d55c2)\n #17 run_mod Python/pythonrun.c:1472 (python3.16+0x4d5b88)\n #18 pyrun_file Python/pythonrun.c:1296 (python3.16+0x4d9913)\n #19 _PyRun_SimpleFileObject Python/pythonrun.c:518 (python3.16+0x4d9913)\n #20 _PyRun_AnyFileObject Python/pythonrun.c:81 (python3.16+0x4da3c5)\n #21 pymain_run_file_obj Modules/main.c:411 (python3.16+0x52940d)\n #22 pymain_run_file Modules/main.c:430 (python3.16+0x52940d)\n #23 pymain_run_python Modules/main.c:715 (python3.16+0x52940d)\n #24 Py_RunMain Modules/main.c:796 (python3.16+0x52a2fd)\n #25 pymain_main Modules/main.c:826 (python3.16+0x52a2fd)\n #26 Py_BytesMain Modules/main.c:850 (python3.16+0x52a2fd)\n #27 main Programs/python.c:15 (python3.16+0xa0786)\n\n Thread T8 'Thread-8 (reade' (tid=2274096, running) created by main thread at:\n #0 pthread_create \u003cnull\u003e (libtsan.so.2+0x58491)\n #1 do_start_joinable_thread Python/thread_pthread.h:281 (python3.16+0x508d67)\n #2 PyThread_start_joinable_thread Python/thread_pthread.h:323 (python3.16+0x5090ab)\n #3 ThreadHandle_start Modules/_threadmodule.c:475 (python3.16+0x60aca9)\n #4 do_start_new_thread Modules/_threadmodule.c:1919 (python3.16+0x60aca9)\n #5 thread_PyThread_start_joinable_thread Modules/_threadmodule.c:2042 (python3.16+0x60b5b9)\n #6 cfunction_call Objects/methodobject.c:564 (python3.16+0x2192fd)\n #7 _PyObject_MakeTpCall Objects/call.c:242 (python3.16+0x150e5b)\n #8 _PyObject_VectorcallTstate Include/internal/pycore_call.h:142 (python3.16+0x151de1)\n #9 _PyObject_VectorcallTstate Include/internal/pycore_call.h:129 (python3.16+0x151de1)\n #10 PyObject_Vectorcall Objects/call.c:327 (python3.16+0x151de1)\n #11 _Py_VectorCall_StackRefSteal Python/ceval.c:733 (python3.16+0x3c0fc5)\n #12 _PyEval_EvalFrameDefault Python/generated_cases.c.h:3528 (python3.16+0x3d8f55)\n #13 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122 (python3.16+0x3ee06c)\n #14 _PyEval_Vector Python/ceval.c:2156 (python3.16+0x3ee06c)\n #15 PyEval_EvalCode Python/ceval.c:686 (python3.16+0x3ee06c)\n #16 run_eval_code_obj Python/pythonrun.c:1369 (python3.16+0x4d55c2)\n #17 run_mod Python/pythonrun.c:1472 (python3.16+0x4d5b88)\n #18 pyrun_file Python/pythonrun.c:1296 (python3.16+0x4d9913)\n #19 _PyRun_SimpleFileObject Python/pythonrun.c:518 (python3.16+0x4d9913)\n #20 _PyRun_AnyFileObject Python/pythonrun.c:81 (python3.16+0x4da3c5)\n #21 pymain_run_file_obj Modules/main.c:411 (python3.16+0x52940d)\n #22 pymain_run_file Modules/main.c:430 (python3.16+0x52940d)\n #23 pymain_run_python Modules/main.c:715 (python3.16+0x52940d)\n #24 Py_RunMain Modules/main.c:796 (python3.16+0x52a2fd)\n #25 pymain_main Modules/main.c:826 (python3.16+0x52a2fd)\n #26 Py_BytesMain Modules/main.c:850 (python3.16+0x52a2fd)\n #27 main Programs/python.c:15 (python3.16+0xa0786)\n\nSUMMARY: ThreadSanitizer: data race Include/cpython/pyatomic_gcc.h:70 in _Py_atomic_compare_exchange_int\n==================\nThreadSanitizer: reported 2 warnings\n```\n\n## Root Cause\n\nThis problem would happen when cyclic references allocation frequently in free threading build.\n\nThe `alloc_count` would be reset when `gc-\u003ealloc_count \u003e= LOCAL_ALLOC_COUNT_THRESHOLD` and the `gcstate-\u003eyoung.count` would increase.\nhttps://github.com/python/cpython/blob/c714b5679817099ac574890392f408129e6c67cb/Python/gc_free_threading.c#L2025-L2029\n\nAnd we read the count in `gc_get_count_impl`, it was partially protected by atomic.\nhttps://github.com/python/cpython/blob/c714b5679817099ac574890392f408129e6c67cb/Modules/gcmodule.c#L211-L237\n\n## Solution\n\nSo we should add atomic load for the read action.\n\nFor `count` in `gc_collect_internal`, they are guarded by STW so I think they should be safe.\nhttps://github.com/python/cpython/blob/c714b5679817099ac574890392f408129e6c67cb/Python/gc_free_threading.c#L2065-L2077\n\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-150413\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/LindaSummer","@type":"Person","name":"LindaSummer"},"datePublished":"2026-05-25T16:03:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/150411/cpython/issues/150411"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d27e194c-1682-9e6b-fafb-04ad26129177 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E9D0:386175:23FE:2D4F:6A577461 |
| html-safe-nonce | eff61ee7d954d093946f82de7f96846203ddf1ab0c7abb57560bf5dcd5e97fb6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUQwOjM4NjE3NToyM0ZFOjJENEY6NkE1Nzc0NjEiLCJ2aXNpdG9yX2lkIjoiMzkyODMwNzAyNjM1MTMyMjIwOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ad2620968deb6610e3810b160fa289ec8d0b59a89b8918f64c7002ae3392e60b |
| hovercard-subject-tag | issue:4518155395 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/150411/issue_layout |
| twitter:image | https://opengraph.githubassets.com/84e536c4745bd5a7799bdd61ecc1a2c6378a2505857a37feb803ea0e1c734faa/python/cpython/issues/150411 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/84e536c4745bd5a7799bdd61ecc1a2c6378a2505857a37feb803ea0e1c734faa/python/cpython/issues/150411 |
| og:image:alt | Bug report Bug description: I found this during solve #148613. The gc_generation.count has one read not protected by atomic operation. import gc from threading import Barrier, Thread class CyclicRe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | LindaSummer |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4e7a7296a3830877cf21a6ad2a972c9e618a48915e03966cf0c53eb08e5aad98 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c0ca805e53f128ba1f966be1d3ccd81d0d1959c7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width