Skip to content

[reaper] Reap orphaned processes when we're init or a subreaper - #1455

Merged
manugarg merged 6 commits into
mainfrom
reap-orphaned-processes
Sep 2, 2026
Merged

manugarg merged 6 commits into
mainfrom
reap-orphaned-processes

Conversation

@manugarg

@manugarg manugarg commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

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 named headless_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.go can do, structurally cannot see them.

Why not the previous approaches

We tried this generically before and reverted it in #1132:

The missing piece both times was knowing whose zombie it is. The kernel won't tell us, but /proc will.

This change

internal/reaper scans /proc every 60s for zombie children and reaps only the ones still unclaimed 5 minutes later, with a plain wait4(<pid>, WNOHANG). Every spawn site in the tree is already blocked in wait before its child can exit (probes/common/command, probes/external server mode, internal/servers/external, internal/alerting/notifier, common/oauth — all cmd.Wait/Run/Output, and os/exec reaps before draining pipes), so a zombie that old has no owner coming for it. No wait4(-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 across execve). Otherwise Start returns immediately — so shareProcessNamespace: true on Kubernetes and --init on 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 wait4 with 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

TestOrphanedDetachedGrandchild reproduces the production shape in a subprocess: it makes itself a child subreaper (same duty as PID 1), orphans a setsid'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>/stat parsing (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.

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
manugarg force-pushed the reap-orphaned-processes branch from d7770f0 to 619a4c6 Compare September 2, 2026 16:51
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
manugarg force-pushed the reap-orphaned-processes branch from 619a4c6 to 1c0b4f9 Compare September 2, 2026 16:59
- 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.
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