Commit Graph
4 Commits
Author SHA1 Message Date
yetone 73ce9765f1 proc: EndProbes returns once a killed probe's whole group is gone, so an exit right after can't leave a missed child running (#1278)
JJ-Lin's fault injection on #1278: when the first signal to a probe's
group missed a process being forked, group.kill went on signalling it
from a goroutine nobody waited for, and the probe counted as ended as
soon as the first signal went out. EndProbes returned in ~0.2ms; magpie
then exiting (or Exec'ing the new version) ended that goroutine, and the
missed child lived on with PPID 1.

group.kill now returns a channel closed once none of the group is left
(or killFor has passed). A probe's Cancel marks its kill as begun and
counts the probe ended only when that channel closes. EndProbes waits up
to endWait for each kill to begin (a probe that already answered has
none, as before) and then for the group to be empty, bounded by killFor
plus a second. Every exit path goes through EndProbes: the GUI's
OnShutdown, the CLI's return and signal handler, and update.Reexec.

Windows: the job's TerminateJobObject takes no new process once it is
ending, and a probe is killed alone with TerminateProcess, so there was
no background loop to outlive; kill returns a closed channel there. The
known Windows gap that a shim's children aren't in a job for probes is
unchanged.

Run:
- TestEndProbesThenExitLeavesNothing (new) re-runs the test binary as a
  magpie whose group signals miss for 300ms, which calls EndProbes and
  os.Exit(0) at once. On the old code it fails 3/3 on darwin: "what the
  probe started (pid N) outlived magpie's exit after EndProbes".
- darwin: go test -race ./internal/proc ok; -race -count=10 of the
  probe/EndProbes tests ok.
- linux (docker golang:1.26.8 --init, GOTOOLCHAIN=local): -race -count=5
  of the probe/tree tests ok. Without --init the container's PID 1
  reaps nothing and every group test fails on zombies, on old code too.
- Windows box: new TestEndProbesWindows (ping probe ended, EndProbes
  ~0ms) passes 3/3; the rest of the package passes except
  TestNoCommandBypassesProc, which walks ../.. and found other
  checkouts in C:\Users\yeton since the binary ran outside a tree.
- gofmt -l, go vet (darwin), GOOS=linux vet -tags nogui, GOOS=windows vet.
2026-10-13 05:21:03 +08:00
ggbdpqandfactory-droid[bot] c409693806 proc: a test's own signalGroup put back no longer races the kill loop still reading it (#1568, ggbdpq)
What changed:
- group.kill's retry loop outlives its caller: it keeps signalling a
  process group every 5ms for up to 10s after the caller (a probe's
  cmd.Cancel on exec's watchCtx goroutine) has gone. It read the package
  var signalGroup on every pass, so a test that swaps the var for its own
  and puts the old one back when it ends (TestProbeKillEndsWhatTheSignal-
  Missed's cleanup) raced the loop's reads: go test -race ./internal/proc/
  went red with "WARNING: DATA RACE" (write at probe_test.go:151, read at
  tree_other.go:43), 5 of 5 runs.
- kill now takes the kill function once at its entry and the loop uses
  that copy. In production nothing changes (the var is never written);
  a test's swap is seen by every kill started after it, and a loop under
  way finishes with the function it was started with, which is the one
  that must answer whether the group is empty.

Run:
- go test -race -tags nogui -count=1 -run TestProbeKillEndsWhatTheSignalMissed
  ./internal/proc/: FAIL with DATA RACE without the change (1 warning,
  5 of 5 runs), ok with it, and -race -count=20 ok.
- go test -race -tags nogui -count=1 and -count=1 ./internal/proc/ ok.
  Nothing else in the tree reads or writes signalGroup (grep).

Review: on main (3f5702421) go test -race -count=5 -run
TestProbeKillEndsWhatTheSignalMissed ./internal/proc reported 4 DATA RACE
warnings: the cleanup's write (probe_test.go:151) and the next run's swap
(probe_test.go:141) against the loop's read (tree_other.go:43), and one
run failed. With the change -race -count=30 is ok and the whole package
is ok under -race.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2026-10-11 12:00:41 +08:00
yetone 1831ac4b63 proc: a CLI probe or agent run that is killed leaves nothing of itself running on Mac and Linux, even a launcher that keeps calling itself: its process group is signalled until it is empty, not once (#1278, JJ-Lin)
An old ~/.local/bin/cursor-agent that runs `cursor agent`, which (Cursor's
own bin/cursor) first runs ~/.local/bin/cursor-agent --min-version=... status,
calls itself without end. magpie asks cursor-agent who is signed in (about,
10s timeout) and kills the probe's session on the timeout with one SIGKILL
to the group. On macOS that one signal can miss a process being forked as it
goes out; the miss lives on, in the same group, adopted by init, and keeps
calling itself until the user can start no process (JJ-Lin: ~4800 bash
processes in one chain whose root's parent is PID 1). group.kill now sends
SIGKILL to the group again every 5ms, behind the caller, until the group is
gone (ESRCH) or 10s pass. The group keeps its id while any of it lives, so
nothing else is signalled. It covers ProbeContext's timeout and EndProbes,
and StartTree's Kill and sweep.

Verified:
- red without the change: TestProbeKillEndsWhatTheSignalMissed (the first
  group signal is made to miss, as the race does) fails with
  "what the probe started (pid 95576) outlived a kill that missed it once";
  passes with it (-count=5).
- reproduced the leak with the real Cursor 3.12.6 launcher script
  (/Applications/Cursor.app/.../bin/cursor, read only) and JJ-Lin's 87-byte
  wrapper in a sandbox HOME, under ulimit -u: ProbeContext(cursor-agent about)
  with a 10s timeout peaked at 311-700 processes; before the change 1 of 3
  runs left a chain behind (root ppid 1, same pgid, args
  "agent --min-version=2025.10.01 status"), after it 0 of 6. A fake
  launcher (60 parallel self-calling chains, killed at 500ms) left survivors
  4 of 30 runs before, 0 of 30 after.
- the same through provider.askCursorStatus with the real launcher, 5 runs:
  no cursor process left after each.
- go test -tags nogui ./internal/proc/ in a sandbox HOME; go vet -tags nogui
  of proc for darwin, linux and windows.
2026-10-08 18:18:15 +08:00
yetone b5340c7033 grok, cursor: a run ends with its caller — one whose client left mid-reply (Esc, /clear, Claude Code quit) is stopped at once instead of answering no one, one waiting on tool results that never come is let go after 5 minutes, not 30, and a stop kills what the CLI started too (its own session on Mac and Linux, a job on Windows). A tool that runs longer still gets its answer, from a run told the whole conversation. Runs are stopped with the gateway 2026-09-27 17:42:01 +08:00