Commit 5fe0bc5
committed
Add leak_detach path for internals after shutdown
Py_IsInitialized/Py_IsFinalizing are runtime-wide, not per-interpreter, and
Py_IsFinalizing stays set after Py_Finalize until the next init. Using those
checks to decide whether to DECREF internals can skip cleanup or touch the
C-API after a specific interpreter is gone.
Add internals::leak_detach() and local_internals::leak_detach() to clear the
owned PyType/PyObject pointers without calling into Python, and invoke that
from internals_pp_manager::destroy() before deleting the pp in the post-
Py_Finalize/Py_EndInterpreter paths. The destructors now always Py_CLEAR when
invoked during state-dict teardown, preserving cleanup of the pybind11 heap
types while avoiding UB in late-destroy scenarios.
Cursor GPT-5.2 Codex Extra High
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ → Could you please read this file for context: /wrk/logs/gh_pr_info_pybind_pybind11_5958_2026-01-19+100747.md │
│ │
│ In my last comment there I wrote: I'll point Cursor to this, the pybind11 sources, and the CPython 3.14 sources, and ask it to look very thoroughly to figure out what is the │
│ best achievable solution in terms of avoiding UB but leaking as little as possible. │
│ │
│ The up-to-date pybind11 sources are right here (master branch). │
│ │
│ The CPython sources are here: │
│ │
│ smc120-0009.ipp2a2.colossus.nvidia.com:/wrk/clone/cpython $ git branch │
│ * 3.14 │
│ main │
│ v3.14.2_release │
│ │
│ JIC we need this for your analysis: │
│ │
│ I last git pulled the CPython 3.14 branch yesterday and built from sources using this script: │
│ │
│ /home/rgrossekunst/rwgk_config/bin/build_cpython_from_git_twice.sh │
│ │
│ The installations are here: │
│ │
│ /wrk/cpython_installs/3.14_branch_23e3c0ae867_default │
│ /wrk/cpython_installs/3.14_branch_23e3c0ae867_freethreaded │
│ │
│ Could you please "look very thoroughly to figure out what is the best achievable solution in terms of avoiding UB but leaking as little as possible.", also taking Joshua's "I │
│ think the correct solution is a internals::leak_detach() method" comment into account? │
│ │
│ Please use the cursor_workspace/ subdirectly here for all intermediate/temporary files. Do NOT use /tmp. │
│ │
│ If you want to rebuild the pybind11 unit tests for your analysis, please do NOT use cmake. Use this command instead: │
│ │
│ cd /wrk/bld/pybind11_gcc_v3.14.2_df793163d58_default/ && scons -j $(nproc) && TestVenv/bin/python3 ../../clone/pybind11_scons/run_tests.py ../../forked/pybind11 24 │
│ │
│ Currently I want to focus on the "default" cpython build. We can leave any "freethreaded" considerations for later. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘1 parent da6e071 commit 5fe0bc5
1 file changed
+29
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | 198 | | |
207 | 199 | | |
208 | 200 | | |
| |||
316 | 308 | | |
317 | 309 | | |
318 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
319 | 319 | | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
329 | 326 | | |
330 | 327 | | |
331 | 328 | | |
| |||
344 | 341 | | |
345 | 342 | | |
346 | 343 | | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
347 | 349 | | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
355 | 354 | | |
356 | 355 | | |
357 | 356 | | |
| |||
716 | 715 | | |
717 | 716 | | |
718 | 717 | | |
719 | | - | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
720 | 721 | | |
721 | 722 | | |
722 | 723 | | |
723 | 724 | | |
724 | 725 | | |
725 | 726 | | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
726 | 730 | | |
727 | 731 | | |
728 | 732 | | |
| |||
0 commit comments