Skip to content

Conversation

@gpshead
Copy link
Member

@gpsheadgpshead commented Aug 15, 2025

Instead of surprise crashes and memory corruption, we now hang threads that attempt to re-enter the Python interpreter after Python runtime finalization has started. These are typically daemon threads (our long standing mis-feature) but could also be threads spawned by extension modules that then try to call into Python. This backport documents that the PyThread_exit_thread public C API should not be used (deprecated in 3.14) as there is no plausible safe way to accomplish that on any supported platform in the face of things like C++ code with finalizers anywhere on a thread's stack. Doing this was the least bad option.

(cherry picked from commit 8cc5aa4)


📚 Documentation preview 📚: https://cpython-previews--137827.org.readthedocs.build/

…the GIL during finalization (pythonGH-105805) Instead of surprise crashes and memory corruption, we now hang threads that attempt to re-enter the Python interpreter after Python runtime finalization has started. These are typically daemon threads (our long standing mis-feature) but could also be threads spawned by extension modules that then try to call into Python. This marks the `PyThread_exit_thread` public C API as deprecated as there is no plausible safe way to accomplish that on any supported platform in the face of things like C++ code with finalizers anywhere on a thread's stack. Doing this was the least bad option. (cherry picked from commit 8cc5aa4) Co-authored-by: Jeremy Maitin-Shepard <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]>
@gpsheadgpsheadforce-pushed the backport-8cc5aa4-3.13 branch from 4f77306 to 977b0d4CompareAugust 15, 2025 16:28
@gpsheadgpshead marked this pull request as ready for review August 15, 2025 16:30
@gpsheadgpshead added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.13 bugs and security fixes labels Aug 15, 2025
@gpsheadgpshead self-assigned this Aug 15, 2025
@gpsheadgpsheadforce-pushed the backport-8cc5aa4-3.13 branch from 977b0d4 to f412737CompareAugust 15, 2025 16:34
@gpsheadgpshead added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 15, 2025
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @gpshead for commit f412737 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F137827%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-botbedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 15, 2025
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

I cannot say it LGTM, but it looks less bad than anything else. ☹️ It seems there are no good solutions here.

@gpsheadgpshead merged commit 9face21 into python:3.13Aug 17, 2025
40 checks passed
@gpshead
Copy link
MemberAuthor

I'll leave this PR around for reference, but do not intend to merge it so late in the 3.13 release cycle even though I believe this least bad option is the only viable one. It is a behavior change, even if the existing behavior is an undertiministic sometimes crashing or sometimes working. See #87135 (comment) and #123940 (comment) for rationale from @encukou about invasiveness of such a change as in a bugfix release.

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

Labels

3.13bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@gpshead@bedevere-bot@serhiy-storchaka@jbms