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.
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.
gil_scoped_acquireinside thecall_oncelambda inverts the lock order relative to callers that still hold the GIL, creating an ABBA deadlock. Consider two threads both hitting an uninitialisedtexentry point simultaneously: Thread A (GIL released viacall_guard<gil_scoped_release>) wins thecall_oncerace and blocks ongil_scoped_acquire; Thread B (holds the GIL, e.g. callingquantize) reachescall_onceand blocks waiting for Thread A's lambda to complete. Thread A owns thecall_onceinternal lock and needs the GIL; Thread B owns the GIL and needs thecall_oncelock — neither can proceed.The fix is to acquire the GIL before
call_onceso that every caller establishes the same ordering (GIL →call_oncelock), eliminating the inversion. After the one-time init completes, subsequent calls toinit_extension()will acquire the GIL briefly and return immediately fromcall_once;pybind11::gil_scoped_acquireis a no-op when the calling thread already holds the GIL, so callers that never released it are unaffected.