Repository navigation
Closed
Labels
triageNew bug, unverified
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
master (fdec8c3)
Problem description
On free-threaded Python with a non-libstdc++ build check_internals_local_exception_translator() reads and modifiesinternals.registered_exception_translators without holding exception_translator_mutex:
PYBIND11_INLINE void check_internals_local_exception_translator(internals *internals_ptr) {
if (internals_ptr) {
for (auto et : internals_ptr->registered_exception_translators) { // <--- Read
if (et == &translate_local_exception) {
return;
}
}
internals_ptr->registered_exception_translators.push_front(&translate_local_exception); // <--- Write
}
}- All other accesses to this list go through
with_exception_translators()which takes the lock. - This function appears to run every time a module fetches internals that already exist, e.g. from
ensure_internals()in module init. Each extension module has its owntranslate_local_exception, so a module's first import always pushes a new entry. - Under free threading, if another thread is walking the list from a different module at that moment, the list is read and written concurrently.
- I ran into this as a TSan report (CPython 3.14 free-threaded, clang + libc++, pybind11 v3.1.0) where two pybind11 extension modules were first imported from different threads at about the same time.
Note that I updated the TSan report to hide internal details.
WARNING: ThreadSanitizer: data race
Read of size 8 at 0x72480003a8e0 by thread T345:
#0 pybind11::detail::get_internals() libcxx/include/forward_list:1114:66 (module_a.abi3.so)
#1 PyInit_module_a module_a.cc:19:1 (module_a.abi3.so)
#2 _PyImport_RunModInitFunc Python/importdl.c:428:19
#3 import_run_extension Python/import.c:2110:14
#4 _imp_create_dynamic Python/import.c:4870:11
...
Previous write of size 8 at 0x72480003a8e0 by main thread:
#0 pybind11::detail::get_internals() libcxx/include/forward_list:1113:37 (module_b.abi3.so)
#1 PyInit_module_b module_b.cc:217:1 (module_b.abi3.so)
#2 _PyImport_RunModInitFunc Python/importdl.c:428:19
#3 import_run_extension Python/import.c:2110:14
#4 _imp_create_dynamic Python/import.c:4870:11
...
Location is heap block of size 352 at 0x72480003a800 allocated by main thread:
#0 operator new(unsigned long) compiler-rt/lib/tsan/rtl/tsan_new_delete.cpp:69:3
#1 pybind11::detail::get_internals() pybind11/detail/internals.h:467:25 (module_c.so)
#2 PyInit_module_c module_c.cc:10:3 (module_c.so)
#3 _PyImport_RunModInitFunc Python/importdl.c:428:19
...
SUMMARY: ThreadSanitizer: data race libcxx/include/forward_list:1114:66 in pybind11::detail::get_internals()
I double checked the existing issues and I couldn't see this issue already reported. If I'm missing anything, please let me know. Thanks!
Reproducible example code
// mod_a.cpp
#include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(mod_a, m, py::mod_gil_not_used()) {
// Re-fetches internals, which runs the on_fetch hook.
m.def("ensure_internals", [] { py::detail::ensure_internals(); });
}
// mod_b.cpp
#include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(mod_b, m, py::mod_gil_not_used()) {}
# repro.py
import threading
import mod_a
stop = threading.Event()
def loop():
while not stop.is_set():
mod_a.ensure_internals()
t = threading.Thread(target=loop)
t.start()
import mod_b # first import adds mod_b's translate_local_exception
stop.set()
t.join()
Is this a regression? Put the last known working version here if it is.
Not a regression
Metadata
Metadata
Assignees
Labels
triageNew bug, unverified
Activity