Skip to content

[probes.command] Fix zombie reaper mechanism - #1130

Merged
manugarg merged 1 commit into
mainfrom
external_cleanup_2
Aug 19, 2025
Merged

manugarg merged 1 commit into
mainfrom
external_cleanup_2

Conversation

@manugarg

@manugarg manugarg commented Aug 19, 2025 •

Copy link
Copy Markdown
Contributor
  • After a lot of research and testing, it's clear to me that my old implementation of zombie reaper doesn't really work. Reason is -- once child process exits, that child's children (cloudprober's grandchildren) will get reparented to pid 1 immediately.

  • Calling wait4 on these grandchildren's pgid (-pgid) is not going to work whether cloudprober is pid 1 or not. It should theoretically have worked when cloudprober's pid is 1 because orphaned process will get reparented to cloudprober (e.g. on docker, k8s), but I've confirmed that in practice it doesn't. [Update] I was wrong. In the case I was looking at, descendants were being put in a different process group (pg) by their parents -- that's why waiting on them with their grandparents' pgid didn't work.

  • I've changed my implementation to instead wait on pid -1, which basically will reap all children of cloudprober. We start one goroutine per cloudprober process that checks every minute for any children to be waited. I've verified that this works in docker environment.

This should fix #1128.

@manugarg
manugarg merged commit fac0ae8 into main Aug 19, 2025
@manugarg
manugarg deleted the external_cleanup_2 branch August 19, 2025 01:30
manugarg added a commit that referenced this pull request Sep 2, 2026
Browser probe runs leaked 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 get reparented to
whoever collects orphans -- cloudprober itself, when it's PID 1 in its
container -- and nothing ever waited on them. Waiting on the command's
process group, all runcmd_linux.go can do, structurally can't see them.
Seen in the field as ~28k <defunct> headless_shell processes after five
months, 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 the ones still unclaimed five minutes 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. Sightings are keyed on (pid, start time), so a
recycled pid can't inherit an older process's clock. No wait4(-1), no
locks, nothing in the probe path.

This runs only 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
Kubernetes, tini with docker's --init -- and Start returns immediately,
so those remain good ways to sidestep the mechanism entirely.

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 minutes.

Revisits #1128.
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.

[probes.browser] We need a better way to collect forked off proceses

1 participant