[3.14] gh-137384: fix crash when accessing warnings state late in runtime shutdown (GH-138027) (#138065)
Some checks failed
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Check if the ABI has changed (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
Tail calling interpreter / aarch64-apple-darwin/clang (push) Has been cancelled
Tail calling interpreter / aarch64-unknown-linux-gnu/gcc (push) Has been cancelled
Tail calling interpreter / x86_64-pc-windows-msvc/msvc (push) Has been cancelled
Tail calling interpreter / x86_64-apple-darwin/clang (push) Has been cancelled
Tail calling interpreter / free-threading (push) Has been cancelled
Tail calling interpreter / x86_64-unknown-linux-gnu/gcc (push) Has been cancelled

This commit is contained in:
Kumar Aditya 2025-08-22 19:57:49 +05:30 committed by GitHub
parent c213eb90a0
commit d619015ecd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 1 deletions

View file

@ -1522,6 +1522,19 @@ class PythonFinalizationTests(unittest.TestCase):
""")
assert_python_ok("-c", code)
def test_warnings_fini(self):
# See https://github.com/python/cpython/issues/137384
code = textwrap.dedent('''
import asyncio
from contextvars import ContextVar
context_loop = ContextVar("context_loop", default=None)
loop = asyncio.new_event_loop()
context_loop.set(loop)
''')
assert_python_ok("-c", code)
def setUpModule():
global enabled, debug

View file

@ -0,0 +1 @@
Fix a crash when using the :mod:`warnings` module in a finalizer at shutdown. Patch by Kumar Aditya.

View file

@ -914,7 +914,6 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
_Py_ClearExecutorDeletionList(interp);
#endif
_PyAST_Fini(interp);
_PyWarnings_Fini(interp);
_PyAtExit_Fini(interp);
// All Python types must be destroyed before the last GC collection. Python
@ -925,6 +924,9 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
_PyGC_CollectNoFail(tstate);
_PyGC_Fini(interp);
// Finalize warnings after last gc so that any finalizers can
// access warnings state
_PyWarnings_Fini(interp);
/* We don't clear sysdict and builtins until the end of this function.
Because clearing other attributes can execute arbitrary Python code
which requires sysdict and builtins. */