]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-140080: Clear `atexit` callbacks when memory allocation fails during finalization...
authoryihong <zouzou0208@gmail.com>
Wed, 15 Oct 2025 13:49:55 +0000 (21:49 +0800)
committerGitHub <noreply@github.com>
Wed, 15 Oct 2025 13:49:55 +0000 (09:49 -0400)
This fixes a regression introduced by GH-136004, in which finalization would hang while executing atexit handlers if the system was out of memory.

---------

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_atexit.py
Misc/NEWS.d/next/Core_and_Builtins/2025-10-14-20-18-31.gh-issue-140080.8ROjxW.rst [new file with mode: 0644]
Modules/atexitmodule.c

index 66142a108d5d9327466244bd2b4ba9dce402596e..8256ff183f28c9cedb33f0ea58062adba1da27ed 100644 (file)
@@ -1,9 +1,11 @@
 import atexit
 import os
+import subprocess
 import textwrap
 import unittest
 from test import support
-from test.support import script_helper
+from test.support import SuppressCrashReport, script_helper
+from test.support import os_helper
 from test.support import threading_helper
 
 class GeneralTest(unittest.TestCase):
@@ -189,6 +191,37 @@ class SubinterpreterTest(unittest.TestCase):
         self.assertEqual(os.read(r, len(expected)), expected)
         os.close(r)
 
+    # Python built with Py_TRACE_REFS fail with a fatal error in
+    # _PyRefchain_Trace() on memory allocation error.
+    @unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
+    def test_atexit_with_low_memory(self):
+        # gh-140080: Test that setting low memory after registering an atexit
+        # callback doesn't cause an infinite loop during finalization.
+        code = textwrap.dedent("""
+            import atexit
+            import _testcapi
+
+            def callback():
+                print("hello")
+
+            atexit.register(callback)
+            # Simulate low memory condition
+            _testcapi.set_nomemory(0)
+        """)
+
+        with os_helper.temp_dir() as temp_dir:
+            script = script_helper.make_script(temp_dir, 'test_atexit_script', code)
+            with SuppressCrashReport():
+                with script_helper.spawn_python(script,
+                                                stderr=subprocess.PIPE) as proc:
+                    proc.wait()
+                    stdout = proc.stdout.read()
+                    stderr = proc.stderr.read()
+
+        self.assertIn(proc.returncode, (0, 1))
+        self.assertNotIn(b"hello", stdout)
+        self.assertIn(b"MemoryError", stderr)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-14-20-18-31.gh-issue-140080.8ROjxW.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-14-20-18-31.gh-issue-140080.8ROjxW.rst
new file mode 100644 (file)
index 0000000..0ddcea5
--- /dev/null
@@ -0,0 +1 @@
+Fix hang during finalization when attempting to call :mod:`atexit` handlers under no memory.
index 4b068967a6ca6ea1408bf0f54408d638d61803b5..4536b03fbc4de947b35ed43192193557e9f029cd 100644 (file)
@@ -112,6 +112,7 @@ atexit_callfuncs(struct atexit_state *state)
     {
         PyErr_FormatUnraisable("Exception ignored while "
                                "copying atexit callbacks");
+        atexit_cleanup(state);
         return;
     }