Skip to content

Conversation

@MiguelCompany
Copy link
Contributor

Description

When using gil_scoped_release, care must be taken not to take the GIL when the Python runtime is finalizing.

According to this one can call _Py_IsFinalizing() to check the failure condition and then disarm() the gil_scoped_release object so it does not take the GIL back.

This would fix (in theroy) #1484 but locally it doesn't seem so. Opening in draft nonetheless, in case someone wants to take this as a base for a proper fix.

Is this user-facing behavior change?

Did you use Generative AI?

Additional Information


~gil_scoped_release() {
// Avoid taking the GIL if runtime is finalizing
if (_Py_IsFinalizing()) {
Copy link
Member

@skyegalaxy skyegalaxy Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function _Py_IsFinalizing was removed in Python 3.13 (in favor of a public method) this might require some forward compatibility hackiness. In situations like this there's usually a way to check the python version, e.g. something like

#if PY_VERSION_HEX < Py_PACK_FULL_VERSION(3, 13)) 
    #define IsFinalizing _Py_IsFinalizing
#else
    #define IsFinalizing Py_IsFinalizing
#endif

@MiguelCompany MiguelCompany force-pushed the bugfix/disarm-gil-release branch from 2662bba to b5b4ff9 Compare December 3, 2025 06:47
@MiguelCompany
Copy link
Contributor Author

MiguelCompany commented Dec 3, 2025

@skyegalaxy Thanks for the review. I applied your suggestion.

Note however that this is still a draft because:

This would fix (in theroy) #1484 but locally it doesn't seem so. Opening in draft nonetheless, in case someone wants to take this as a base for a proper fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants