Backport: Fix crash in getcurrent()/greenlet during early Py_FinalizeEx (3.2.6)#500
Open
nbouvrette wants to merge 2 commits intopython-greenlet:maint/3.2from
Open
Conversation
Ports all crash fixes from the main branch (PR python-greenlet#499) to maint/3.2 for a 3.2.6 release targeting Python 3.9 stability. Three root causes of SIGSEGV during Py_FinalizeEx on Python < 3.11: 1. clear_deleteme_list() vector allocation crash: replaced copy with std::swap and switched deleteme_t to std::allocator (system malloc). 2. ThreadState memory corruption: switched from PythonAllocator (PyObject_Malloc) to std::malloc/std::free. 3. getcurrent() crash on invalidated type objects: added atexit handler that sets g_greenlet_shutting_down before _Py_IsFinalizing() is set. Also fixes exception preservation in clear_deleteme_list(), adds Py_IsFinalizing() compat shim for Python < 3.13, Windows USS tolerance for flaky memory test, and additional shutdown tests. Made-with: Cursor
6 tasks
The SPDX identifier "Python-2.0" refers to the Python 2.0 license, which is not GPL-compatible. The correct identifier for the PSF license used by greenlet is "PSF-2.0", which is GPL-compatible. Backport of 77e65f0 from master. Made-with: Cursor
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
Backports all crash fixes from PR #499 (targeting
mainfor 3.3.3) to themaint/3.2branch for a 3.2.6 release, providing stability fixes for Python 3.9 users.While regression-testing greenlet 3.2.5 (the backport of PR #495) against Python 3.9.7 under uWSGI, we discovered three additional crash paths during
Py_FinalizeExthat were not fixed by PR #495:clear_deleteme_list()allocator crash: The vector copy usedPythonAllocator(PyMem_Malloc), which can SIGSEGV during earlyPy_FinalizeExwhen Python's allocator pools are partially torn down. Replaced withstd::swap(zero-allocation) and switcheddeleteme_ttostd::allocator.ThreadStatememory corruption:ThreadStateobjects were allocated viaPyObject_Malloc, placing them inpymallocpools that can be disrupted during finalization. Switched tostd::malloc/std::free.getcurrent()crash on invalidated type objects:_Py_IsFinalizing()is only set aftercall_py_exitfuncscompletes insidePy_FinalizeEx, so atexit handlers could still callgreenlet.getcurrent()when type objects had been invalidated. An atexit handler now sets a shutdown flag before any other cleanup runs.Exception preservation:
clear_deleteme_list()now preserves pending exceptions around its cleanup loop.All new code is guarded by
#if !GREENLET_PY311— zero impact on Python 3.11+.Additional changes
Py_IsFinalizing()compat shim: Maps the public API name (available since Python 3.13) to_Py_IsFinalizing()on older versions. All call sites now use the standard name; remove the two-line shim when < 3.13 is dropped.test_interpreter_shutdown.pytests covering atexit-registered greenlet construction and cross-thread cleanup.Test plan
test_interpreter_shutdown.pytests pass3.2.6.dev0)Relationship to PR #499
This is a manual port (not cherry-pick) of PR #499's crash fixes, adapted to the
maint/3.2code structure which differs frommainin several ways (no free-threading support,setup.pyvspyproject.toml, different allocator layout, etc.). The crash fix logic is identical.Made with Cursor