Skip to content

thread: running out of kernel stacks fails clone and fork with -EAGAIN - #399

Merged
daniel-rossier merged 1 commit into
mainfrom
fix/thread-limit
Dec 2, 2026
Merged

daniel-rossier merged 1 commit into
mainfrom
fix/thread-limit

Conversation

@daniel-rossier

Copy link
Copy Markdown
Contributor
  • thread_create() returns NULL when the CONFIG_MAX_THREADS kernel stack slots are all taken, instead of panicking.
  • clone() of a thread returns -EAGAIN (pthread_create reports EAGAIN); fork() checks for a slot before duplicating the process and returns -EAGAIN.
  • Kernel threads keep the panic: the kernel cannot do without them.

Tested on QEMU virt64: a program creating sleeping pthreads in a loop now prints failed at 28: 11 and a following fork() returns -1, and the shell keeps working; before, the kernel panicked at the 29th thread.

@daniel-rossier
daniel-rossier force-pushed the fix/thread-limit branch 3 times, most recently from 614313b to 7b01c66 Compare December 2, 2026 09:02
Every thread takes one of the CONFIG_MAX_THREADS kernel stack slots, a
system-wide pool. When it was empty, thread_create() panicked, so any
user program creating threads or forking in a loop brought the whole
kernel down after about 30 of them.

thread_create() now returns NULL when no slot is free. clone() of a
thread then returns -EAGAIN, which musl's pthread_create reports, and
fork() checks for a free slot before it duplicates anything, so it
fails with -EAGAIN instead of leaving a half-built child. A kernel
thread still panics: the kernel has no way to do without it.
@daniel-rossier
daniel-rossier merged commit d871e01 into main Dec 2, 2026
6 checks passed
@daniel-rossier
daniel-rossier deleted the fix/thread-limit branch December 2, 2026 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant