8000 [BUG]: Incompatible internals between versions · Issue #5420 · pybind/pybind11 · GitHub
[go: up one dir, main page]

Skip to content
[BUG]: Incompatible internals between versions #5420
@rostan-t

Description

@rostan-t

Required prerequisites

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0