Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

bash: fork: retry: Resource temporarily unavailable

fork returned EAGAIN because a process or thread ceiling was hit. Identify whether nproc, cgroup pids, TasksMax or threads-max is the limit.

~4 min readLinux
#Linux#Processes#Limits#Troubleshooting
A shell spawning process cubes that pile against a cyan ceiling rail, an amber limit dial below

Introduction & Quick Summary

text
bash: fork: retry: Resource temporarily unavailable bash: fork: retry: Resource temporarily unavailable bash: fork: Resource temporarily unavailable

fork() returned EAGAIN, and bash retries before giving up. The system has a hard ceiling on processes or threads for your user, your cgroup, or the kernel, and it has been reached. Memory usually is not the problem, even though the second variant says Cannot allocate memory.

Four ceilings produce this exact message, and you have to identify which one you hit:

  • RLIMIT_NPROC (ulimit -u) for the user account, counted per UID across all sessions.
  • cgroup pids.max in containers and systemd slices, which counts processes and threads together.
  • kernel.threads-max and kernel.pid_max, the machine-wide limits.
  • vm.max_map_count or address space, which give the Cannot allocate memory wording with the same visible symptom.
Where you see itLikely limitCheck command
Shell in a containercgroup pids.maxcat /sys/fs/cgroup/pids.max
Interactive SSH sessionulimit -uulimit -u, ulimit -Hu
Service under systemdTasksMaxsystemctl show -p TasksMax <unit>
Entire host saturatedthreads-maxsysctl kernel.threads-max
CI job after many parallel stepsPer-user nprocps -eLf --no-headers | wc -l

Core Mechanics, Signatures & Reproducible Setup

fork() fails with EAGAIN when the kernel cannot allocate a task structure, when the calling user already has RLIMIT_NPROC tasks, or when the cgroup's pids.max would be exceeded. Since Linux 2.6.23 threads are tasks too, so a JVM or Node process with hundreds of threads consumes hundreds of slots from the same counter. That is why the trigger is often an application, not a runaway shell loop.

Read every limit at once:

bash
ulimit -u # soft nproc ulimit -Hu # hard nproc cat /proc/sys/kernel/threads-max cat /proc/sys/kernel/pid_max cat /sys/fs/cgroup/pids.max 2>/dev/null || true cat /sys/fs/cgroup/pids.current 2>/dev/null || true systemctl show -p TasksMax -p TasksCurrent user.slice

Reproduce it in a shell without harming the host:

bash
bash -c 'ulimit -u 40; for i in $(seq 1 200); do sleep 30 & done; wait' # bash: fork: retry: Resource temporarily unavailable

Count what is actually consuming tasks:

bash
ps -eLf --no-headers | wc -l # total threads ps -eLf --no-headers | awk '{print $1}' | sort | uniq -c | sort -rn | head ps -eo user:20,nlwp,pid,comm --sort=-nlwp | head -15

The last command is the fastest way to find a single process holding thousands of threads, which is common with misconfigured connection pools and thread-per-request servers.

Key Comparison Table

LimitScopeCounts threadsRaised at runtimePersistence
ulimit -u (soft)UserYesulimit -u NShell only
ulimit -Hu (hard)UserYesRoot only/etc/security/limits.conf
cgroup pids.maxContainer or sliceYessystemctl set-propertyUnit drop-in
TasksMaxsystemd unitYessystemctl set-property --runtimeDrop-in file
kernel.threads-maxHostYessysctl -w/etc/sysctl.d
kernel.pid_maxHostPIDs onlysysctl -w/etc/sysctl.d
EnvironmentDefault ceilingWhere to change
Docker containerInherits host, often unlimited--pids-limit, Compose pids
Kubernetes podNode allocatable, no pod limit by default--max-pods, kubelet podPidsLimit
systemd serviceDefaultTasksMax (15% of threads-max)TasksMax= in unit
SSH login shellnproc from limits.conf/etc/security/limits.d/
GitHub Actions runnerHost nprocNot configurable

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: A container with --pids-limit set

docker run --rm --pids-limit=100 alpine sh -c \ 'for i in $(seq 1 200); do sleep 30 & done; wait' # sh: can't fork: Resource temporarily unavailable

Raise the limit or, better, find the leak. A JVM started without -XX:ActiveProcessorCount and -Xss tuning can spawn more threads than a small pids.max allows:

services: api: image: app:latest pids_limit: 512

Case 2: systemd service hitting TasksMax

bash
systemctl show -p TasksMax -p TasksCurrent myservice.service sudo systemctl edit myservice.service
ini
[Service] TasksMax=4096

DefaultTasksMax on modern systemd is 15% of kernel.threads-max, so a busy machine effectively gives each service a moving target. Set it explicitly for servers that manage their own thread pools.

Case 3: The nproc limit that does not match limits.conf

PAM applies limits at login only if pam_limits.so is configured for the service. A unit started by systemd does not read limits.conf; it reads LimitNPROC in the unit. This is the single most common reason a documented ulimit change has no effect:

ini
[Service] LimitNPROC=65535

Case 4: Thread leaks in Java and Node

bash
jcmd 1 Thread.print | grep -c '"' # thread count from a JVM node -e "console.log(process._getActiveHandles().length)"

A JVM at its thread ceiling usually reports java.lang.OutOfMemoryError: unable to create native thread in logs at the same time as the shell error. The fix is pool sizing, not a bigger limit.

Case 5: Zombie processes and Cannot allocate memory

A parent that never calls wait() leaves zombies, which hold PID slots but no memory.

bash
ps -eo stat,ppid,pid,comm | awk '$1 ~ /^Z/ {print}' | head

Zombies disappear when the parent exits or reaps. A container whose PID 1 is a shell script often accumulates them; use exec or tini so signals and reaping work.

Case 6: The host-wide ceiling during a fork storm

bash
sysctl kernel.threads-max kernel.pid_max vm.max_map_count grep -c ^processor /proc/cpuinfo

If ps -eLf | wc -l is near threads-max, no per-user change helps. Find the offender and cap it, then raise threads-max only as a stopgap.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Cannot allocate memory is misleading. It appears for EAGAIN, not only ENOMEM; bash uses the same wording for both. Do not start by adding RAM.
  • ulimit -u in a container shows the host value. cgroup v2 pids.max can still be the lower ceiling, so check both.
  • RLIMIT_NPROC is per UID, not per process tree. Two services running as the same user share one budget, which is why moving a service to its own user often fixes it.
  • cgroup v1 versus v2 paths. pids.max lives at /sys/fs/cgroup/pids/pids.max on v1 and /sys/fs/cgroup/<path>/pids.max on v2; a script that reads only one path silently reports nothing.
  • LimitNPROC has no effect on root. Root bypasses RLIMIT_NPROC in many kernels, so a root shell appears unlimited while application users are constrained.
  • Fork bombs from build tools. Parallel make -j, jest --maxWorkers=100%, or xargs -P on a 64-core host can exceed a per-user limit intentionally set for interactive use. Cap parallelism rather than raising limits.

When the failing command is wrapped by a script that hides the real error text, log the full stderr and compare a working and failing run with the diff tool. If the limit is being applied by a container runtime, the Docker Compose viewer shows whether pids_limit or deploy.resources is set where you think it is. For confirming the exact numeric ceilings configured on a host, a small cron entry checked through the cron expression tool can sample ps -eLf | wc -l before an incident rather than after.

Primary references: fork(2) lists EAGAIN and its causes, and the kernel sysctl documentation covers threads-max and pid_max.

Official references

primary documentation

The Linux documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Linux guides

More in Linux

All Linux guides

The guides filed next to this one in Linux, in library order.