diff --git a/.wolf/buglog.json b/.wolf/buglog.json index 9e3b0d8f..aea01cfa 100644 --- a/.wolf/buglog.json +++ b/.wolf/buglog.json @@ -22374,6 +22374,33 @@ ], "occurrences": 1, "last_seen": "2026-09-17T04:10:00Z" + }, + { + "id": "bug-1823", + "timestamp": "2026-09-17T04:20:00Z", + "error_message": "User-reported (regression on top of bug-1820's own fix, live on the user's real host, 'pc13v'): after ./scripts/stop-all.sh reported 'Stopping worker-1 (PID N)' with no warning, the real xcecd worker was STILL running afterward as a different PID -- and a second `sudo ./scripts/stop-all.sh` run immediately reported 'Nothing was running for this deploy' (the pidfile had already been deleted by the first run despite the kill not actually succeeding).", + "file": "src/tools/deploy-ha.sh (generated scripts/stop-all.sh and scripts/cleanup.sh)", + "root_cause": "bug-1820's own fix assumed sudo always has exactly ONE level of indirection (sudo forks exactly one direct child that IS the real target), and looked for that child via `pgrep -P $PID` (direct children of the pidfile'd PID only). On the user's real host, sudo interposes an ADDITIONAL 'monitor' process between the invoked sudo and the actual target (sudo -> monitor -> target, THREE processes, not two -- standard sudo 1.8.7+ architecture for proper signal/exit-status relay, not present in this sandbox's own simpler sudo build, which is why bug-1820's fix appeared to work here but not on the user's real box). `pgrep -P $PID` found and killed the MONITOR (a direct child of the pidfile'd PID) while the actual worker -- the monitor's OWN child, not a direct child of the pidfile'd PID -- survived completely untouched, silently orphaned exactly as in the original bug-1820 symptom. Confirmed and reproduced deterministically via a synthetic 3-level process tree (fake-outer -> fake-monitor -> fake-worker, no real sudo needed) built specifically to isolate the tree-walking logic from any particular sudo version's actual behavior.", + "fix": "FIXED (2026-09-17). Replaced the parent/child tree-walking approach entirely with a depth-independent identity-based re-scan: after attempting to kill the pidfile'd PID, both generated scripts/stop-all.sh and scripts/cleanup.sh now search system-wide (`pgrep -f`) for ANY still-running process whose own cmdline matches the worker's distinguishing identity -- its `-n $NAME` flag plus this specific deploy's `-l $TOP/logs` flag, the same two args start-worker.sh's own 'already running' guard already checks -- and kills every match directly (both a plain `kill -9` and a `sudo kill -9`, since ownership can't be known in advance), regardless of how many hops deep in the process tree it landed. No longer needs to know or guess sudo's internal process-tree depth. A no-op for ctrl-N/scheduler/apiserver pidfiles, which use neither flag. Live-verified: (1) regression-checked against this sandbox's own real (2-level) sudo worker deploy -- still cleans up correctly; (2) built and killed a synthetic 3-level tree (fake-outer/fake-monitor/fake-worker, exact process-tree shape modeled on the user's live report) via the exact identity-pgrep mechanism the fix uses, confirming the deepest (target) process is found and killed directly, and the intermediate 'monitor' hop then exits on its own once its child is gone (it was just wait()-ing/relaying) -- proving the fix generalizes to arbitrary sudo process-tree depth rather than being tuned to one specific sudo version's behavior.", + "tags": [ + "deploy-ha", + "stop-all.sh", + "cleanup.sh", + "xcecd", + "sudo", + "orphaned-process", + "real-worker", + "sudo-monitor", + "process-tree-depth", + "fixed", + "live-verified", + "synthetic-repro" + ], + "related_bugs": [ + "bug-1820" + ], + "occurrences": 1, + "last_seen": "2026-09-17T04:20:00Z" } ] } diff --git a/src/tools/deploy-ha.sh b/src/tools/deploy-ha.sh index 37f7d500..e2d3b141 100755 --- a/src/tools/deploy-ha.sh +++ b/src/tools/deploy-ha.sh @@ -325,33 +325,40 @@ for PF in $TOP/run/*.pid; do NAME=\$(basename "\$PF" .pid) if [ -n "\$PID" ] && kill -0 "\$PID" 2>/dev/null; then echo "cleanup.sh: stopping \$NAME (PID \$PID)" - # bug-1820: the real (sudo-launched) worker's pidfile records sudo's - # OWN pid -- start-worker.sh's "exec sudo \$WORKER_BIN ..." replaces - # the pidfile-writing shell with sudo in place (same pid), but sudo - # then forks a SEPARATE, root-owned child that actually runs the - # worker. kill -9 on sudo's pid kills only sudo -- SIGKILL cannot be - # forwarded to a child -- silently orphaning the real worker, and the - # "still alive" check below never catches it either (sudo's own pid, - # owned by the invoking user, dies cleanly). Detect a sudo wrapper by - # its cmdline and kill its child(ren) first, via sudo (root-owned). - CHILDREN="" - if tr '\0' ' ' < /proc/\$PID/cmdline 2>/dev/null | grep -q '^sudo '; then - CHILDREN=\$(pgrep -P "\$PID" 2>/dev/null || true) - for CHILD in \$CHILDREN; do - echo " \$NAME (PID \$PID) is a sudo wrapper -- also stopping its child (PID \$CHILD)" - sudo kill -9 "\$CHILD" 2>/dev/null || true - done - fi kill -9 "\$PID" 2>/dev/null || true - sleep 0.2 - STILL_ALIVE=0 - kill -0 "\$PID" 2>/dev/null && STILL_ALIVE=1 - for CHILD in \$CHILDREN; do - [ -d "/proc/\$CHILD" ] && STILL_ALIVE=1 - done - if [ "\$STILL_ALIVE" -eq 1 ]; then - echo "Warning: \$NAME (PID \$PID \$CHILDREN) still alive after kill -9 -- likely the real worker running as root; try: sudo kill -9 \$PID \$CHILDREN" - fi + fi + # bug-1820/1823: a real (sudo-launched) worker's pidfile records sudo's + # OWN pid (start-worker.sh's "exec sudo \$WORKER_BIN ..." replaces the + # pidfile-writing shell with sudo in place, same pid) -- but the ACTUAL + # worker process can be several sudo-internal hops away: modern sudo + # (1.8.7+) commonly interposes an additional "monitor" process before + # the real target (sudo -> monitor -> target, three processes, not + # two), so a parent/child walk of just \$PID's DIRECT children (this + # fix's own first attempt, bug-1820) finds and kills the monitor while + # the real worker -- the monitor's OWN child, not a direct child of + # the pidfile'd PID -- survives untouched. Live-reproduced: process- + # tree depth varies by sudo version/config, so rather than guess how + # many hops deep it goes on any given host, independently re-scan for + # ANY still-running process whose own cmdline matches this worker's + # distinguishing identity (its -n \$NAME flag plus this deploy's -l + # $TOP/logs flag -- the same two args start-worker.sh's own "already + # running" guard already checks) and kill it directly, wherever it + # landed in the process tree. A no-op for ctrl-N/scheduler/apiserver + # pidfiles, which use neither flag. + SURVIVORS=\$(pgrep -f -- "-n \$NAME .*-l $TOP/logs" 2>/dev/null || true) + for SURVIVOR in \$SURVIVORS; do + echo " \$NAME: process \$SURVIVOR still running -- stopping it directly (sudo process-tree workaround)" + kill -9 "\$SURVIVOR" 2>/dev/null || true + sudo kill -9 "\$SURVIVOR" 2>/dev/null || true + done + sleep 0.2 + STILL_ALIVE=0 + [ -n "\$PID" ] && kill -0 "\$PID" 2>/dev/null && STILL_ALIVE=1 + for SURVIVOR in \$SURVIVORS; do + [ -d "/proc/\$SURVIVOR" ] && STILL_ALIVE=1 + done + if [ "\$STILL_ALIVE" -eq 1 ]; then + echo "Warning: \$NAME (PID \$PID \$SURVIVORS) still alive after kill -9 -- likely the real worker running as root; try: sudo kill -9 \$PID \$SURVIVORS" fi done rm -f $TOP/logs/*.log @@ -378,38 +385,41 @@ for PF in $TOP/run/*.pid; do [ -f "\$PF" ] || continue PID=\$(cat "\$PF" 2>/dev/null || true) NAME=\$(basename "\$PF" .pid) + FOUND_SOMETHING=0 if [ -n "\$PID" ] && kill -0 "\$PID" 2>/dev/null; then echo "Stopping \$NAME (PID \$PID)" - # bug-1820: see cleanup.sh's identical comment above -- the real - # (sudo-launched) worker's pidfile records sudo's OWN pid (its - # start-worker.sh does "exec sudo \$WORKER_BIN ...", so the - # pidfile-writing shell becomes sudo in place, same pid), but sudo - # forks a SEPARATE, root-owned child that actually runs the worker. - # kill -9 on sudo's pid only kills sudo -- SIGKILL cannot be - # forwarded to a child -- silently orphaning the real worker, and - # the "still alive" check below never caught it either (sudo's own - # pid, owned by the invoking user, dies cleanly). Detect a sudo - # wrapper by its cmdline and kill its child(ren) first, via sudo. - CHILDREN="" - if tr '\0' ' ' < /proc/\$PID/cmdline 2>/dev/null | grep -q '^sudo '; then - CHILDREN=\$(pgrep -P "\$PID" 2>/dev/null || true) - for CHILD in \$CHILDREN; do - echo " \$NAME (PID \$PID) is a sudo wrapper -- also stopping its child (PID \$CHILD)" - sudo kill -9 "\$CHILD" 2>/dev/null || true - done - fi kill -9 "\$PID" 2>/dev/null || true - sleep 0.2 - STILL_ALIVE=0 - kill -0 "\$PID" 2>/dev/null && STILL_ALIVE=1 - for CHILD in \$CHILDREN; do - [ -d "/proc/\$CHILD" ] && STILL_ALIVE=1 - done - if [ "\$STILL_ALIVE" -eq 1 ]; then - echo "Warning: \$NAME (PID \$PID \$CHILDREN) still alive after kill -9 -- likely the real worker running as root; try: sudo kill -9 \$PID \$CHILDREN" - else - STOPPED=1 - fi + FOUND_SOMETHING=1 + fi + # bug-1820/1823: see cleanup.sh's identical comment above -- a real + # (sudo-launched) worker's pidfile records sudo's OWN pid, but the + # ACTUAL worker process can be several sudo-internal hops away (modern + # sudo commonly interposes an additional "monitor" process before the + # real target, so a parent/child walk of just \$PID's DIRECT children + # -- this fix's own first attempt, bug-1820 -- finds and kills the + # monitor while the real worker, the monitor's OWN child, survives + # untouched). Rather than guess the process-tree depth, independently + # re-scan for ANY still-running process whose own cmdline matches this + # worker's distinguishing identity (-n \$NAME plus this deploy's -l + # $TOP/logs) and kill it directly. A no-op for ctrl-N/scheduler/ + # apiserver pidfiles, which use neither flag. + SURVIVORS=\$(pgrep -f -- "-n \$NAME .*-l $TOP/logs" 2>/dev/null || true) + [ -n "\$SURVIVORS" ] && FOUND_SOMETHING=1 + for SURVIVOR in \$SURVIVORS; do + echo " \$NAME: process \$SURVIVOR still running -- stopping it directly (sudo process-tree workaround)" + kill -9 "\$SURVIVOR" 2>/dev/null || true + sudo kill -9 "\$SURVIVOR" 2>/dev/null || true + done + sleep 0.2 + STILL_ALIVE=0 + [ -n "\$PID" ] && kill -0 "\$PID" 2>/dev/null && STILL_ALIVE=1 + for SURVIVOR in \$SURVIVORS; do + [ -d "/proc/\$SURVIVOR" ] && STILL_ALIVE=1 + done + if [ "\$STILL_ALIVE" -eq 1 ]; then + echo "Warning: \$NAME (PID \$PID \$SURVIVORS) still alive after kill -9 -- likely the real worker running as root; try: sudo kill -9 \$PID \$SURVIVORS" + elif [ "\$FOUND_SOMETHING" -eq 1 ]; then + STOPPED=1 fi rm -f "\$PF" done