Skip to content

Possibly data race in dict popitem vs do_lookup, dictobject.c #131113

Closed
@vfdev-5

Description

@vfdev-5

Bug report

Bug description:

I built cpython 3.13 from source with TSAN and running the following code:

import concurrent.futures
import threading


if __name__ == "__main__":
    num_workers = 20
    num_runs = 100

    barrier = threading.Barrier(num_workers)

    shared_dict = {}
    for i in range(50):
        shared_dict[f"{i}"] = i

    def closure():
        barrier.wait()

        for _ in range(num_runs):
            for i in range(10):
                key = f"{i}"
                if key in shared_dict:
                    obj = shared_dict[key]
                if len(shared_dict) > 0:
                    another_obj = shared_dict.popitem()

    with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
        futures = []
        for i in range(num_workers):
            futures.append(executor.submit(closure))
        assert len(list(f.result() for f in futures)) == num_workers

TSAN reports the following data race:

data race report
==================
WARNING: ThreadSanitizer: data race (pid=379167)
  Write of size 8 at 0x7fffb46b66b0 by thread T20:
    #0 __tsan_memset <null> (python3.13+0xda21d) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #1 dict_popitem_impl /project/cpython/Objects/dictobject.c:4469:25 (python3.13+0x2768db) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #2 dict_popitem /project/cpython/Objects/clinic/dictobject.c.h:220:20 (python3.13+0x2768db)
    #3 method_vectorcall_NOARGS /project/cpython/Objects/descrobject.c:447:24 (python3.13+0x200b61) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #4 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:168:11 (python3.13+0x1eafea) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #5 PyObject_Vectorcall /project/cpython/Objects/call.c:327:12 (python3.13+0x1eafea)
    #6 _PyEval_EvalFrameDefault /project/cpython/Python/generated_cases.c.h:813:23 (python3.13+0x3e35db) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #7 _PyEval_EvalFrame /project/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.13+0x3df70a) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #8 _PyEval_Vector /project/cpython/Python/ceval.c:1813:12 (python3.13+0x3df70a)
    #9 _PyFunction_Vectorcall /project/cpython/Objects/call.c (python3.13+0x1eb65f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #10 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:168:11 (python3.13+0x1ef62f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #11 method_vectorcall /project/cpython/Objects/classobject.c:70:20 (python3.13+0x1ef62f)
    #12 _PyVectorcall_Call /project/cpython/Objects/call.c:273:16 (python3.13+0x1eb2d3) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #13 _PyObject_Call /project/cpython/Objects/call.c:348:16 (python3.13+0x1eb2d3)
    #14 PyObject_Call /project/cpython/Objects/call.c:373:12 (python3.13+0x1eb355) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #15 thread_run /project/cpython/./Modules/_threadmodule.c:337:21 (python3.13+0x567ef2) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #16 pythread_wrapper /project/cpython/Python/thread_pthread.h:243:5 (python3.13+0x4c0e67) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)

  Previous atomic read of size 8 at 0x7fffb46b66b0 by thread T3:
    #0 _Py_atomic_load_ptr_relaxed /project/cpython/./Include/cpython/pyatomic_gcc.h:359:18 (python3.13+0x25f09c) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #1 compare_unicode_unicode_threadsafe /project/cpython/Objects/dictobject.c:1397:26 (python3.13+0x25f09c)
    #2 do_lookup /project/cpython/Objects/dictobject.c:1066:23 (python3.13+0x25f09c)
    #3 unicodekeys_lookup_unicode_threadsafe /project/cpython/Objects/dictobject.c:1423:12 (python3.13+0x25f09c)
    #4 _Py_dict_lookup_threadsafe /project/cpython/Objects/dictobject.c:1478:18 (python3.13+0x260c37) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #5 _PyDict_Contains_KnownHash /project/cpython/Objects/dictobject.c:4691:10 (python3.13+0x26b366) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #6 PyDict_Contains /project/cpython/Objects/dictobject.c:4667:12 (python3.13+0x26b366)
    #7 PySequence_Contains /project/cpython/Objects/abstract.c:2277:19 (python3.13+0x1c2cdd) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #8 _PyEval_EvalFrameDefault /project/cpython/Python/generated_cases.c.h:2358:27 (python3.13+0x3e99c9) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #9 _PyEval_EvalFrame /project/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.13+0x3df70a) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #10 _PyEval_Vector /project/cpython/Python/ceval.c:1813:12 (python3.13+0x3df70a)
    #11 _PyFunction_Vectorcall /project/cpython/Objects/call.c (python3.13+0x1eb65f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #12 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:168:11 (python3.13+0x1ef62f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #13 method_vectorcall /project/cpython/Objects/classobject.c:70:20 (python3.13+0x1ef62f)
    #14 _PyVectorcall_Call /project/cpython/Objects/call.c:273:16 (python3.13+0x1eb2d3) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #15 _PyObject_Call /project/cpython/Objects/call.c:348:16 (python3.13+0x1eb2d3)
    #16 PyObject_Call /project/cpython/Objects/call.c:373:12 (python3.13+0x1eb355) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #17 thread_run /project/cpython/./Modules/_threadmodule.c:337:21 (python3.13+0x567ef2) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #18 pythread_wrapper /project/cpython/Python/thread_pthread.h:243:5 (python3.13+0x4c0e67) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)

  Thread T20 (tid=379188, running) created by main thread at:
    #0 pthread_create <null> (python3.13+0xde1df) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #1 do_start_joinable_thread /project/cpython/Python/thread_pthread.h:290:14 (python3.13+0x4bfd18) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #2 PyThread_start_joinable_thread /project/cpython/Python/thread_pthread.h:314:9 (python3.13+0x4bfb3a) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #3 ThreadHandle_start /project/cpython/./Modules/_threadmodule.c:422:9 (python3.13+0x567a87) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #4 do_start_new_thread /project/cpython/./Modules/_threadmodule.c:1849:9 (python3.13+0x567a87)
    #5 thread_PyThread_start_joinable_thread /project/cpython/./Modules/_threadmodule.c:1972:14 (python3.13+0x566b81) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #6 cfunction_call /project/cpython/Objects/methodobject.c:540:18 (python3.13+0x28afe7) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #7 _PyObject_MakeTpCall /project/cpython/Objects/call.c:242:18 (python3.13+0x1ea44c) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #8 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:166:16 (python3.13+0x1eb0a8) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #9 PyObject_Vectorcall /project/cpython/Objects/call.c:327:12 (python3.13+0x1eb0a8)
    #10 _PyEval_EvalFrameDefault /project/cpython/Python/generated_cases.c.h:1502:19 (python3.13+0x3e625d) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #11 _PyEval_EvalFrame /project/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.13+0x3df3e2) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #12 _PyEval_Vector /project/cpython/Python/ceval.c:1813:12 (python3.13+0x3df3e2)
    #13 PyEval_EvalCode /project/cpython/Python/ceval.c:603:21 (python3.13+0x3df3e2)
    #14 run_eval_code_obj /project/cpython/Python/pythonrun.c:1381:9 (python3.13+0x4a2d0e) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #15 run_mod /project/cpython/Python/pythonrun.c:1466:19 (python3.13+0x4a2435) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #16 pyrun_file /project/cpython/Python/pythonrun.c:1295:15 (python3.13+0x49df45) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #17 _PyRun_SimpleFileObject /project/cpython/Python/pythonrun.c:517:13 (python3.13+0x49df45)
    #18 _PyRun_AnyFileObject /project/cpython/Python/pythonrun.c:77:15 (python3.13+0x49d698) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #19 pymain_run_file_obj /project/cpython/Modules/main.c:410:15 (python3.13+0x4db15f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #20 pymain_run_file /project/cpython/Modules/main.c:429:15 (python3.13+0x4db15f)
    #21 pymain_run_python /project/cpython/Modules/main.c:697:21 (python3.13+0x4da3ac) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #22 Py_RunMain /project/cpython/Modules/main.c:776:5 (python3.13+0x4da3ac)
    #23 pymain_main /project/cpython/Modules/main.c:806:12 (python3.13+0x4da7e8) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #24 Py_BytesMain /project/cpython/Modules/main.c:830:12 (python3.13+0x4da86b) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #25 main /project/cpython/./Programs/python.c:15:12 (python3.13+0x15c7eb) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)

  Thread T3 (tid=379171, running) created by main thread at:
    #0 pthread_create <null> (python3.13+0xde1df) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #1 do_start_joinable_thread /project/cpython/Python/thread_pthread.h:290:14 (python3.13+0x4bfd18) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #2 PyThread_start_joinable_thread /project/cpython/Python/thread_pthread.h:314:9 (python3.13+0x4bfb3a) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #3 ThreadHandle_start /project/cpython/./Modules/_threadmodule.c:422:9 (python3.13+0x567a87) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #4 do_start_new_thread /project/cpython/./Modules/_threadmodule.c:1849:9 (python3.13+0x567a87)
    #5 thread_PyThread_start_joinable_thread /project/cpython/./Modules/_threadmodule.c:1972:14 (python3.13+0x566b81) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #6 cfunction_call /project/cpython/Objects/methodobject.c:540:18 (python3.13+0x28afe7) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #7 _PyObject_MakeTpCall /project/cpython/Objects/call.c:242:18 (python3.13+0x1ea44c) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #8 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:166:16 (python3.13+0x1eb0a8) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #9 PyObject_Vectorcall /project/cpython/Objects/call.c:327:12 (python3.13+0x1eb0a8)
    #10 _PyEval_EvalFrameDefault /project/cpython/Python/generated_cases.c.h:1502:19 (python3.13+0x3e625d) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #11 _PyEval_EvalFrame /project/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.13+0x3df3e2) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #12 _PyEval_Vector /project/cpython/Python/ceval.c:1813:12 (python3.13+0x3df3e2)
    #13 PyEval_EvalCode /project/cpython/Python/ceval.c:603:21 (python3.13+0x3df3e2)
    #14 run_eval_code_obj /project/cpython/Python/pythonrun.c:1381:9 (python3.13+0x4a2d0e) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #15 run_mod /project/cpython/Python/pythonrun.c:1466:19 (python3.13+0x4a2435) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #16 pyrun_file /project/cpython/Python/pythonrun.c:1295:15 (python3.13+0x49df45) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #17 _PyRun_SimpleFileObject /project/cpython/Python/pythonrun.c:517:13 (python3.13+0x49df45)
    #18 _PyRun_AnyFileObject /project/cpython/Python/pythonrun.c:77:15 (python3.13+0x49d698) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #19 pymain_run_file_obj /project/cpython/Modules/main.c:410:15 (python3.13+0x4db15f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #20 pymain_run_file /project/cpython/Modules/main.c:429:15 (python3.13+0x4db15f)
    #21 pymain_run_python /project/cpython/Modules/main.c:697:21 (python3.13+0x4da3ac) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #22 Py_RunMain /project/cpython/Modules/main.c:776:5 (python3.13+0x4da3ac)
    #23 pymain_main /project/cpython/Modules/main.c:806:12 (python3.13+0x4da7e8) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #24 Py_BytesMain /project/cpython/Modules/main.c:830:12 (python3.13+0x4da86b) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #25 main /project/cpython/./Programs/python.c:15:12 (python3.13+0x15c7eb) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)

