Skip to content

An open-source, faster Devin alternative

TypeScript

set max limit 4096, zombie reaper

Brian Reardon7ca8992453f1
diff --git a/cmd/agent/main.go b/cmd/agent/main.go
index 13de991c..bdf90c31 100644
--- a/cmd/agent/main.go
+++ b/cmd/agent/main.go
@@ -43,6 +43,14 @@ func main() {
srv.ListenPort = listenPortForPTY
}
+ // SIGCHLD reaper: when osb-agent runs as PID 1, every orphaned process
+ // in the VM is re-parented to it. If we don't wait4() them they stay as
+ // zombies, occupying a slot in the sandbox cgroup's pids.max budget.
+ // Within hours of a build/test loop the customer hits "fork failed:
+ // resource temporarily unavailable" even though they have nothing
+ // running. Drain on each SIGCHLD; signals coalesce so we loop.
+ startReaper()
+
// Signal handling:
// SIGTERM/SIGINT: clean shutdown (exit)
// SIGUSR1: hibernate prep — reset virtio-serial listener so Accept
diff --git a/cmd/agent/reaper_linux.go b/cmd/agent/reaper_linux.go
new file mode 100644
index 00000000..19256aac
--- /dev/null
+++ b/cmd/agent/reaper_linux.go
@@ -0,0 +1,59 @@
+//go:build linux
+
+package main
+
+import (
+ "log"
+ "os"
+ "os/signal"
+ "sync/atomic"
+ "syscall"
+)
+
+// startReaper spawns a goroutine that waits on SIGCHLD and reaps any
+// available zombie children. This is mandatory when osb-agent runs as PID 1
+// (always true inside the sandbox VM) — Linux re-parents orphaned processes
+// to PID 1, which is then responsible for calling wait() on them. If we
+// don't, zombies accumulate in the sandbox cgroup's pids.max budget and
+// eventually every fork() returns EAGAIN.
+//
+// Also calls prctl(PR_SET_CHILD_SUBREAPER) defensively so reaping still
+// works even if some future setup parents us to PID > 1.
+func startReaper() {
+ // Best-effort: PR_SET_CHILD_SUBREAPER (option 36). If running as PID 1
+ // this is a no-op; if not, it makes us inherit orphans of our subtree.
+ const PR_SET_CHILD_SUBREAPER = 36
+ _, _, _ = syscall.Syscall(syscall.SYS_PRCTL, PR_SET_CHILD_SUBREAPER, 1, 0)
+
+ ch := make(chan os.Signal, 1)
+ signal.Notify(ch, syscall.SIGCHLD)
+ go func() {
+ for range ch {
+ drainZombies()
+ }
+ }()
+}
+
+var reapedTotal atomic.Int64
+
+// drainZombies repeatedly waits for any child in non-blocking mode until
+// none are available. Multiple SIGCHLDs that arrive while we're already
+// reaping coalesce into one delivery, so each notification must drain all.
+func drainZombies() {
+ for {
+ var ws syscall.WaitStatus
+ pid, err := syscall.Wait4(-1, &ws, syscall.WNOHANG, nil)
+ if pid <= 0 {
+ // 0 = children exist but none ready; ECHILD = no children.
+ if err != nil && err != syscall.ECHILD {
+ log.Printf("agent: reaper: wait4 error: %v", err)
+ }
+ return
+ }
+ n := reapedTotal.Add(1)
+ // Log first few + every 100th so the journal stays readable.
+ if n <= 5 || n%100 == 0 {
+ log.Printf("agent: reaped pid=%d (status=%d, total=%d)", pid, ws.ExitStatus(), n)
+ }
+ }
+}
diff --git a/cmd/agent/reaper_other.go b/cmd/agent/reaper_other.go
new file mode 100644
index 00000000..b9162c1c
--- /dev/null
+++ b/cmd/agent/reaper_other.go
@@ -0,0 +1,8 @@
+//go:build !linux
+
+package main
+
+// startReaper is a no-op on non-Linux platforms. The agent only runs as
+// PID 1 inside the sandbox VM (Linux); local Darwin/Windows builds are
+// for testing and don't need to reap children.
+func startReaper() {}
diff --git a/deploy/ec2/build-rootfs-docker.sh b/deploy/ec2/build-rootfs-docker.sh
index 40758cd5..57c9f3b4 100755
--- a/deploy/ec2/build-rootfs-docker.sh
+++ b/deploy/ec2/build-rootfs-docker.sh
@@ -164,10 +164,15 @@ if [ -f /sys/fs/cgroup/cgroup.controllers ]; then
echo "+cpu +memory +pids" > /sys/fs/cgroup/cgroup.subtree_control 2>/dev/null
# Create sandbox cgroup
mkdir -p /sys/fs/cgroup/sandbox
- # Defaults: 256 pids, 90% of total memory, 90% of CPUs
+ # Defaults: 4096 pids, 90% of total memory, 90% of CPUs.
+ # 4096 is enough headroom for typical dev tooling (npm install, go test,
+ # multi-process build systems) while still bounding fork-bomb damage.
+ # The previous default of 128 was below an interactive shell + LSP +
+ # build agent working set, and customers hit "fork failed: resource
+ # temporarily unavailable" inside otherwise-idle sandboxes.
# CPU limit reserves 10% for the agent (PID 1) so it stays responsive
# even under fork bomb / CPU exhaustion attacks.
- echo 128 > /sys/fs/cgroup/sandbox/pids.max
+ echo 4096 > /sys/fs/cgroup/sandbox/pids.max
SANDBOX_MEM=$(awk '/MemTotal/{printf "%.0f", $2 * 1024 * 0.9}' /proc/meminfo)
echo "$SANDBOX_MEM" > /sys/fs/cgroup/sandbox/memory.max 2>/dev/null
# cpu.max: limit user processes to 80% of available CPUs.
@@ -178,7 +183,7 @@ if [ -f /sys/fs/cgroup/cgroup.controllers ]; then
echo "$CPU_MAX 100000" > /sys/fs/cgroup/sandbox/cpu.max 2>/dev/null
# cpu.weight: lower priority than agent
echo 50 > /sys/fs/cgroup/sandbox/cpu.weight 2>/dev/null
- echo "init: cgroup sandbox ready (pids=128, mem=${SANDBOX_MEM}, cpu=${CPU_MAX}/100000)"
+ echo "init: cgroup sandbox ready (pids=4096, mem=${SANDBOX_MEM}, cpu=${CPU_MAX}/100000)"
else
echo "init: warning: cgroup v2 not available"
fi