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 <zouzou0208@gmail.com> Co-authored-by: Peter Bierma <zintensitydev@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 32c2649 commit a05aece

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎Lib/test/test_atexit.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
importatexit
22
importos
3+
importsubprocess
34
importtextwrap
45
importunittest
56
fromtestimportsupport
6-
fromtest.supportimportscript_helper
7+
fromtest.supportimportSuppressCrashReport, script_helper
8+
fromtest.supportimportos_helper
79
fromtest.supportimportthreading_helper
810

911
classGeneralTest(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+
deftest_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+
withos_helper.temp_dir() astemp_dir:
213+
script=script_helper.make_script(temp_dir, 'test_atexit_script', code)
214+
withSuppressCrashReport():
215+
withscript_helper.spawn_python(script,
216+
stderr=subprocess.PIPE) asproc:
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)