8000 [BUG]: Segmentation fault on Python finalization with `PYBIND11_HAS_SUBINTERPRETER_SUPPORT` on Python 3.12d · Issue #5687 · pybind/pybind11 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[BUG]: Segmentation fault on Python finalization with PYBIND11_HAS_SUBINTERPRETER_SUPPORT on Python 3.12d #5687

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

Closed
2 of 3 tasks
XuehaiPan opened this issue May 23, 2025 · 2 comments
Labels
triage New bug, unverified

Comments

@XuehaiPan
Copy link
Contributor
XuehaiPan commented May 23, 2025

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

master @ 98bd78f

Problem description

Building with Python 3.12+ will automatically enable subinterpreter support after #5564.

While testing with Python 3.12 with Py_DEBUG enabled, a segmentation fault will be raised on Python finalization:

Fatal Python error: _PyInterpreterState_GET: the function must be called with the GIL held, after Python initialization and before Python finalization, but the GIL is released (the current Python thread state is NULL)

Reproducible example code


Is this a regression? Put the last known working version here if it is.

Not a regression

@XuehaiPan XuehaiPan added the triage New bug, unverified label May 23, 2025
@XuehaiPan XuehaiPan changed the title [BUG]: Segmentation fault on module initialization with PYBIND11_HAS_SUBINTERPRETER_SUPPORT on Python 3.12 [BUG]: Segmentation fault on module initialization with PYBIND11_HAS_SUBINTERPRETER_SUPPORT on Python 3.12d May 23, 2025
@XuehaiPan XuehaiPan changed the title [BUG]: Segmentation fault on module initialization with PYBIND11_HAS_SUBINTERPRETER_SUPPORT on Python 3.12d [BUG]: Segmentation fault on Python finalization with PYBIND11_HAS_SUBINTERPRETER_SUPPORT on Python 3.12d May 23, 2025
@b-pass
Copy link
Contributor
b-pass commented May 23, 2025

Ah, I think this is caused by the multiphase init changes and not (directly) by subinterpreter support. Are you able to try this one-line change and see if it fixes the issue?

diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h
index 2e81150f..6a89d332 100644
--- a/include/pybind11/detail/common.h
+++ b/include/pybind11/detail/common.h
@@ -386,6 +386,7 @@ PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
                 slots,                                                                            \
                 ##__VA_ARGS__);                                                                   \
         }();                                                                                      \
+        Py_SET_REFCNT(result.ptr(), _Py_IMMORTAL_REFCNT);                                         \
         return result.ptr();                                                                      \
     }

If this works for you I will make a PR for it.

@XuehaiPan
Copy link
Contributor Author

Fixed on latest HEAD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage New bug, unverified
Projects
None yet
Development

No branches or pull requests

2 participants
0