Repository navigation
[reaper] Reap orphaned processes when we're init or a subreaper - #1455
Merged
Merged
Conversation
Browser probe runs leak zombie processes: playwright starts browsers detached (setsid), so the browser and the helpers it forks live in a process group of their own. When they're orphaned they're reparented to whoever is collecting orphans -- cloudprober itself, when it's PID 1 in its container -- and nothing ever waits on them. Waiting on the command's process group, which is all runcmd_linux.go can do, structurally can't see them. Reported in the field as ~28k <defunct> headless_shell processes, a handful per probe run. We tried reaping generically before (#1130, #1131) and reverted it in #1132: wait4(-1) also reaps our own direct children, stealing exit statuses that probes need, and serializing the reaper against runCommand with an RWMutex just stalls probe execution behind the waiting writer. The missing piece both times was knowing whose zombie it is, which the kernel won't tell us but /proc will: scan for zombie children, and reap only those still unclaimed a minute later. Everything cloudprober starts is waited on immediately (every spawn site blocks in cmd.Wait/Run/Output before its child can exit), so a zombie that old has no owner coming for it. No wait4(-1), no locks, and nothing in the probe path. This only runs when orphans are actually reparented to us, i.e. we're init in our PID namespace or a child subreaper. Otherwise somebody else is already doing the job -- /pause with shareProcessNamespace on k8s, tini with docker's --init -- and Start returns immediately, so those remain good ways to sidestep this. Also fixes a busy-spin in the existing process-group reaper: it polled wait4 with no delay whenever the group still had live processes, which for the browser probe could mean a 100% CPU spin for interval/2. Revisits #1128.
The kernel doesn't record when a process died, so "unclaimed for a minute" is measured from when we first saw the zombie. That's only sound if we can tell a recycled pid from the one we saw a minute ago, which /proc/<pid>/stat's start time (field 22) gives us for free.
manugarg
force-pushed
the
reap-orphaned-processes
branch
from
September 2, 2026 16:51
d7770f0 to
619a4c6
Compare
Reaping a zombie a few minutes later than we could costs nothing; the leak this fixes took 5+ months in production to become visible. Reaping one somebody was about to wait on breaks that probe run immediately. Buy margin on the side that hurts. Scan every 60s to match: a zombie needs two sightings a grace period apart, so looking twice a minute bought us nothing.
manugarg
force-pushed
the
reap-orphaned-processes
branch
from
September 2, 2026 16:59
619a4c6 to
1c0b4f9
Compare
- reap now returns the pid wait4 gave us. WNOHANG returns (0, nil) for a child that's alive, which is what a pid recycled between the /proc scan and the wait4 looks like; we were logging those as reaped. Drop the stale sighting either way, but only count a reap when wait4 says so. - runcmd_linux.go: bound the whole wait loop by a deadline, reaps included. Restoring the timeout check the rewrite dropped on the reap path, so a group that keeps producing dead children can't keep the goroutine (one per run) alive indefinitely. - Explain inside the non-Linux Start why its body is empty (sonar S1186).
Review follow-ups, comments only: - The recycled-pid note claimed more than the check delivers. wait4 sees a pid, not a (pid, start time), so a pid recycled to an already-exited child in the window between the scan and the call would still be reaped out from under its owner. Unreachable without a pid-space wraparound in that window, and not expressible through wait4; say so rather than implying the sighting key covers it. - scanInterval's rationale was written when it equalled the grace period. - Note that a stat read failing for anything but ENOENT restarts the grace period, i.e. it delays reaping rather than rushing it. - Tell embedders in cloudprober.Start's doc that this reaps their own unwaited-for children too, when they're init.
run() had no test at all: it now has to tick, reap, and return when its context is cancelled. Alongside it, the mid-scan cases a real /proc produces -- a process that vanishes between the readdir and the read, a stat we can't parse -- and a wait4 that fails for an unexpected reason, which leaves the sighting in place to retry rather than dropping it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Browser probe runs leak zombie processes. Reported in the field as ~28k
[headless_shell] <defunct>on a long-running instance, a handful accumulating per probe run, all parented to cloudprober.Playwright starts browsers detached (
processLauncher.js:detached: process.platform !== 'win32', i.e.setsid()), so the browser and the helper processes it forks — zygote, gpu, renderers, all namedheadless_shell— live in a process group of their own. When they outlive their parent they're reparented to whoever collects orphans, which is cloudprober itself when it's PID 1 in its container (our images have no init shim), and nothing ever waits on them.Waiting on the command's process group, which is all
runcmd_linux.gocan do, structurally cannot see them.Why not the previous approaches
We tried this generically before and reverted it in #1132:
wait4(-1, WNOHANG)on a timer. It also reaps our own direct children, stealing exit statuses that the external probe needs.runCommandvia anRWMutex. Go'sRWMutexblocks newRLocks once a writer is queued, so a waiting reaper stalls probe execution.The missing piece both times was knowing whose zombie it is. The kernel won't tell us, but
/procwill.This change
internal/reaperscans/procevery 60s for zombie children and reaps only the ones still unclaimed 5 minutes later, with a plainwait4(<pid>, WNOHANG). Every spawn site in the tree is already blocked in wait before its child can exit (probes/common/command,probes/externalserver mode,internal/servers/external,internal/alerting/notifier,common/oauth— allcmd.Wait/Run/Output, andos/execreaps before draining pipes), so a zombie that old has no owner coming for it. Nowait4(-1), no locks, nothing in the probe path.The grace period is deliberately generous because the two failure modes are asymmetric: leaking a zombie for a few extra minutes costs nothing (this leak ran for 5+ months before anyone noticed), while reaping a process somebody was about to wait on breaks that probe run immediately. Sightings are keyed on (pid, start time) from
/proc/<pid>/stat, so a recycled pid can't inherit an older process's first-sighting time.It runs only when orphans are actually reparented to us: PID 1 in our namespace, or a child subreaper (not inherited across
fork, but preserved acrossexecve). OtherwiseStartreturns immediately — soshareProcessNamespace: trueon Kubernetes and--initon docker remain good ways to sidestep the mechanism entirely, with no scanning at all.Also fixes a busy-spin in the existing process-group reaper: it polled
wait4with no delay whenever the group still had live processes, which for the browser probe (ChildProcessWaitTime=interval/2) could mean a 100% CPU spin for minutes.Testing
TestOrphanedDetachedGrandchildreproduces the production shape in a subprocess: it makes itself a child subreaper (same duty as PID 1), orphans asetsid'ed grandchild in its own process group, confirms it lands as an unreapable zombie, and that the reaper clears it. Plus unit tests for/proc/<pid>/statparsing (comm containing spaces and parens, malformed fields), the grace-period logic, pid recycling inside the grace window,ECHILD, and a real zombie against the real/proc.Revisits #1128.