SUMMARY: ThreadSanitizer: data race (/tmp/output-python/bin/python3.13+0xda21d) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa) in __tsan_memset
==================
==================
WARNING: ThreadSanitizer: data race (pid=379167)
  Write of size 8 at 0x7fffb46b66a0 by thread T4:
    #0 __tsan_memset <null> (python3.13+0xda21d) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #1 dict_popitem_impl /project/cpython/Objects/dictobject.c:4469:25 (python3.13+0x2768db) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #2 dict_popitem /project/cpython/Objects/clinic/dictobject.c.h:220:20 (python3.13+0x2768db)
    #3 method_vectorcall_NOARGS /project/cpython/Objects/descrobject.c:447:24 (python3.13+0x200b61) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #4 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:168:11 (python3.13+0x1eafea) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #5 PyObject_Vectorcall /project/cpython/Objects/call.c:327:12 (python3.13+0x1eafea)
    #6 _PyEval_EvalFrameDefault /project/cpython/Python/generated_cases.c.h:813:23 (python3.13+0x3e35db) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #7 _PyEval_EvalFrame /project/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.13+0x3df70a) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #8 _PyEval_Vector /project/cpython/Python/ceval.c:1813:12 (python3.13+0x3df70a)
    #9 _PyFunction_Vectorcall /project/cpython/Objects/call.c (python3.13+0x1eb65f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #10 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:168:11 (python3.13+0x1ef62f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #11 method_vectorcall /project/cpython/Objects/classobject.c:70:20 (python3.13+0x1ef62f)
    #12 _PyVectorcall_Call /project/cpython/Objects/call.c:273:16 (python3.13+0x1eb2d3) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #13 _PyObject_Call /project/cpython/Objects/call.c:348:16 (python3.13+0x1eb2d3)
    #14 PyObject_Call /project/cpython/Objects/call.c:373:12 (python3.13+0x1eb355) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #15 thread_run /project/cpython/./Modules/_threadmodule.c:337:21 (python3.13+0x567ef2) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #16 pythread_wrapper /project/cpython/Python/thread_pthread.h:243:5 (python3.13+0x4c0e67) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)

  Previous atomic read of size 8 at 0x7fffb46b66a0 by thread T1:
    #0 _Py_atomic_load_ptr_relaxed /project/cpython/./Include/cpython/pyatomic_gcc.h:359:18 (python3.13+0x25f09c) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #1 compare_unicode_unicode_threadsafe /project/cpython/Objects/dictobject.c:1397:26 (python3.13+0x25f09c)
    #2 do_lookup /project/cpython/Objects/dictobject.c:1066:23 (python3.13+0x25f09c)
    #3 unicodekeys_lookup_unicode_threadsafe /project/cpython/Objects/dictobject.c:1423:12 (python3.13+0x25f09c)
    #4 _Py_dict_lookup_threadsafe /project/cpython/Objects/dictobject.c:1478:18 (python3.13+0x260c37) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #5 dict_subscript /project/cpython/Objects/dictobject.c:3311:10 (python3.13+0x275be6) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #6 PyObject_GetItem /project/cpython/Objects/abstract.c:158:26 (python3.13+0x1b8e3c) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #7 _PyEval_EvalFrameDefault /project/cpython/Python/generated_cases.c.h:446:23 (python3.13+0x3e1cdf) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #8 _PyEval_EvalFrame /project/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.13+0x3df70a) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #9 _PyEval_Vector /project/cpython/Python/ceval.c:1813:12 (python3.13+0x3df70a)
    #10 _PyFunction_Vectorcall /project/cpython/Objects/call.c (python3.13+0x1eb65f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #11 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:168:11 (python3.13+0x1ef62f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #12 method_vectorcall /project/cpython/Objects/classobject.c:70:20 (python3.13+0x1ef62f)
    #13 _PyVectorcall_Call /project/cpython/Objects/call.c:273:16 (python3.13+0x1eb2d3) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #14 _PyObject_Call /project/cpython/Objects/call.c:348:16 (python3.13+0x1eb2d3)
    #15 PyObject_Call /project/cpython/Objects/call.c:373:12 (python3.13+0x1eb355) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #16 thread_run /project/cpython/./Modules/_threadmodule.c:337:21 (python3.13+0x567ef2) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #17 pythread_wrapper /project/cpython/Python/thread_pthread.h:243:5 (python3.13+0x4c0e67) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)

  Thread T4 (tid=379172, running) created by main thread at:
    #0 pthread_create <null> (python3.13+0xde1df) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #1 do_start_joinable_thread /project/cpython/Python/thread_pthread.h:290:14 (python3.13+0x4bfd18) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #2 PyThread_start_joinable_thread /project/cpython/Python/thread_pthread.h:314:9 (python3.13+0x4bfb3a) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #3 ThreadHandle_start /project/cpython/./Modules/_threadmodule.c:422:9 (python3.13+0x567a87) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #4 do_start_new_thread /project/cpython/./Modules/_threadmodule.c:1849:9 (python3.13+0x567a87)
    #5 thread_PyThread_start_joinable_thread /project/cpython/./Modules/_threadmodule.c:1972:14 (python3.13+0x566b81) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #6 cfunction_call /project/cpython/Objects/methodobject.c:540:18 (python3.13+0x28afe7) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #7 _PyObject_MakeTpCall /project/cpython/Objects/call.c:242:18 (python3.13+0x1ea44c) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #8 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:166:16 (python3.13+0x1eb0a8) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #9 PyObject_Vectorcall /project/cpython/Objects/call.c:327:12 (python3.13+0x1eb0a8)
    #10 _PyEval_EvalFrameDefault /project/cpython/Python/generated_cases.c.h:1502:19 (python3.13+0x3e625d) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #11 _PyEval_EvalFrame /project/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.13+0x3df3e2) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #12 _PyEval_Vector /project/cpython/Python/ceval.c:1813:12 (python3.13+0x3df3e2)
    #13 PyEval_EvalCode /project/cpython/Python/ceval.c:603:21 (python3.13+0x3df3e2)
    #14 run_eval_code_obj /project/cpython/Python/pythonrun.c:1381:9 (python3.13+0x4a2d0e) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #15 run_mod /project/cpython/Python/pythonrun.c:1466:19 (python3.13+0x4a2435) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #16 pyrun_file /project/cpython/Python/pythonrun.c:1295:15 (python3.13+0x49df45) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #17 _PyRun_SimpleFileObject /project/cpython/Python/pythonrun.c:517:13 (python3.13+0x49df45)
    #18 _PyRun_AnyFileObject /project/cpython/Python/pythonrun.c:77:15 (python3.13+0x49d698) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #19 pymain_run_file_obj /project/cpython/Modules/main.c:410:15 (python3.13+0x4db15f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #20 pymain_run_file /project/cpython/Modules/main.c:429:15 (python3.13+0x4db15f)
    #21 pymain_run_python /project/cpython/Modules/main.c:697:21 (python3.13+0x4da3ac) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #22 Py_RunMain /project/cpython/Modules/main.c:776:5 (python3.13+0x4da3ac)
    #23 pymain_main /project/cpython/Modules/main.c:806:12 (python3.13+0x4da7e8) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #24 Py_BytesMain /project/cpython/Modules/main.c:830:12 (python3.13+0x4da86b) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #25 main /project/cpython/./Programs/python.c:15:12 (python3.13+0x15c7eb) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)

  Thread T1 (tid=379169, running) created by main thread at:
    #0 pthread_create <null> (python3.13+0xde1df) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #1 do_start_joinable_thread /project/cpython/Python/thread_pthread.h:290:14 (python3.13+0x4bfd18) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #2 PyThread_start_joinable_thread /project/cpython/Python/thread_pthread.h:314:9 (python3.13+0x4bfb3a) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #3 ThreadHandle_start /project/cpython/./Modules/_threadmodule.c:422:9 (python3.13+0x567a87) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #4 do_start_new_thread /project/cpython/./Modules/_threadmodule.c:1849:9 (python3.13+0x567a87)
    #5 thread_PyThread_start_joinable_thread /project/cpython/./Modules/_threadmodule.c:1972:14 (python3.13+0x566b81) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #6 cfunction_call /project/cpython/Objects/methodobject.c:540:18 (python3.13+0x28afe7) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #7 _PyObject_MakeTpCall /project/cpython/Objects/call.c:242:18 (python3.13+0x1ea44c) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #8 _PyObject_VectorcallTstate /project/cpython/./Include/internal/pycore_call.h:166:16 (python3.13+0x1eb0a8) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #9 PyObject_Vectorcall /project/cpython/Objects/call.c:327:12 (python3.13+0x1eb0a8)
    #10 _PyEval_EvalFrameDefault /project/cpython/Python/generated_cases.c.h:1502:19 (python3.13+0x3e625d) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #11 _PyEval_EvalFrame /project/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.13+0x3df3e2) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #12 _PyEval_Vector /project/cpython/Python/ceval.c:1813:12 (python3.13+0x3df3e2)
    #13 PyEval_EvalCode /project/cpython/Python/ceval.c:603:21 (python3.13+0x3df3e2)
    #14 run_eval_code_obj /project/cpython/Python/pythonrun.c:1381:9 (python3.13+0x4a2d0e) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #15 run_mod /project/cpython/Python/pythonrun.c:1466:19 (python3.13+0x4a2435) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #16 pyrun_file /project/cpython/Python/pythonrun.c:1295:15 (python3.13+0x49df45) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #17 _PyRun_SimpleFileObject /project/cpython/Python/pythonrun.c:517:13 (python3.13+0x49df45)
    #18 _PyRun_AnyFileObject /project/cpython/Python/pythonrun.c:77:15 (python3.13+0x49d698) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #19 pymain_run_file_obj /project/cpython/Modules/main.c:410:15 (python3.13+0x4db15f) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #20 pymain_run_file /project/cpython/Modules/main.c:429:15 (python3.13+0x4db15f)
    #21 pymain_run_python /project/cpython/Modules/main.c:697:21 (python3.13+0x4da3ac) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #22 Py_RunMain /project/cpython/Modules/main.c:776:5 (python3.13+0x4da3ac)
    #23 pymain_main /project/cpython/Modules/main.c:806:12 (python3.13+0x4da7e8) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #24 Py_BytesMain /project/cpython/Modules/main.c:830:12 (python3.13+0x4da86b) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)
    #25 main /project/cpython/./Programs/python.c:15:12 (python3.13+0x15c7eb) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa)

