Skip to content

panic on attach if interpreter is shutting down - #5317

Merged
davidhewitt merged 5 commits into
PyO3:mainfrom
davidhewitt:attach-panic-shutting-down
Aug 21, 2025
Merged

davidhewitt merged 5 commits into
PyO3:mainfrom
davidhewitt:attach-panic-shutting-down

Conversation

@davidhewitt

Copy link
Copy Markdown
Member

This is an attempt to solve the build failure seen in #5309 (at least locally, it seems to be good enough).

The problem is that the test added in #4874 which repeatedly calls Python::attach to try to hit the "hang thread" case seems to just cause Python 3.14 to segfault. The docs explicitly say this might happen if you call PyGILState_Ensure during finalization, to be fair.

To try to avoid that crash, this adds the FFI definition Py_IsFinalizing (available on 3.13 and up) and uses that to try a best-effort panic instead of segfault. As a follow-up I'll probably expose Python::try_attach and change the test to use it, to avoid a slightly messy panic at the end of the test suite.

While adding Py_IsFinalizing I synced the pylifecycle definitions and corrected a few other cases.

@codspeed

codspeed Bot commented Aug 13, 2025 •

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #5317 will not alter performance

Comparing davidhewitt:attach-panic-shutting-down (d4febfe) with main (12a1792)

Summary

✅ 93 untouched benchmarks

@davidhewitt
davidhewitt enabled auto-merge August 20, 2025 20:13
@davidhewitt

Copy link
Copy Markdown
Member Author

Thanks for the review! FYI I'm just drafting a follow up to expose Python::try_attach.

@davidhewitt
davidhewitt added this pull request to the merge queue Aug 20, 2025
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Aug 21, 2025
@davidhewitt
davidhewitt added this pull request to the merge queue Aug 21, 2025
Merged via the queue into PyO3:main with commit e93393c Aug 21, 2025
@davidhewitt
davidhewitt deleted the attach-panic-shutting-down branch August 21, 2025 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants