From d5440012406669f2d8b7eca8658157ffb66995a2 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Thu, 21 Aug 2025 22:26:58 +0530 Subject: [PATCH 1/4] fix warnings finalization bug --- Python/pystate.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index 9091057f6f62cf..2465d8667472dc 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -805,7 +805,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 @@ -815,6 +814,10 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) /* Last garbage collection on this interpreter */ _PyGC_CollectNoFail(tstate); _PyGC_Fini(interp); + + // Finalize warnings after last gc so that any finalizers can + // access warnings state + _PyWarnings_Fini(interp); struct _PyExecutorObject *cold = interp->cold_executor; if (cold != NULL) { interp->cold_executor = NULL; From bfb7d5d21455e4d072a6492f78b126768cfd7905 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 22 Aug 2025 17:06:29 +0530 Subject: [PATCH 2/4] add a test --- Lib/test/test_gc.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py index 7c9adf3049a131..4328909053465e 100644 --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -1580,6 +1580,19 @@ def test_ast_fini(self): """) 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 From fc0f013e72908c27d91fd0fa0f1032580e099374 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 22 Aug 2025 11:39:47 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst new file mode 100644 index 00000000000000..462bf5bde1a9f9 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst @@ -0,0 +1 @@ +Fix a crash when :mod:`warnings` state was finalised too early. Patch by Kumar Aditya. From 80d0ff8208e07699b659ee5e63ed94fe63a048b1 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 22 Aug 2025 18:43:44 +0530 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst Co-authored-by: Peter Bierma --- .../2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst index 462bf5bde1a9f9..583d751a460eb6 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-22-11-39-40.gh-issue-137384.j4b_in.rst @@ -1 +1 @@ -Fix a crash when :mod:`warnings` state was finalised too early. Patch by Kumar Aditya. +Fix a crash when using the :mod:`warnings` module in a finalizer at shutdown. Patch by Kumar Aditya.