Repository navigation
Closed
Closed
Copy link
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixestopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
When running ./python -m test test_exceptions --parallel-threads=10 in a TSAN build:
WARNING: ThreadSanitizer: data race (pid=763025)
Atomic read of size 4 at 0x7fffbe0718cc by thread T190:
#0 _Py_atomic_load_uint32_relaxed Include/cpython/pyatomic_gcc.h:367 (python+0x1d386e) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
#1 Py_INCREF Include/refcount.h:261 (python+0x1d386e)
#2 _Py_NewRef Include/refcount.h:518 (python+0x1d386e)
#3 dict_setdefault_ref_lock_held Objects/dictobject.c:4386 (python+0x1e6817) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
#4 PyDict_SetDefaultRef Objects/dictobject.c:4403 (python+0x1e6a37) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
#5 intern_common Objects/unicodeobject.c:15820 (python+0x2a7a8d) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
#6 _PyUnicode_InternImmortal Objects/unicodeobject.c:15874 (python+0x2e92d1) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
#7 _PyPegen_new_identifier Parser/pegen.c:549 (python+0xad61f) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
....
Previous write of size 4 at 0x7fffbe0718cc by thread T182:
#0 Py_SET_REFCNT Include/refcount.h:176 (python+0x2a7c9a) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
#1 Py_SET_REFCNT Include/refcount.h:145 (python+0x2a7c9a)
#2 intern_common Objects/unicodeobject.c:15848 (python+0x2a7c9a)
#3 _PyUnicode_InternImmortal Objects/unicodeobject.c:15874 (python+0x2e92d1) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
#4 _PyPegen_new_identifier Parser/pegen.c:549 (python+0xad61f) (BuildId: 5612db6eff0f51c7fd99ee4409b2ceafceea484c)
...
There are a few thread-safety issues with intern_common:
- It can return a string that's not marked as interned because we insert into
internedbefore we mark the string as interned. This can be fixed with some additional locking. - The
Py_SET_REFCNT(s, Py_REFCNT(s) - 2)modification is not thread-safe with respect to other reference count modifications in the free threading build _Py_SetImmortalis not thread-safe in some circumstances (see_Py_SetImmortalmust be run on allocating thread (no-gil) #113956)
The _Py_SetImmortal() issue is tricky and I think it's unlikely to cause problems in practice, so I think we can defer dealing with that for now.
Linked PRs
Metadata
Metadata
Assignees
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixestopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
lysnikolaou commentedon Feb 13, 2025
I hit this while testing PyYAML (though the TSAN stack trace was slightly different). Are you going to work on this @colesbury?
colesbury commentedon Feb 13, 2025
Yes, I'll probably put up a PR today.
Would you please share the TSAN stack trace?
pythongh-129701: Fix a data race in `intern_common` in the free threa…
lysnikolaou commentedon Feb 13, 2025
Sure, here it is.
colesbury commentedon Feb 13, 2025
Thanks. Is this is with 3.13? I think that one was fixed in #128196, but it's not backported to 3.13. That particular PR changes the data types of public fields, so I don't think we can backport it.
It might be worth doing the thread sanitizer tests on 3.14. cc @ngoldbaum
ngoldbaum commentedon Feb 13, 2025
I think I've seen similar tracebacks on 3.13 with PyO3 about interning strings. See this line in the gist from this PyO3 issue.
I want to re-do the TSAN PyO3 tests with 3.14, but I need to update the FFI definitions first. Generally PyO3 doesn't add support for new python versions until beta 1 to avoid dealing with churn.
pythongh-129701: Fix a data race in `intern_common` in the free threa…
lysnikolaou commentedon Feb 13, 2025
Yes, it's with 3.13.2. I'll try 3.14 later today or early tomorrow.
lysnikolaou commentedon Feb 14, 2025
FWIW I did verify that the
unicode_eqstack trace does not appear under 3.14.gh-129701: Fix a data race in `intern_common` in the free threaded bu…
colesbury commentedon Feb 25, 2025
I'm going to close this as the only remaining issue is that _Py_SetImmortal is not thread-safe in some circumstances, which is tracked in:
_Py_SetImmortalmust be run on allocating thread (no-gil) #113956