Repository navigation
Deallocating highly nested list can cause stack overflow on Alpine Linux (musl) #151546
Copy link
Copy link
Closed
Closed
Copy link
Labels
OS-unsupportedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Example:
import threading
def run_thread(func, *args):
thread = threading.Thread(target=func, args=args)
thread.start()
thread.join()
def producer(results):
L = [None, None]
for i in range(5_000):
L = [i, L]
results.append(L)
def consumer(results):
L = results.pop()
L = None
threading.stack_size(1024*1024)
results = []
run_thread(producer, results)
run_thread(consumer, results)Example on Alpine Linux (Python linked to musl), Python uses a stack size of 1 MiB:
~/cpython # ./python bug.py
Segmentation fault (core dumped)
gdb traceback, _Py_MergeZeroLocalRefcount() calls _Py_MergeZeroLocalRefcount() in a loop:
Thread 3 "Thread-2 (consu" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 30901]
0x00005594970a5b41 in _Py_Dealloc (op=0x0) at Objects/object.c:3291
3291 {
(gdb) where
#0 0x00005594970a5b41 in _Py_Dealloc (op=0x0) at Objects/object.c:3291
#1 0x00005594970a054a in _Py_MergeZeroLocalRefcount (op=0x200041aa8d0) at Objects/object.c:444
#2 0x0000559497051010 in Py_DECREF (filename=0x55949745644c "./Include/refcount.h", lineno=520, op=0x200041aa8d0) at ./Include/refcount.h:359
#3 0x000055949705107d in Py_XDECREF (op=0x200041aa8d0) at ./Include/refcount.h:520
#4 0x0000559497054266 in list_dealloc (self=0x200041aa930) at Objects/listobject.c:567
#5 0x00005594970a5c35 in _Py_Dealloc (op=0x200041aa930) at Objects/object.c:3319
#6 0x00005594970a054a in _Py_MergeZeroLocalRefcount (op=0x200041aa930) at Objects/object.c:444
#7 0x0000559497051010 in Py_DECREF (filename=0x55949745644c "./Include/refcount.h", lineno=520, op=0x200041aa930) at ./Include/refcount.h:359
#8 0x000055949705107d in Py_XDECREF (op=0x200041aa930) at ./Include/refcount.h:520
#9 0x0000559497054266 in list_dealloc (self=0x200041aa990) at Objects/listobject.c:567
#10 0x00005594970a5c35 in _Py_Dealloc (op=0x200041aa990) at Objects/object.c:3319
#11 0x00005594970a054a in _Py_MergeZeroLocalRefcount (op=0x200041aa990) at Objects/object.c:444
#12 0x0000559497051010 in Py_DECREF (filename=0x55949745644c "./Include/refcount.h", lineno=520, op=0x200041aa990) at ./Include/refcount.h:359
#13 0x000055949705107d in Py_XDECREF (op=0x200041aa990) at ./Include/refcount.h:520
#14 0x0000559497054266 in list_dealloc (self=0x200041aa9f0) at Objects/listobject.c:567
#15 0x00005594970a5c35 in _Py_Dealloc (op=0x200041aa9f0) at Objects/object.c:3319
#16 0x00005594970a054a in _Py_MergeZeroLocalRefcount (op=0x200041aa9f0) at Objects/object.c:444
#17 0x0000559497051010 in Py_DECREF (filename=0x55949745644c "./Include/refcount.h", lineno=520, op=0x200041aa9f0) at ./Include/refcount.h:359
#18 0x000055949705107d in Py_XDECREF (op=0x200041aa9f0) at ./Include/refcount.h:520
#19 0x0000559497054266 in list_dealloc (self=0x200041aaa50) at Objects/listobject.c:567
#20 0x00005594970a5c35 in _Py_Dealloc (op=0x200041aaa50) at Objects/object.c:3319
#21 0x00005594970a054a in _Py_MergeZeroLocalRefcount (op=0x200041aaa50) at Objects/object.c:444
#22 0x0000559497051010 in Py_DECREF (filename=0x55949745644c "./Include/refcount.h", lineno=520, op=0x200041aaa50) at ./Include/refcount.h:359
#23 0x000055949705107d in Py_XDECREF (op=0x200041aaa50) at ./Include/refcount.h:520
#24 0x0000559497054266 in list_dealloc (self=0x200041aaab0) at Objects/listobject.c:567
#25 0x00005594970a5c35 in _Py_Dealloc (op=0x200041aaab0) at Objects/object.c:3319
#26 0x00005594970a054a in _Py_MergeZeroLocalRefcount (op=0x200041aaab0) at Objects/object.c:444
...
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
OS-unsupportedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
vstinner commentedon Jun 16, 2026
The issue can be reproduced on Linux with Python linked to the glibc.
Apply the following patch:
And build Python with
./configure --with-pydebug --disable-gil CFLAGS="-O0" LDFLAGS="-Wl,-z,stack-size=1048576".Example:
vstinner commentedon Jun 16, 2026
_Py_Dealloc()has a generic mechanism to avoid stack overflow:_Py_RecursionLimit_GetMargin()relies ontstate->c_stack_soft_limitwhich is set by_Py_InitializeRecursionLimits().If Python is built with the glibc,
pthread_getattr_np()is used to getpthread_attr_getguardsize()andpthread_attr_getstack(). But when Python is built with musl, a fallback is used and it seems like the fallback doesn't compute stack limits properly. MyPython/ceval.cpatch above disables glibc code path to also use the fallback on glibc.[-]Deallocating highly nested list can cause stack overflow on Free Threading[/-][+]Deallocating highly nested list can cause stack overflow on Alpine Linux (musl)[/+]vstinner commentedon Jun 16, 2026
Ah, I can also reproduce the bug without Free Threading. I updated the issue title to mention musl instead.
vstinner commentedon Jun 16, 2026
gdb traceback on a default Python (with GIL),
list_dealloc()callslist_dealloc()in a loop:vstinner commentedon Jun 16, 2026
The reproducer comes from
test_concurrent_tee_supports_concurrent_consumers()oftest_threading: see issue gh-151542.pythongh-151546: Fix thread stack size on musl
pythongh-151546: Fix stack limits on musl
gh-151546: Fix stack limits on musl (#151548)
[3.15] gh-151546: Fix stack limits on musl (#151548) (#151583)
[3.14] gh-151546: Fix stack limits on musl (#151548) (#151583) (#151591)
pythongh-151546: Fix stack limits on musl (python#151548)