Restore Python 3.9 support#496
Open
nbouvrette wants to merge 2 commits intopython-greenlet:masterfrom
Open
Conversation
094f77a to
317415d
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>
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>
317415d to
fc0ec82
Compare
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
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
This PR restores Python 3.9 support that was dropped in greenlet 3.3.0.
Context
When Python 3.9 was dropped in 3.3.0, no C/C++ code was actually removed — the change was purely in packaging metadata (
requires-pythonand trove classifiers inpyproject.toml) and CI configuration. Thetox.initest matrix already includespy39. This means restoring 3.9 is a minimal, low-risk change.Why Restore 3.9?
Python 3.9 remains widely deployed in production environments. Many teams (including ours) run mission-critical services on Python 3.9 with greenlet-dependent stacks (uWSGI, gevent, SQLAlchemy) and cannot immediately upgrade Python.
With the safe finalization fix in PR #495, the primary stability concern for Python 3.9 (SIGSEGV/SIGABRT during interpreter shutdown) is now resolved. Restoring 3.9 support gives these teams a stable greenlet release path while they plan their Python version upgrade.
Changes
pyproject.toml:requires-python:">=3.10"→">=3.9""Programming Language :: Python :: 3.9"trove classifier.github/workflows/tests.yml:"3.9"to the CI test matrixwindows-11-armexclusion for 3.9 (not available on that platform)CHANGES.rst: Added entries for both this change and the finalization fix from PR Fix SIGSEGV/SIGABRT during interpreter shutdown on Python < 3.11 #495.Dependencies
This PR builds on top of PR #495 (safe finalization fix). Without that fix, Python 3.9 would still crash during interpreter shutdown when active greenlets exist. With both PRs merged, Python 3.9 is fully stable.
Test Plan
tox -e py39passes (already in the tox config)