SUMMARY: ThreadSanitizer: data race (/tmp/output-python/bin/python3.13+0xda21d) (BuildId: 3ae84a424a863e898f8ae5899a1f37386e6a2faa) in __tsan_memset
==================
Traceback (most recent call last):
  File "/project/playground/cpython_checks/dict_popitem/repro.py", line 31, in <module>
    assert len(list(f.result() for f in futures)) == num_workers
               ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/project/playground/cpython_checks/dict_popitem/repro.py", line 31, in <genexpr>
    assert len(list(f.result() for f in futures)) == num_workers
                    ~~~~~~~~^^
  File "/tmp/output-python/lib/python3.13t/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ~~~~~~~~~~~~~~~~~^^
  File "/tmp/output-python/lib/python3.13t/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/tmp/output-python/lib/python3.13t/concurrent/futures/thread.py", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/project/playground/cpython_checks/dict_popitem/repro.py", line 23, in closure
    obj = shared_dict[key]
          ~~~~~~~~~~~^^^^^
KeyError: '0'
ThreadSanitizer: reported 2 warnings

We believe that this should be a bug. An expected behaviour should be no race in c-level.

cpython version:

Python 3.13.2+ experimental free-threading build (heads/3.13:aa2c4e4417d, Mar  5 2025, 16:21:25) [Clang 18.1.3 (1ubuntu1)]

cc @hawkinsp

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixes3.14bugs and security fixestopic-free-threadingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions