Skip to content

TSAN alarm in dictobject.c while using setattr #144774

@Naserume

Description

@Naserume

Bug report

Bug description:

While working around with my previous issue, I found another TSAN alarm.

from threading import Thread
import random
import copy

E = Exception()

def test1():
    for _ in range(100):
        setattr(E, 'x', random.randint(0,1000))
        copy.copy(E)

def main():
    threads = []
    for _ in range(4):
        threads.append(Thread(target=test1))

    for t in threads:
        t.start()

    for t in threads:
        t.join()

if __name__ == "__main__":
    main()

TSAN report:

WARNING: ThreadSanitizer: data race (pid=455134)
  Read of size 8 at 0x7fffb60c01b0 by thread T1:
    #0 _PyDict_Next /cpython/Objects/dictobject.c:3020:40 (python3.15t+0x291d09) 
    #1 PyDict_Next /cpython/Objects/dictobject.c:3074:12 (python3.15t+0x291d09)
    #2 BaseException___setstate___impl /cpython/Objects/exceptions.c:245:16 (python3.15t+0x232246) 
    #3 BaseException___setstate__ /cpython/Objects/clinic/exceptions.c.h:48:20 (python3.15t+0x232246)
    #4 method_vectorcall_O /cpython/Objects/descrobject.c:476:24 (python3.15t+0x2262d5) 
    #5 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x20c593) 
    #6 PyObject_Vectorcall /cpython/Objects/call.c:327:12 (python3.15t+0x20c593)
    #7 _Py_VectorCallInstrumentation_StackRefSteal /cpython/Python/ceval.c:762:11 (python3.15t+0x422cbc) 
    #8 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:1817:35 (python3.15t+0x428ce6) 
    #9 _PyEval_EvalFrame /cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15t+0x422090) 
    #10 _PyEval_Vector /cpython/Python/ceval.c:2125:12 (python3.15t+0x422090)
    #11 _PyFunction_Vectorcall /cpython/Objects/call.c (python3.15t+0x20cc0f) 
    #12 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x2112e6) 
    #13 method_vectorcall /cpython/Objects/classobject.c:74:20 (python3.15t+0x2112e6)
    #14 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x476281) 
    #15 context_run /cpython/Python/context.c:727:29 (python3.15t+0x476281)
    #16 method_vectorcall_FASTCALL_KEYWORDS /cpython/Objects/descrobject.c:421:24 (python3.15t+0x226017) 
    #17 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x20c593) 
    #18 PyObject_Vectorcall /cpython/Objects/call.c:327:12 (python3.15t+0x20c593)
    #19 _Py_VectorCallInstrumentation_StackRefSteal /cpython/Python/ceval.c:762:11 (python3.15t+0x422cbc) 
    #20 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:1817:35 (python3.15t+0x428ce6) 
    #21 _PyEval_EvalFrame /cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15t+0x422090) 
    #22 _PyEval_Vector /cpython/Python/ceval.c:2125:12 (python3.15t+0x422090)
    #23 _PyFunction_Vectorcall /cpython/Objects/call.c (python3.15t+0x20cc0f) 
    #24 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x2112e6) 
    #25 method_vectorcall /cpython/Objects/classobject.c:74:20 (python3.15t+0x2112e6)
    #26 _PyVectorcall_Call /cpython/Objects/call.c:273:16 (python3.15t+0x20c89b) 
    #27 _PyObject_Call /cpython/Objects/call.c:348:16 (python3.15t+0x20c89b)
    #28 PyObject_Call /cpython/Objects/call.c:373:12 (python3.15t+0x20c905) 
    #29 thread_run /cpython/./Modules/_threadmodule.c:387:21 (python3.15t+0x5f5512) 
    #30 pythread_wrapper /cpython/Python/thread_pthread.h:234:5 (python3.15t+0x526127) 

  Previous atomic write of size 8 at 0x7fffb60c01b0 by thread T2:
    #0 _Py_atomic_store_ptr_release /cpython/./Include/cpython/pyatomic_gcc.h:565:3 (python3.15t+0x29435b) 
    #1 insertdict /cpython/Objects/dictobject.c (python3.15t+0x29435b)
    #2 setitem_take2_lock_held /cpython/Objects/dictobject.c:2684:12 (python3.15t+0x2936a7) 
    #3 setitem_lock_held /cpython/Objects/dictobject.c:2721:12 (python3.15t+0x29ff83) 
    #4 _PyDict_SetItem_LockHeld /cpython/Objects/dictobject.c:6850:16 (python3.15t+0x29ff83)
    #5 _PyObjectDict_SetItem /cpython/Objects/dictobject.c:7549:11 (python3.15t+0x2a3d97) 
    #6 _PyObject_GenericSetAttrWithDict /cpython/Objects/object.c:2055:19 (python3.15t+0x2cbf44) 
    #7 PyObject_GenericSetAttr /cpython/Objects/object.c:2083:12 (python3.15t+0x2cc697) 
    #8 PyObject_SetAttr /cpython/Objects/object.c:1528:15 (python3.15t+0x2c8f4f) 
    #9 builtin_setattr_impl /cpython/Python/bltinmodule.c:1744:9 (python3.15t+0x41d2e7) 
    #10 builtin_setattr /cpython/Python/clinic/bltinmodule.c.h:692:20 (python3.15t+0x41d2e7)
    #11 cfunction_vectorcall_FASTCALL /cpython/Objects/methodobject.c:449:24 (python3.15t+0x2bbb1e) 
    #12 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x20c593) 
    #13 PyObject_Vectorcall /cpython/Objects/call.c:327:12 (python3.15t+0x20c593)
    #14 _Py_VectorCallInstrumentation_StackRefSteal /cpython/Python/ceval.c:762:11 (python3.15t+0x422cbc) 
    #15 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:1817:35 (python3.15t+0x428ce6) 
    #16 _PyEval_EvalFrame /cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15t+0x422090) 
    #17 _PyEval_Vector /cpython/Python/ceval.c:2125:12 (python3.15t+0x422090)
    #18 _PyFunction_Vectorcall /cpython/Objects/call.c (python3.15t+0x20cc0f) 
    #19 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x2112e6) 
    #20 method_vectorcall /cpython/Objects/classobject.c:74:20 (python3.15t+0x2112e6)
    #21 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x476281) 
    #22 context_run /cpython/Python/context.c:727:29 (python3.15t+0x476281)
    #23 method_vectorcall_FASTCALL_KEYWORDS /cpython/Objects/descrobject.c:421:24 (python3.15t+0x226017) 
    #24 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x20c593) 
    #25 PyObject_Vectorcall /cpython/Objects/call.c:327:12 (python3.15t+0x20c593)
    #26 _Py_VectorCallInstrumentation_StackRefSteal /cpython/Python/ceval.c:762:11 (python3.15t+0x422cbc) 
    #27 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:1817:35 (python3.15t+0x428ce6) 
    #28 _PyEval_EvalFrame /cpython/./Include/internal/pycore_ceval.h:118:16 (python3.15t+0x422090) 
    #29 _PyEval_Vector /cpython/Python/ceval.c:2125:12 (python3.15t+0x422090)
    #30 _PyFunction_Vectorcall /cpython/Objects/call.c (python3.15t+0x20cc0f) 
    #31 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:136:11 (python3.15t+0x2112e6) 
    #32 method_vectorcall /cpython/Objects/classobject.c:74:20 (python3.15t+0x2112e6)
    #33 _PyVectorcall_Call /cpython/Objects/call.c:273:16 (python3.15t+0x20c89b) 
    #34 _PyObject_Call /cpython/Objects/call.c:348:16 (python3.15t+0x20c89b)
    #35 PyObject_Call /cpython/Objects/call.c:373:12 (python3.15t+0x20c905) 
    #36 thread_run /cpython/./Modules/_threadmodule.c:387:21 (python3.15t+0x5f5512) 
    #37 pythread_wrapper /cpython/Python/thread_pthread.h:234:5 (python3.15t+0x526127) 

SUMMARY: ThreadSanitizer: data race /cpython/Objects/dictobject.c:3020:40 in _PyDict_Next

Version: Python 3.15.0a5+ free-threading build (heads/main:d5f96c86653, Feb 11 2026, 04:50:14) [Clang 18.1.3 (1ubuntu1)]

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions