Skip to content

Commit b15d04c

Browse files
committed
fix JIT tracer memory leak when daemon thread exits
1 parent 76b484b commit b15d04c

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

Lib/test/test_capi/test_opt.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3792,6 +3792,29 @@ def __next__(self):
37923792
"""), PYTHON_JIT="1", PYTHON_JIT_STRESS="1")
37933793
self.assertEqual(result[0].rc, 0, result)
37943794

3795+
def test_144068_daemon_thread_jit_cleanup(self):
3796+
result = script_helper.run_python_until_end('-c', textwrap.dedent("""
3797+
import threading
3798+
import time
3799+
3800+
def hot_loop():
3801+
end = time.time() + 5.0
3802+
while time.time() < end:
3803+
pass
3804+
3805+
# Create a daemon thread that will be abandoned at shutdown
3806+
t = threading.Thread(target=hot_loop, daemon=True)
3807+
t.start()
3808+
3809+
time.sleep(0.1)
3810+
"""), PYTHON_JIT="1", ASAN_OPTIONS="detect_leaks=1")
3811+
self.assertEqual(result[0].rc, 0, result)
3812+
stderr = result[0].err.decode('utf-8', errors='replace')
3813+
self.assertNotIn('LeakSanitizer', stderr,
3814+
f"Memory leak detected by ASan:\n{stderr}")
3815+
self.assertNotIn('_PyJit_TryInitializeTracing', stderr,
3816+
f"JIT tracer memory leak detected:\n{stderr}")
3817+
37953818
def global_identity(x):
37963819
return x
37973820

Python/pystate.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,10 @@ PyThreadState_Clear(PyThreadState *tstate)
18361836

18371837
_PyThreadState_ClearMimallocHeaps(tstate);
18381838

1839+
#ifdef _Py_TIER2
1840+
_PyJit_TracerFree((_PyThreadStateImpl *)tstate);
1841+
#endif
1842+
18391843
tstate->_status.cleared = 1;
18401844

18411845
// XXX Call _PyThreadStateSwap(runtime, NULL) here if "current".

0 commit comments

Comments
 (0)