Package: python3.15
Version: 3.15.2-1
Severity: normal
As part of debugging a FTBFS in the qasync package (see #1152576), the
following bug came to light.
qasync's test suite has a test that runs unbounded recursion in 5 worker
threads and expects a RecursionError from each:
def rec(a, *args, **kwargs):
rec(a, *args, **kwargs)
fs = [executor.submit(rec, 1) for _ in range(10)]
for f in fs:
with pytest.raises(RecursionError):
f.result()
(tests/test_qthreadexec.py::test_stack_recursion_limit; the workers are
Qt QThreads, see src/qasync/__init__.py, QThreadExecutor.)
Observed, in a clean pbuilder chroot (unstable, amd64, default recursion
limit 1000, default 8 MiB stack rlimit):
- Python 3.14.10: the test passes in 0.10 s. The first worker raises
RecursionError immediately and then takes the remaining queued jobs.
- Python 3.15.2: all 5 workers pick up a job and start rec(); none of them
ever reports a result. The pytest process then gets "Killed" (exit 137,
presumably the OOM killer; I have not captured the kernel log). Not a
SIGSEGV or SIGBUS, so it does not look like a stack overflow.
Expected: RecursionError at about sys.getrecursionlimit() frames in each
thread, as with 3.14.
The tests in the same file that run before this one pass on 3.15, and all
of tests/test_qeventloop.py passes on 3.15 once qasync is patched for the
asyncio selector-transport "context" change, so this appears specific to
the recursion test.
Standalone reproducer (no Qt): the attached repro-recursion-threads.py
starts N plain threading.Thread workers running the same rec(). It caps
its own address space (2 GiB by default) so a runaway ends in MemoryError
rather than an OOM kill.
Standalone reproducer (no Qt): the attached repro-recursion-threads.py
starts N plain threading.Thread workers running the same rec(). It caps
its own address space (2 GiB by default) so a runaway ends in MemoryError
rather than an OOM kill.
python3.15 repro-recursion-threads.py 5
python3.14 repro-recursion-threads.py 5 # for comparison
Result of the standalone reproducer (in a clean, up-to-date pbuilder chroot):
root@nevermir:~# python3.14 mre.py
3.14.10 recursion limit 1000 thread stack default
OK worker-0: RecursionError after 996 frames
OK worker-1: RecursionError after 996 frames
OK worker-2: RecursionError after 996 frames
OK worker-3: RecursionError after 996 frames
OK worker-4: RecursionError after 996 frames
done in 0.00s, maxrss 11 MiB
root@nevermir:~# python3.15 mre.py
3.15.2 recursion limit 1000 thread stack default
watchdog: depths [697269, 963321, 995395, 1091276, 1242967]
FAIL worker-1: MemoryError after 1615783 frames (limit 1000)
watchdog: depths [1328656, 1538676, 1615783, 1747936, 1783352]
FAIL worker-0: MemoryError after 2161008 frames (limit 1000)
watchdog: depths [1615783, 1905112, 2129161, 2161008, 2268980]
FAIL worker-2: MemoryError after 2787706 frames (limit 1000)
watchdog: depths [1615783, 2161008, 2438160, 2787706, 3031899]
watchdog: depths [1615783, 2161008, 2787706, 3700887, 4305635]
FAIL worker-3: MemoryError after 4305635 frames (limit 1000)
watchdog: depths [1615783, 2161008, 2787706, 4305635, 8006611]
FAIL worker-4: MemoryError after 8006611 frames (limit 1000)
done in 16.09s, maxrss 1680 MiB
root@nevermir:~#
Context:
- Debian bug: https://bugs.debian.org/1152576 (qasync FTBFS with 3.15)
- Upstream qasync: https://github.com/CabbageDevelopment/qasync/issues/180
- Workaround in the qasync package: skip test_stack_recursion_limit on 3.15.
I have not seen anything related to RecursionError in the Python 3.15 release
notes, but if the bug really is in qasync then I will take it up with qasync
upstream.
Thanks,
Roland.
(Disclosure: the investigations and the reproducer script were generated with
help from an AI agent, but reviewed and confirmed by hand)
-- System Information:
Debian Release: 13.7
APT prefers stable-updates
APT policy: (500, 'stable-updates'), (500, 'stable-security'), (500, 'stable')
Architecture: amd64 (x86_64)
Kernel: Linux 6.12.118+deb13-amd64 (SMP w/16 CPU threads; PREEMPT)
Locale: LANG=fr_FR.UTF-8, LC_CTYPE=fr_FR.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled
#!/usr/bin/python3
"""Reproducer: RecursionError not raised in worker threads on Python 3.15.
Extracted from qasync's tests/test_qthreadexec.py::test_stack_recursion_limit,
which passes on Python <= 3.14 and gets SIGKILLed (OOM) on 3.15.2.
Each thread calls a function that recurses forever through *args/**kwargs.
Expected: every thread raises RecursionError at about sys.getrecursionlimit()
frames. Seen on 3.15 (via qasync's QThread-based executor): the threads never
finish and memory grows until the OOM killer strikes.
To avoid taking the machine down, the address space is capped (default 2 GiB,
override with the MEM_MIB environment variable); a runaway then ends in
MemoryError instead of a SIGKILL. A watchdog also reports the depth reached.
Usage: python3 repro-recursion-threads.py [NUM_THREADS] [--main]
"""
import os
import resource
import sys
import threading
import time
mem = int(os.environ.get("MEM_MIB", "2048")) * 1024 * 1024
resource.setrlimit(resource.RLIMIT_AS, (mem, mem))
depth = {}
def rec(a, *args, **kwargs):
depth[threading.get_ident()] = depth.get(threading.get_ident(), 0) + 1
rec(a, *args, **kwargs)
def work():
tid = threading.get_ident()
depth[tid] = 0
try:
rec(1)
except RecursionError:
print(f"OK {threading.current_thread().name}: RecursionError "
f"after {depth[tid]} frames", flush=True)
except MemoryError:
print(f"FAIL {threading.current_thread().name}: MemoryError after "
f"{depth[tid]} frames (limit {sys.getrecursionlimit()})",
flush=True)
def watchdog():
while True:
time.sleep(2)
print("watchdog: depths", sorted(depth.values()), flush=True)
print(sys.version.split()[0], "recursion limit", sys.getrecursionlimit(),
"thread stack", threading.stack_size() or "default", flush=True)
args = [a for a in sys.argv[1:] if not a.startswith("--")]
n = int(args[0]) if args else 5
threading.Thread(target=watchdog, daemon=True).start()
t0 = time.time()
if "--main" in sys.argv:
work()
else:
ts = [threading.Thread(target=work, name=f"worker-{i}") for i in range(n)]
for t in ts:
t.start()
for t in ts:
t.join()
print(f"done in {time.time() - t0:.2f}s, "
f"maxrss {resource.getrusage(resource.RUSAGE_SELF).ru_maxrss // 1024}
MiB")