pybind/pybind11

[BUG] pybind11::cast is not thread safe

Open

#2765 opened on Dec 31, 2020

View on GitHub
 (21 comments) (0 reactions) (0 assignees)C++ (14,677 stars) (2,005 forks)batch import
bugcastershelp wanted

Description

in particular use of loader_life_support seems problematic. This function keeps state in the global loader_patient_stack: https://github.com/pybind/pybind11/blob/b7dfe5cc84ca1891b50e728e83c9b5b393cf5272/include/pybind11/detail/internals.h#L105 In the case where a cast can release the GIL, two thread's push/pop of loader_patient_stack can become interleaved.

While releasing the GIL in a conversion seems like a bad idea, it can be surprisingly hard to avoid. Any cast that results in the bytecode interpreter being run (either directly or through something like a __del__ method) can cause python itself to release GIL (sys.setswitchinterval).

Contributor guide