Repository navigation
Closed
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifeOS-unsupportedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
Description
| BPO | 32307 |
|---|---|
| Nosy | @gpshead, @bitdancer |
| PRs |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/gpshead'
closed_at = None
created_at = <Date 2017-12-13.17:46:34.284>
labels = ['interpreter-core', '3.10', '3.9', 'type-crash', '3.11']
title = 'Bad assumption on thread stack size makes python crash with musl libc'
updated_at = <Date 2021-10-04.22:00:28.959>
user = 'https://bugs.python.org/NatanaelCopa'bugs.python.org fields:
activity = <Date 2021-10-04.22:00:28.959>
actor = 'gregory.p.smith'
assignee = 'gregory.p.smith'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2017-12-13.17:46:34.284>
creator = 'Natanael Copa'
dependencies = []
files = []
hgrepos = []
issue_num = 32307
keywords = ['patch']
message_count = 8.0
messages = ['308226', '308228', '308293', '308294', '308304', '308305', '308307', '403184']
nosy_count = 5.0
nosy_names = ['gregory.p.smith', 'r.david.murray', 'python-dev', 'Natanael Copa', '4-launchpad-kalvdans-no-ip-org']
pr_nums = ['26224']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue32307'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifeOS-unsupportedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
Projects
- StatusShow more project fieldsDone
- StatusShow more project fieldsDone
Activity
NatanaelCopa commentedon Dec 13, 2017
Python assumes that the system default thread stack size is big enough for python, except for OSX and FreeBSD where stack size is explicitly set. With musl libc the system thread stack size is only 80k, which leads to hard crash before
sys.getrecursionlimit()is hit.See: https://github.com/python/cpython/blob/master/Python/thread_pthread.h#L22
Testcase:
This can be pasted into:
docker run --rm -it python:2.7-alpine
docker run --rm -it python:3.6-alpine
bitdancer commentedon Dec 13, 2017
Well, from our point of view it isn't a bad assumption, it's that muslc needs to be added to the list of exceptions. (I know almost nothing about this...I assume there is some reason we can't determine the stack size programatically?)
Would you care to propose a PR?
NatanaelCopa commentedon Dec 14, 2017
With "bad assumption" I mean that currently the code assumes that all unknown systems has big enough default thread stack size. This would not be a "bad assumption" if POSIX (or any other standard) would specify a requirement on the default stack size. To my understanding, there are no such requirement in the spec, and thus, applications can not really assume anything about the default thread stack size.
musl libc does not provide any __MUSL__ macro by design, because they consider "it’s a bug to assume a certain implementation has particular properties rather than testing" so we cannot really make exception for musl.
https://wiki.musl-libc.org/faq.html#Q:_Why_is_there_no_\<code>MUSL</code>_macro?
I think the options we have are:
I can of course make a check for musl in the configure script and special case it, but it still does not fix the issue: There is no guarantee that stack size is big enough for python's use for every existing and unknown future system.
NatanaelCopa commentedon Dec 14, 2017
I ran a test of pthread_attr_getstacksize() on various systems. Here is what they return:
Linux/glibc: 8388608
MacOS: 524288
FreeBSD 11.1: 0 (ulimit -s gives 524288)
NetBSD 7.1: 4194304
OpenBSD 6.2: 524288
I could not see any #ifdef for __OpenBSD__ so I would expect to see segfaults on OpenBSD as well. Google gave me this:
https://bugs.python.org/issue25342
alonho/pytrace#8
NatanaelCopa commentedon Dec 14, 2017
I suggest a runtime check like this:
The reasoning here is:
Scripts that uses threading.stack_size() will still work as before, and you can still manually set the stack size down to 32k (THREAD_STACK_MIN) if you need that.
This should keep existing behavior for known systems like glibc, OSX, FreeBSD, NetBSD but will raise the thread stack size for musl libc and OpenBSD to 1MB.
What do you think?
bitdancer commentedon Dec 14, 2017
I think we need people who do a lot of work at the C level to evaluate this, so I've added a couple to the nosy list :)
NatanaelCopa commentedon Dec 14, 2017
Exactly same as previous patch, but refactored to only have a single pthread_attr_destroy() call instead of 3.
gpshead commentedon Oct 4, 2021
I left some comments on the PR. We can at least make it detect musl libc and improve its default behavior there.
FWIW CPython's recursionlimit and the C stack have long been a thorn in the side of many things.
Even when Python's thread_pthread.h is changed to set a better default when running under musl libc, there are still plenty of ways to run into problems. ex: Extension modules can create threads on their own that then call back into CPython. So can C/C++ code that embeds a Python interpreter. Those choose their own stack sizes. When they choose low values, surprising crashes ensue for people working on the Python side...
As a feature (beyond just this issue): It'd be ideal to actually be able to introspect the C stack space remaining and issue a RecursionError whenever it falls below a threshold. Rather than just blindly using a runtime adjustable number that any Python code can tweak to allow crashes via
sys.setrecursionlimit(). Other languages like Golang use a dynamic stack and allocate more on SIGSEGV.[meta/off-topic: Why does anyone use small thread stack sizes? This is often done to save virtual address space in applications that expect to have thousands of threads. It matters a lot less on 64-bit address space. But there are reasons...]
vstinner commentedon Jun 18, 2026
Hi. I modified Python build system to increase the stack size from 128 kiB to 1 MiB and adapted runtime stack size checks.
The reproducer script does no longer crash, but fail with an exception as expected:
I close the issue. Thanks for your bug report.