-
Notifications
You must be signed in to change notification settings - Fork 266
Fix SIGSEGV/SIGABRT during interpreter shutdown on Python < 3.11 #495
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
Open
nbouvrette
wants to merge
1
commit into
python-greenlet:master
Choose a base branch
from
nbouvrette:fix/safe-finalization-py310
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix SIGSEGV/SIGABRT during interpreter shutdown on Python < 3.11 #495
nbouvrette
wants to merge
1
commit into
python-greenlet:master
from
nbouvrette:fix/safe-finalization-py310
+361
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nbouvrette
added a commit
to nbouvrette/greenlet
that referenced
this pull request
Feb 11, 2026
Re-add Python 3.9 to requires-python and trove classifiers in pyproject.toml. No C/C++ code was removed when 3.9 support was dropped in 3.3.0 — the drop was purely a packaging metadata change. Combined with the safe finalization fix in the parent commit (PR python-greenlet#495), greenlet now works reliably on Python 3.9 during interpreter shutdown, which was the primary stability concern for older Python versions. This gives teams still running Python 3.9 in production a stable greenlet release path while they plan their Python upgrade. Also adds CHANGES.rst entries for both this change and the finalization fix. Co-authored-by: Cursor <cursoragent@cursor.com>
b7878d0 to
a39c535
Compare
nbouvrette
added a commit
to nbouvrette/greenlet
that referenced
this pull request
Feb 11, 2026
Re-add Python 3.9 to requires-python, trove classifiers, and CI test matrix. No C/C++ code was removed when 3.9 support was dropped in 3.3.0 — the drop was purely a packaging metadata change. Combined with the safe finalization fix in the parent commit (PR python-greenlet#495), greenlet now works reliably on Python 3.9 during interpreter shutdown, which was the primary stability concern for older Python versions. Changes: - pyproject.toml: requires-python >= 3.9, add 3.9 trove classifier - .github/workflows/tests.yml: add "3.9" to test matrix, exclude windows-11-arm (not available for 3.9) - CHANGES.rst: add entries for both this change and the finalization fix Co-authored-by: Cursor <cursoragent@cursor.com>
93cd07e to
4eb8fc1
Compare
During interpreter finalization (Py_FinalizeEx), active greenlets being
deallocated would trigger g_switch() to throw GreenletExit. This performs
a stack switch and executes Python code in a partially-torn-down
interpreter, causing:
- SIGSEGV (signal 11) on greenlet 3.x
- SIGABRT (signal 6 / "Accessing state after destruction") on greenlet 2.x
On Python >= 3.11, CPython's restructured finalization internals (frame
representation, data stack management, recursion tracking) make g_switch()
during finalization safe. On Python < 3.11, this was not the case.
This commit adds two guards, compiled only on Python < 3.11
(!GREENLET_PY311):
1. In _green_dealloc_kill_started_non_main_greenlet (PyGreenlet.cpp):
When the interpreter is finalizing, call murder_in_place() directly
instead of attempting g_switch(). This marks the greenlet as dead
without throwing GreenletExit, avoiding the crash at the cost of not
running cleanup code inside the greenlet.
2. In ~ThreadState (TThreadState.hpp):
When the interpreter is finalizing, skip the GC-based leak detection
that calls PyImport_ImportModule("gc"), which is unsafe when the
import machinery is partially torn down. Only perform minimal safe
cleanup (clearing strong references).
On Python >= 3.11, no changes are made — the existing behavior (throwing
GreenletExit via g_switch, running cleanup code) continues to work
correctly during finalization.
Also adds test_interpreter_shutdown.py with 9 subprocess-based tests
covering:
- Single/multiple/nested/threaded/deeply-nested active greenlets at
shutdown (no-crash safety on all Python versions)
- Version-aware behavioral tests verifying that GreenletExit cleanup
code runs on Python >= 3.11 but is correctly skipped on < 3.11
- Edge cases: active exception context, stress test with 50 greenlets
Fixes python-greenlet#411
See also python-greenlet#351, python-greenlet#376
Co-authored-by: Cursor <cursoragent@cursor.com>
4eb8fc1 to
292e126
Compare
nbouvrette
added a commit
to nbouvrette/greenlet
that referenced
this pull request
Feb 11, 2026
Re-add Python 3.9 to requires-python, trove classifiers, and CI test matrix. No C/C++ code was removed when 3.9 support was dropped in 3.3.0 — the drop was purely a packaging metadata change. Combined with the safe finalization fix in the parent commit (PR python-greenlet#495), greenlet now works reliably on Python 3.9 during interpreter shutdown, which was the primary stability concern for older Python versions. Changes: - pyproject.toml: requires-python >= 3.9, add 3.9 trove classifier - .github/workflows/tests.yml: add "3.9" to test matrix, exclude windows-11-arm (not available for 3.9) - CHANGES.rst: add entries for both this change and the finalization fix Co-authored-by: Cursor <cursoragent@cursor.com>
Author
|
@jamadden please let me know if you have any questions but I think if this would fix the worst stability bug I am aware of, it could possibly make a lot of people happy 🙏 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
During interpreter finalization (
Py_FinalizeEx), active greenlets being deallocated triggerg_switch()to throwGreenletExit. This performs a stack switch and executes Python code in a partially-torn-down interpreter, causing:std::runtime_error: Accessing state after destruction) on greenlet 2.xOn Python >= 3.11, CPython's restructured finalization internals (frame representation, data stack management, recursion tracking) make
g_switch()during finalization safe — so no crash occurs. On Python < 3.11, this was not the case, leading to crashes during shutdown when active greenlets exist.Root Cause
We traced this to two unsafe operations during
Py_FinalizeEx:_green_dealloc_kill_started_non_main_greenletinPyGreenlet.cpp— callsg_switch()to throwGreenletExitinto the greenlet, which performs a stack switch and runs Python code via_PyEval_EvalFrameDefaultin a partially-torn-down interpreter.~ThreadStateinTThreadState.hpp— callsPyImport_ImportModule(\"gc\")for leak detection, which is unsafe when the import machinery is partially torn down.Fix
This PR adds two
_Py_IsFinalizing()guards, compiled only on Python < 3.11 (#if !GREENLET_PY311):In
_green_dealloc_kill_started_non_main_greenlet: When finalizing, callmurder_in_place()directly instead ofg_switch(). This marks the greenlet as dead without throwingGreenletExit— avoiding the crash at the cost of not running cleanup code inside the greenlet.In
~ThreadState: When finalizing, skip the GC-based leak detection and perform only minimal safe cleanup (clearing strong references to prevent leaks).On Python >= 3.11, no changes are made — the existing behavior (throwing
GreenletExitviag_switch(), running cleanup code) continues to work correctly during finalization.Behavioral Difference by Python Version
murder_in_place()— greenlet killed without cleanup (safe)g_switch()+GreenletExit— cleanup code runs normally (already safe)Tests
Adds
test_interpreter_shutdown.pywith 9 subprocess-based tests:GreenletExitcleanup runs on Python >= 3.11, but is correctly skipped (viamurder_in_place) on < 3.11Reproduction
This was discovered in a production environment running Python 3.9.7 + uWSGI + greenlet where worker recycling caused active greenlets to be cleaned up during interpreter shutdown, generating core dumps that filled disk space (P0 incident). A full reproduction environment and detailed analysis is available at: https://github.com/nbouvrette/scheduling-tools/tree/main/greenlet-tests
Motivation
This fix stabilizes greenlet for the many production deployments still running Python 3.9 and 3.10, where upgrading Python is not immediately feasible. It also closes a longstanding gap in the test suite by adding the first interpreter-shutdown tests.
Fixes #411
See also #351, #376