-
-
Notifications
You must be signed in to change notification settings - Fork 34k
Open
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Build info
$ cd cpython-tsan/bin/
$ ./python3.15
Python 3.15.0a5+ free-threading build (heads/main:96e4cd698a, Jan 31 2026, 11:45:37) [Clang 18.1.3 (1ubuntu1)] on linux
Type "help", "copyright", "credits" or "license" for more information.Bug description
Under Py_GIL_DISABLED, setiter_len() reads so->used non-atomically while concurrent set mutations update it atomically.
This triggers a data race detectable by TSAN when one thread repeatedly calls iterator.__length_hint__() while another mutates the same set
Python script
import threading, time
s = set(range(2000))
it = iter(s)
stop = threading.Event()
def reader():
while not stop.is_set():
it.__length_hint__()
def writer():
i = 0
while not stop.is_set():
s.add(i)
s.discard(i - 1)
i += 1
threads = [threading.Thread(target=reader) for _ in range(4)]
threads.append(threading.Thread(target=writer))
for t in threads: t.start()
time.sleep(3)
stop.set()
for t in threads: t.join()TSAN result
$ TSAN_OPTIONS="halt_on_error=1:history_size=7:report_signal_unsafe=0:verbosity=1" ./python3.15 ./tsan_setiter_len_race.py
==1703386==WARNING: ThreadSanitizer: memory layout is incompatible, possibly due to high-entropy ASLR.
Re-execing with fixed virtual address space.
N.B. reducing ASLR entropy is preferable.
==1703386==Installed the sigaction for signal 11
==1703386==Installed the sigaction for signal 7
==1703386==Installed the sigaction for signal 8
***** Running under ThreadSanitizer v3 (pid 1703386) *****
==================
WARNING: ThreadSanitizer: data race (pid=1703386)
Atomic write of size 8 at 0x7fffb4395ce8 by thread T5:
#0 _Py_atomic_store_ssize_relaxed /home/hyongtao/worksapce/cpython/./Include/cpython/pyatomic_gcc.h:513:3 (python3.15+0x30378c) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#1 set_discard_entry /home/hyongtao/worksapce/cpython/Objects/setobject.c:594:5 (python3.15+0x30378c)
#2 set_discard_key /home/hyongtao/worksapce/cpython/Objects/setobject.c:630:12 (python3.15+0x30378c)
#3 set_discard_impl /home/hyongtao/worksapce/cpython/Objects/setobject.c:2616:10 (python3.15+0x30b3c3) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#4 set_discard /home/hyongtao/worksapce/cpython/Objects/clinic/setobject.c.h:502:20 (python3.15+0x30b3c3)
#5 _PyEval_EvalFrameDefault /home/hyongtao/worksapce/cpython/Python/generated_cases.c.h:4033:35 (python3.15+0x42abe4) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#6 _PyEval_EvalFrame /home/hyongtao/worksapce/cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15+0x41f890) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#7 _PyEval_Vector /home/hyongtao/worksapce/cpython/Python/ceval.c:2092:12 (python3.15+0x41f890)
#8 _PyFunction_Vectorcall /home/hyongtao/worksapce/cpython/Objects/call.c (python3.15+0x20b85f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#9 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:136:11 (python3.15+0x20ff36) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#10 method_vectorcall /home/hyongtao/worksapce/cpython/Objects/classobject.c:73:20 (python3.15+0x20ff36)
#11 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:136:11 (python3.15+0x472d31) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#12 context_run /home/hyongtao/worksapce/cpython/Python/context.c:727:29 (python3.15+0x472d31)
#13 method_vectorcall_FASTCALL_KEYWORDS /home/hyongtao/worksapce/cpython/Objects/descrobject.c:421:24 (python3.15+0x224ab7) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#14 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:136:11 (python3.15+0x20b1e3) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#15 PyObject_Vectorcall /home/hyongtao/worksapce/cpython/Objects/call.c:327:12 (python3.15+0x20b1e3)
#16 _Py_VectorCallInstrumentation_StackRefSteal /home/hyongtao/worksapce/cpython/Python/ceval.c:762:11 (python3.15+0x4204bc) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#17 _PyEval_EvalFrameDefault /home/hyongtao/worksapce/cpython/Python/generated_cases.c.h:1809:35 (python3.15+0x426376) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#18 _PyEval_EvalFrame /home/hyongtao/worksapce/cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15+0x41f890) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#19 _PyEval_Vector /home/hyongtao/worksapce/cpython/Python/ceval.c:2092:12 (python3.15+0x41f890)
#20 _PyFunction_Vectorcall /home/hyongtao/worksapce/cpython/Objects/call.c (python3.15+0x20b85f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#21 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:136:11 (python3.15+0x20ff36) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#22 method_vectorcall /home/hyongtao/worksapce/cpython/Objects/classobject.c:73:20 (python3.15+0x20ff36)
#23 _PyVectorcall_Call /home/hyongtao/worksapce/cpython/Objects/call.c:273:16 (python3.15+0x20b4eb) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#24 _PyObject_Call /home/hyongtao/worksapce/cpython/Objects/call.c:348:16 (python3.15+0x20b4eb)
#25 PyObject_Call /home/hyongtao/worksapce/cpython/Objects/call.c:373:12 (python3.15+0x20b555) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#26 thread_run /home/hyongtao/worksapce/cpython/./Modules/_threadmodule.c:387:21 (python3.15+0x5f12e2) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#27 pythread_wrapper /home/hyongtao/worksapce/cpython/Python/thread_pthread.h:234:5 (python3.15+0x522317) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
Previous read of size 8 at 0x7fffb4395ce8 by thread T3:
#0 setiter_len /home/hyongtao/worksapce/cpython/Objects/setobject.c:1059:58 (python3.15+0x304ce9) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#1 _PyEval_EvalFrameDefault /home/hyongtao/worksapce/cpython/Python/generated_cases.c.h:3935:35 (python3.15+0x42a9d4) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#2 _PyEval_EvalFrame /home/hyongtao/worksapce/cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15+0x41f890) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#3 _PyEval_Vector /home/hyongtao/worksapce/cpython/Python/ceval.c:2092:12 (python3.15+0x41f890)
#4 _PyFunction_Vectorcall /home/hyongtao/worksapce/cpython/Objects/call.c (python3.15+0x20b85f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#5 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:136:11 (python3.15+0x20ff36) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#6 method_vectorcall /home/hyongtao/worksapce/cpython/Objects/classobject.c:73:20 (python3.15+0x20ff36)
#7 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:136:11 (python3.15+0x472d31) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#8 context_run /home/hyongtao/worksapce/cpython/Python/context.c:727:29 (python3.15+0x472d31)
#9 method_vectorcall_FASTCALL_KEYWORDS /home/hyongtao/worksapce/cpython/Objects/descrobject.c:421:24 (python3.15+0x224ab7) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#10 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:136:11 (python3.15+0x20b1e3) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#11 PyObject_Vectorcall /home/hyongtao/worksapce/cpython/Objects/call.c:327:12 (python3.15+0x20b1e3)
#12 _Py_VectorCallInstrumentation_StackRefSteal /home/hyongtao/worksapce/cpython/Python/ceval.c:762:11 (python3.15+0x4204bc) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#13 _PyEval_EvalFrameDefault /home/hyongtao/worksapce/cpython/Python/generated_cases.c.h:1809:35 (python3.15+0x426376) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#14 _PyEval_EvalFrame /home/hyongtao/worksapce/cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15+0x41f890) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#15 _PyEval_Vector /home/hyongtao/worksapce/cpython/Python/ceval.c:2092:12 (python3.15+0x41f890)
#16 _PyFunction_Vectorcall /home/hyongtao/worksapce/cpython/Objects/call.c (python3.15+0x20b85f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#17 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:136:11 (python3.15+0x20ff36) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#18 method_vectorcall /home/hyongtao/worksapce/cpython/Objects/classobject.c:73:20 (python3.15+0x20ff36)
#19 _PyVectorcall_Call /home/hyongtao/worksapce/cpython/Objects/call.c:273:16 (python3.15+0x20b4eb) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#20 _PyObject_Call /home/hyongtao/worksapce/cpython/Objects/call.c:348:16 (python3.15+0x20b4eb)
#21 PyObject_Call /home/hyongtao/worksapce/cpython/Objects/call.c:373:12 (python3.15+0x20b555) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#22 thread_run /home/hyongtao/worksapce/cpython/./Modules/_threadmodule.c:387:21 (python3.15+0x5f12e2) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#23 pythread_wrapper /home/hyongtao/worksapce/cpython/Python/thread_pthread.h:234:5 (python3.15+0x522317) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
Thread T5 'Thread-5 (write' (tid=1703392, running) created by main thread at:
#0 pthread_create <null> (python3.15+0xf421f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#1 do_start_joinable_thread /home/hyongtao/worksapce/cpython/Python/thread_pthread.h:281:14 (python3.15+0x521424) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#2 PyThread_start_joinable_thread /home/hyongtao/worksapce/cpython/Python/thread_pthread.h:323:9 (python3.15+0x52123a) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#3 ThreadHandle_start /home/hyongtao/worksapce/cpython/./Modules/_threadmodule.c:474:9 (python3.15+0x5f109a) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#4 do_start_new_thread /home/hyongtao/worksapce/cpython/./Modules/_threadmodule.c:1920:9 (python3.15+0x5f0b3f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#5 thread_PyThread_start_joinable_thread /home/hyongtao/worksapce/cpython/./Modules/_threadmodule.c:2043:14 (python3.15+0x5efc01) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#6 cfunction_call /home/hyongtao/worksapce/cpython/Objects/methodobject.c:564:18 (python3.15+0x2bab87) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#7 _PyObject_MakeTpCall /home/hyongtao/worksapce/cpython/Objects/call.c:242:18 (python3.15+0x20a68f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#8 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:134:16 (python3.15+0x20b2a7) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#9 PyObject_Vectorcall /home/hyongtao/worksapce/cpython/Objects/call.c:327:12 (python3.15+0x20b2a7)
#10 _Py_VectorCall_StackRefSteal /home/hyongtao/worksapce/cpython/Python/ceval.c:720:11 (python3.15+0x41fce9) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#11 _PyEval_EvalFrameDefault /home/hyongtao/worksapce/cpython/Python/generated_cases.c.h:3387:35 (python3.15+0x429c79) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#12 _PyEval_EvalFrame /home/hyongtao/worksapce/cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15+0x41f3b0) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#13 _PyEval_Vector /home/hyongtao/worksapce/cpython/Python/ceval.c:2092:12 (python3.15+0x41f3b0)
#14 PyEval_EvalCode /home/hyongtao/worksapce/cpython/Python/ceval.c:673:21 (python3.15+0x41f3b0)
#15 run_eval_code_obj /home/hyongtao/worksapce/cpython/Python/pythonrun.c:1366:12 (python3.15+0x4fe967) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#16 run_mod /home/hyongtao/worksapce/cpython/Python/pythonrun.c:1469:19 (python3.15+0x4fe967)
#17 pyrun_file /home/hyongtao/worksapce/cpython/Python/pythonrun.c:1294:15 (python3.15+0x4f9d13) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#18 _PyRun_SimpleFileObject /home/hyongtao/worksapce/cpython/Python/pythonrun.c:518:13 (python3.15+0x4f9d13)
#19 _PyRun_AnyFileObject /home/hyongtao/worksapce/cpython/Python/pythonrun.c:81:15 (python3.15+0x4f9498) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#20 pymain_run_file_obj /home/hyongtao/worksapce/cpython/Modules/main.c:410:15 (python3.15+0x53bedf) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#21 pymain_run_file /home/hyongtao/worksapce/cpython/Modules/main.c:429:15 (python3.15+0x53bedf)
#22 pymain_run_python /home/hyongtao/worksapce/cpython/Modules/main.c:691:21 (python3.15+0x53b20b) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#23 Py_RunMain /home/hyongtao/worksapce/cpython/Modules/main.c:772:5 (python3.15+0x53b20b)
#24 pymain_main /home/hyongtao/worksapce/cpython/Modules/main.c:802:12 (python3.15+0x53b778) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#25 Py_BytesMain /home/hyongtao/worksapce/cpython/Modules/main.c:826:12 (python3.15+0x53b7fb) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#26 main /home/hyongtao/worksapce/cpython/./Programs/python.c:15:12 (python3.15+0x17282b) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
Thread T3 'Thread-3 (reade' (tid=1703390, running) created by main thread at:
#0 pthread_create <null> (python3.15+0xf421f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#1 do_start_joinable_thread /home/hyongtao/worksapce/cpython/Python/thread_pthread.h:281:14 (python3.15+0x521424) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#2 PyThread_start_joinable_thread /home/hyongtao/worksapce/cpython/Python/thread_pthread.h:323:9 (python3.15+0x52123a) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#3 ThreadHandle_start /home/hyongtao/worksapce/cpython/./Modules/_threadmodule.c:474:9 (python3.15+0x5f109a) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#4 do_start_new_thread /home/hyongtao/worksapce/cpython/./Modules/_threadmodule.c:1920:9 (python3.15+0x5f0b3f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#5 thread_PyThread_start_joinable_thread /home/hyongtao/worksapce/cpython/./Modules/_threadmodule.c:2043:14 (python3.15+0x5efc01) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#6 cfunction_call /home/hyongtao/worksapce/cpython/Objects/methodobject.c:564:18 (python3.15+0x2bab87) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#7 _PyObject_MakeTpCall /home/hyongtao/worksapce/cpython/Objects/call.c:242:18 (python3.15+0x20a68f) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#8 _PyObject_VectorcallTstate /home/hyongtao/worksapce/cpython/./Include/internal/pycore_call.h:134:16 (python3.15+0x20b2a7) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#9 PyObject_Vectorcall /home/hyongtao/worksapce/cpython/Objects/call.c:327:12 (python3.15+0x20b2a7)
#10 _Py_VectorCall_StackRefSteal /home/hyongtao/worksapce/cpython/Python/ceval.c:720:11 (python3.15+0x41fce9) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#11 _PyEval_EvalFrameDefault /home/hyongtao/worksapce/cpython/Python/generated_cases.c.h:3387:35 (python3.15+0x429c79) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#12 _PyEval_EvalFrame /home/hyongtao/worksapce/cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15+0x41f3b0) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#13 _PyEval_Vector /home/hyongtao/worksapce/cpython/Python/ceval.c:2092:12 (python3.15+0x41f3b0)
#14 PyEval_EvalCode /home/hyongtao/worksapce/cpython/Python/ceval.c:673:21 (python3.15+0x41f3b0)
#15 run_eval_code_obj /home/hyongtao/worksapce/cpython/Python/pythonrun.c:1366:12 (python3.15+0x4fe967) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#16 run_mod /home/hyongtao/worksapce/cpython/Python/pythonrun.c:1469:19 (python3.15+0x4fe967)
#17 pyrun_file /home/hyongtao/worksapce/cpython/Python/pythonrun.c:1294:15 (python3.15+0x4f9d13) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#18 _PyRun_SimpleFileObject /home/hyongtao/worksapce/cpython/Python/pythonrun.c:518:13 (python3.15+0x4f9d13)
#19 _PyRun_AnyFileObject /home/hyongtao/worksapce/cpython/Python/pythonrun.c:81:15 (python3.15+0x4f9498) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#20 pymain_run_file_obj /home/hyongtao/worksapce/cpython/Modules/main.c:410:15 (python3.15+0x53bedf) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#21 pymain_run_file /home/hyongtao/worksapce/cpython/Modules/main.c:429:15 (python3.15+0x53bedf)
#22 pymain_run_python /home/hyongtao/worksapce/cpython/Modules/main.c:691:21 (python3.15+0x53b20b) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#23 Py_RunMain /home/hyongtao/worksapce/cpython/Modules/main.c:772:5 (python3.15+0x53b20b)
#24 pymain_main /home/hyongtao/worksapce/cpython/Modules/main.c:802:12 (python3.15+0x53b778) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#25 Py_BytesMain /home/hyongtao/worksapce/cpython/Modules/main.c:826:12 (python3.15+0x53b7fb) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
#26 main /home/hyongtao/worksapce/cpython/./Programs/python.c:15:12 (python3.15+0x17282b) (BuildId: 00135b3b874b2f13c2b74122623dcba7b1b9ce4a)
SUMMARY: ThreadSanitizer: data race /home/hyongtao/worksapce/cpython/./Include/cpython/pyatomic_gcc.h:513:3 in _Py_atomic_store_ssize_relaxed
==================
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error