-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add per-interpreter storage for gil_safe_call_once_and_store
#5933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
58834ac to
a46973b
Compare
a46973b to
e741760
Compare
8e5bb6e to
d5b8813
Compare
| // FIXME: We cannot use `get_num_interpreters_seen() > 1` here to create a fast path for | ||
| // the multi-interpreter case. The singleton may be initialized by a subinterpreter not the | ||
| // main interpreter. | ||
| // | ||
| // For multi-interpreter support, the subinterpreters can be initialized concurrently, and | ||
| // the first time this function may not be called in the main interpreter. | ||
| // For example, a clean main interpreter that does not import any pybind11 module and then | ||
| // spawns multiple subinterpreters using `InterpreterPoolExecutor` that each imports a | ||
| // pybind11 module concurrently. | ||
| if (get_num_interpreters_seen() > 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @b-pass @rwgk for thoughts about this, see the code comment referenced for more details.
I test the patch in this PR in:
Most things works fine except test_import_in_subinterpreter_before_main:
run_in_subprocess(
"""
with contextlib.closing(interpreters.create()) as subinterpreter:
subinterpreter.exec('import optree')
import optree
"""
)If I remove the get_num_interpreters_seen() > 1 condition, my import test works but the cpptest in pybind11 CI breaks because internals_singleton_pp_ is never initialized. For instance, the memory address get_internals() should be a shared constant for differenet pybind11 modules in a single program.
Description
Fixes #5926
For
!defined(PYBIND11_HAS_SUBINTERPRETER_SUPPORT), the previous behavior is unchanged.For
defined(PYBIND11_HAS_SUBINTERPRETER_SUPPORT), the pybind11internalsis already a per-interpreter-dependent singleton stored in the interpreter's state dict. This PR adds a new map to the interpreter-dependentinternalsfrom thegil_safe_call_once_and_store's address to the result, where thegil_safe_call_once_and_storeis a static object that has a fixed address.Suggested changelog entry:
gil_safe_call_once_and_storeto make it safe under multi-interpreters.