Skip to content

[BUG]: local_internals appear very thread-unsafe #5799

@oremanj

Description

@oremanj

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

ce71228

Problem description

It appears that, while the shared internals are protected with a PyMutex on free-threaded builds, the module-local local_internals have no synchronization at all. Some accesses happen to be protected by the internals mutex, but that mutex does not seem to be held on calls to get_type_info(const std::type_index&), which calls get_local_type_info, which does a direct lookup in registered_types_cpp. If another thread is concurrently registering or unregistering a type, a crash would probably result in some scenarios, especially if the registered_types_cpp map needs to be rehashed to accommodate the new insertion.

This is from reading the code, not from observing behavior, so it's possible I'm missing something. If I'm not, probably the easiest fix would be to wrap the local internals lookup in with_internals so it's protected by the global mutex. Some refactoring might enable both the local and global lookups to occur under the same lock so that this change wouldn't regress performance.

Reproducible example code


Is this a regression? Put the last known working version here if it is.

Not a regression

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