-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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?
v2.13.6
Problem description
Version 2.13.2 replaces std::mutex
with PyMutex
in struct internals
(#5219) in free-threaded builds. This changes the size and alignment of fields in internals
because std::mutex
occupies 40 bytes on x86-64 Linux while PyMutex
only requires 1.
This can lead to crashes if same program imports two libraries compiled resp. with pybind11 before and after 2.13.2. When the first library is imported, its first call to detail::get_internals()
will find that the interpreter state dict doesn't yet contain an internal object, initialize one and store it (internals.h:541). When the next library is imported, it will load the object from the state dict and cast the void *
to internals *
. At this point, no error occurs but the object is already corrupted. When the second library tries to use the cached internals
object, unexpected things such as segfaults will happen.
A fix for this would be to put the mutex at the end of the structure.
Reproducible example code
- Compile module foo with pybind v2.13.1
- Compile module bar with pybind v2.13.2
import foo
import bar
Is this a regression? Put the last known working version here if it is.
v2.13.1