Skip to content

Commit a05aece

Browse files
yihong0618ZeroIntensityvstinner
authored
gh-140080: Clear atexit callbacks when memory allocation fails during finalization (GH-140103)
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 <[email protected]> Co-authored-by: Peter Bierma <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 32c2649 commit a05aece

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

Lib/test/test_atexit.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import atexit
22
import os
3+
import subprocess
34
import textwrap
45
import unittest
56
from test import support
6-
from test.support import script_helper
7+
from test.support import SuppressCrashReport, script_helper
8+
from test.support import os_helper
79
from test.support import threading_helper
810

911
class GeneralTest(unittest.TestCase):
@@ -189,6 +191,37 @@ def callback():
189191
self.assertEqual(os.read(r, len(expected)), expected)
190192
os.close(r)
191193

194+
# Python built with Py_TRACE_REFS fail with a fatal error in
195+
# _PyRefchain_Trace() on memory allocation error.
196+
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
197+
def test_atexit_with_low_memory(self):
198+
# gh-140080: Test that setting low memory after registering an atexit
199+
# callback doesn't cause an infinite loop during finalization.
200+
code = textwrap.dedent("""
201+
import atexit
202+
import _testcapi
203+
204+
def callback():
205+
print("hello")
206+
207+
atexit.register(callback)
208+
# Simulate low memory condition
209+
_testcapi.set_nomemory(0)
210+
""")
211+
212+
with os_helper.temp_dir() as temp_dir:
213+
script = script_helper.make_script(temp_dir, 'test_atexit_script', code)
214+
with SuppressCrashReport():
215+
with script_helper.spawn_python(script,
216+
stderr=subprocess.PIPE) as proc:
217+
proc.wait()
218+
stdout = proc.stdout.read()
219+
stderr = proc.stderr.read()
220+
221+
self.assertIn(proc.returncode, (0, 1))
222+
self.assertNotIn(b"hello", stdout)
223+
self.assertIn(b"MemoryError", stderr)
224+
192225

193226
if __name__ == "__main__":
194227
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix hang during finalization when attempting to call :mod:`atexit` handlers under no memory.

Modules/atexitmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ atexit_callfuncs(struct atexit_state *state)
112112
{
113113
PyErr_FormatUnraisable("Exception ignored while "
114114
"copying atexit callbacks");
115+
atexit_cleanup(state);
115116
return;
116117
}
117118

0 commit comments

Comments
 (0)