Fix TSS key exhaustion in implicitly_convertible()#6020
Merged
rwgk merged 1 commit intopybind:masterfrom Mar 29, 2026
Merged
Conversation
Replace `static thread_specific_storage<int>` with `thread_local bool` in the implicit conversion reentrancy guard. Since implicitly_convertible is a template function, each unique <InputType, OutputType> pair created its own TSS key via PyThread_tss_create(). Projects with hundreds of modules and many implicit conversions could exhaust PTHREAD_KEYS_MAX (1024 on Linux, 512 on macOS), especially on Python 3.12+ where CPython itself consumes more TSS keys for subinterpreter support. thread_local bool is safe here because: - bool is trivially destructible, so it works on all C++11 platforms including older macOS (the concern that motivated the TSS approach in PR pybind#5777 applied only to types with non-trivial destructors needing __cxa_thread_atexit runtime support) - Each thread gets its own copy, so it is thread-safe for free-threading - Subinterpreter sharing is benign: the guard prevents recursive implicit conversions on the same thread regardless of which interpreter is active - The v3.0.0 code already used thread_local bool under Py_GIL_DISABLED This effectively reverts the core change from PR pybind#5777 while keeping the non-copyable/non-movable set_flag guard. Made-with: Cursor
Collaborator
Author
|
Thanks @b-pass |
rwgk
added a commit
to rwgk/pybind11
that referenced
this pull request
Mar 30, 2026
…ybind#6020) Replace `static thread_specific_storage<int>` with `thread_local bool` in the implicit conversion reentrancy guard. Since implicitly_convertible is a template function, each unique <InputType, OutputType> pair created its own TSS key via PyThread_tss_create(). Projects with hundreds of modules and many implicit conversions could exhaust PTHREAD_KEYS_MAX (1024 on Linux, 512 on macOS), especially on Python 3.12+ where CPython itself consumes more TSS keys for subinterpreter support. thread_local bool is safe here because: - bool is trivially destructible, so it works on all C++11 platforms including older macOS (the concern that motivated the TSS approach in PR pybind#5777 applied only to types with non-trivial destructors needing __cxa_thread_atexit runtime support) - Each thread gets its own copy, so it is thread-safe for free-threading - Subinterpreter sharing is benign: the guard prevents recursive implicit conversions on the same thread regardless of which interpreter is active - The v3.0.0 code already used thread_local bool under Py_GIL_DISABLED This effectively reverts the core change from PR pybind#5777 while keeping the non-copyable/non-movable set_flag guard. Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #5975
PR #5777 (included in v3.0.1) replaced the
static bool/thread_local boolreentrancy guard inimplicitly_convertible()withstatic thread_specific_storage<int>to make it sub-interpreter and free-threading safe. Becauseimplicitly_convertibleis a template function parametrized on<InputType, OutputType>, every unique type pair instantiation creates its own staticthread_specific_storage, each of which allocates a TSS key viaPyThread_tss_create().Projects with hundreds of modules and many implicit conversions (the reporters have 590+ and 1000+
PYBIND11_MODULEs) exhaust the OS TSS key limit (PTHREAD_KEYS_MAX: 1024 on Linux, 512 on macOS). The problem manifests on Python 3.12+ because CPython itself consumes more TSS keys for subinterpreter support, reducing the budget available to user code.The fix replaces
static thread_specific_storage<int>withthread_local bool:thread_localuses compiler/OS TLS storage (e.g.,__threadsegment on Linux), which has effectively unlimited capacity.booldoes not require__cxa_thread_atexitruntime support, so it works on all C++11 platforms including older macOS targets. (This was the concern that motivated using TSS in fix: make implicitly_convertable sub-interpreter and free-threading safe #5777, but it only applies to types with non-trivial destructors.)thread_local boolunder#ifdef Py_GIL_DISABLED.The non-copyable/non-movable
set_flagRAII guard (added in #5777) is retained but simplified back to wrappingbool&.Suggested changelog entry:
thread_specific_storageallocation inimplicitly_convertible().