Repository navigation
fix(process): use monotonic clock for orphaned process-tree reap grace deadline - #181335
Conversation
…e deadline Ref: BUG-213-orphaned-process-reap-grace-deadline-wall-clock (local finding)
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. What this changesThis PR measures orphaned-process cleanup grace periods with a monotonic clock. Example: A managed macOS process ignores SIGTERM while the wall clock moves backward by 60 seconds.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep this PR open for landing: it fixes a current process-cleanup timing defect with a focused change and no actionable findings. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherManaged llama.cpp recovery passes executable and argument identity into the shared orphan reaper, which captures matching processes, validates their identities, and sends termination signals. flowchart TD
A[Managed llama.cpp acquisition] --> B[Managed server recovery]
B --> C[Capture matching orphan tree]
C --> D[Validate identity and send SIGTERM]
D --> E[Monotonic grace deadline]
E --> F[Revalidate survivors and send SIGKILL]
Technical reviewBest possible solution: Land the focused clock replacement in the existing orphan-recovery owner with its regression coverage. Do we have a high-confidence way to reproduce the issue? The current-main deadline arithmetic establishes the failure path, supported by contributor-supplied red/green tests and real macOS process-recovery output; no target code was executed during this review. Is this the best way to solve the issue? This is the smallest complete fix: keep deadline creation, comparison, and remaining-budget calculation on one monotonic clock without changing recovery ownership or signaling safeguards. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against e78d8fcab266. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource 0, Tests +27. Total +27 across 2 files. Added test files: 0. View PR surface stats
LabelsLabel changes: No label changes. Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReviewed October 23, 2026, 2:12 AM ET / 06:12 UTC (Revision 3). |
|
🦞👀 Re-review progress:
|
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review. |
|
🦞👀 Re-review progress:
|
|
Merged via squash.
|
|
@steipete Thanks! |
…e deadline (openclaw#181335) ## What Problem This Solves When a worker/sandbox environment exits, `reapOrphanedProcesses` clears its orphaned process tree. A child that ignores `SIGTERM` is escalated to `SIGKILL` after a 5-second grace window — but that window is measured on the wall clock. If the wall clock steps backward during teardown (NTP correction, VM or laptop resume), the escalation is delayed by the length of the step, so the stuck child stays alive for `5s + step` instead of `5s`. This change keeps the reap grace deadline on the monotonic clock. - **Problem**: `waitForExit` seeds and compares its 5s grace deadline with `Date.now()` (wall clock), while `sleepWithAbort` consumes the remaining budget through `setTimeout` (monotonic). A backward wall-clock step during teardown inflates the grace window, delaying the `SIGKILL` escalation of a child that ignores `SIGTERM`; a forward step escalates early. - **Solution**: use `performance.now()` for the deadline seed, the exit-condition comparison, and the remaining-budget calculation. `performance.now()` is monotonic and unaffected by wall-clock adjustments. - **What changed**: `src/process/orphaned-process.ts` — the `waitForExit` grace deadline now lives entirely on the monotonic clock (3 lines). `src/process/orphaned-process.test.ts` — a regression test that rewinds the wall clock mid-teardown and asserts the `SIGKILL` escalation stays bounded at 5s. - **What did NOT change**: no parser, policy branch, or configuration surface; no behavior change on a normal clock. The `sleepWithAbort` consumer is untouched — `resolveSleepDelayMs` already floors internally, so no `Math.floor` is needed here. ## Why This Change Was Made `reapOrphanedProcesses` is the canonical owner of the orphaned-process-tree reap grace deadline, so the fix lives there rather than at call sites. This is the same cross-clock-domain pattern already fixed for command process-group teardown in openclaw#174151 (`exec-spawn.ts` / `exec-termination.ts`); `orphaned-process.ts` `waitForExit` is the sibling site in the same module that was left on the wall clock. Node 16+ exposes a global `performance`, so no import is required. ## User Impact Environment exit and resource reclamation are no longer delayed or spuriously accelerated by wall-clock adjustments. An orphaned child that ignores `SIGTERM` is always force-killed after a bounded 5s monotonic grace window, regardless of NTP corrections or machine resume. ## Evidence **Pre-fix RED** — source reverted to `Date.now()`, regression test kept: the 60s wall rewind inflates the grace window, tree never reaped in 5s monotonic, test times out at 120s. ```console $ npx vitest run src/process/orphaned-process.test.ts × keeps SIGKILL escalation bounded when the wall clock rewinds 120079ms (timed out 120000ms) Test Files 1 failed | Tests 1 failed | 21 passed (22) ``` **Post-fix GREEN** — with `performance.now()`, same test completes in 56ms, suite 22/22. ```console $ npx vitest run src/process/orphaned-process.test.ts ✓ keeps SIGKILL escalation bounded when the wall clock rewinds 56ms Test Files 1 passed | Tests 22 passed (22) ``` **Behavior matrix** (wall rewound 60s during teardown, child ignores `SIGTERM`): ```text clock during teardown => SIGKILL escalation => result normal (no step) => after 5s grace => bounded wall rewind 60s (pre-fix) => after 5s + 60s (hangs) => delayed by step wall rewind 60s (post-fix) => after 5s grace => bounded ``` **Regression test** uses openclaw#174151's two-clock separation: `vi.setSystemTime(Date.now() - 60_000)` rewinds only wall clock; `performance.now()` keeps advancing on the fake-timer clock. **macOS real-behavior proof (single continuous capture)** — GHA `macos-14` arm64, through the shipped `recoverManagedLlamaServer` (`extensions/llama-cpp`) entry: real managed llama-server asset at its pinned install path (root PPID=1), TERM-resistant descendant, real native inspection + real `process.kill`. 60s wall-clock rollback injected at the remaining-budget read (CI pins clock via NTP; monotonic clock + transport stay real). Full run: [oc-macos-proof run 39908934594](https://github.com/SunnyShu0925/oc-macos-proof/actions/runs/39908934594). ```console ===== CASE: FIXED (wall jump -60000ms) ===== [DIAG] platform=darwin arch=arm64 state_dir=/tmp/llama-state [DIAG] findManagedLlamaServerAsset(command) => llama-b10809-bin-macos-arm64.tar.gz [FIXED] before recovery: 4964 1 /tmp/llama-state/tools/llama.cpp/b10809/darwin-arm64/llama-server [llama-cpp] reaping orphaned managed llama-server: pid=4964 port=19321 [FIXED] recoverManagedLlamaServer returned elapsed_monotonic=5634ms root_alive_after=false VERDICT[FIXED] wall_jump=-60000ms elapsed_monotonic=5634ms reaped_root=4964 root_alive_after=false PROOF PASS: FIXED escalated to SIGKILL in ~5s monotonic under wall-clock rollback ===== CASE: OLD (wall jump -60000ms) ===== [DIAG] platform=darwin arch=arm64 state_dir=/tmp/llama-state [DIAG] findManagedLlamaServerAsset(command) => llama-b10809-bin-macos-arm64.tar.gz [OLD] before recovery: 5509 1 /tmp/llama-state/tools/llama.cpp/b10809/darwin-arm64/llama-server [llama-cpp] reaping orphaned managed llama-server: pid=5509 port=19321 [OLD] recoverManagedLlamaServer returned elapsed_monotonic=65290ms root_alive_after=false VERDICT[OLD] wall_jump=-60000ms elapsed_monotonic=65290ms reaped_root=5509 root_alive_after=false PROOF EXPECTED-OLD: OLD delayed SIGKILL by ~|rollback| (bug reproduced on real tree) ``` | variant | deadline clock | SIGKILL escalation (rollback −60s) | |---|---|---| | OLD (pre-fix) | `Date.now()` | **65.29s** = 5s grace + 60s rollback | | FIXED (this PR) | `performance.now()` | **5.63s** = bounded grace | Real managed-service recovery on a real macOS orphan tree, matching already-landed sibling openclaw#174151. **Open PR collision check**: no open/merged PR touches `orphaned-process.ts` clock domain; sibling openclaw#174151 landed for `exec-spawn.ts`/`exec-termination.ts`, no overlap. Co-authored-by: SunnyShu <[email protected]>
…e deadline (#181335) ## What Problem This Solves When a worker/sandbox environment exits, `reapOrphanedProcesses` clears its orphaned process tree. A child that ignores `SIGTERM` is escalated to `SIGKILL` after a 5-second grace window — but that window is measured on the wall clock. If the wall clock steps backward during teardown (NTP correction, VM or laptop resume), the escalation is delayed by the length of the step, so the stuck child stays alive for `5s + step` instead of `5s`. This change keeps the reap grace deadline on the monotonic clock. - **Problem**: `waitForExit` seeds and compares its 5s grace deadline with `Date.now()` (wall clock), while `sleepWithAbort` consumes the remaining budget through `setTimeout` (monotonic). A backward wall-clock step during teardown inflates the grace window, delaying the `SIGKILL` escalation of a child that ignores `SIGTERM`; a forward step escalates early. - **Solution**: use `performance.now()` for the deadline seed, the exit-condition comparison, and the remaining-budget calculation. `performance.now()` is monotonic and unaffected by wall-clock adjustments. - **What changed**: `src/process/orphaned-process.ts` — the `waitForExit` grace deadline now lives entirely on the monotonic clock (3 lines). `src/process/orphaned-process.test.ts` — a regression test that rewinds the wall clock mid-teardown and asserts the `SIGKILL` escalation stays bounded at 5s. - **What did NOT change**: no parser, policy branch, or configuration surface; no behavior change on a normal clock. The `sleepWithAbort` consumer is untouched — `resolveSleepDelayMs` already floors internally, so no `Math.floor` is needed here. ## Why This Change Was Made `reapOrphanedProcesses` is the canonical owner of the orphaned-process-tree reap grace deadline, so the fix lives there rather than at call sites. This is the same cross-clock-domain pattern already fixed for command process-group teardown in #174151 (`exec-spawn.ts` / `exec-termination.ts`); `orphaned-process.ts` `waitForExit` is the sibling site in the same module that was left on the wall clock. Node 16+ exposes a global `performance`, so no import is required. ## User Impact Environment exit and resource reclamation are no longer delayed or spuriously accelerated by wall-clock adjustments. An orphaned child that ignores `SIGTERM` is always force-killed after a bounded 5s monotonic grace window, regardless of NTP corrections or machine resume. ## Evidence **Pre-fix RED** — source reverted to `Date.now()`, regression test kept: the 60s wall rewind inflates the grace window, tree never reaped in 5s monotonic, test times out at 120s. ```console $ npx vitest run src/process/orphaned-process.test.ts × keeps SIGKILL escalation bounded when the wall clock rewinds 120079ms (timed out 120000ms) Test Files 1 failed | Tests 1 failed | 21 passed (22) ``` **Post-fix GREEN** — with `performance.now()`, same test completes in 56ms, suite 22/22. ```console $ npx vitest run src/process/orphaned-process.test.ts ✓ keeps SIGKILL escalation bounded when the wall clock rewinds 56ms Test Files 1 passed | Tests 22 passed (22) ``` **Behavior matrix** (wall rewound 60s during teardown, child ignores `SIGTERM`): ```text clock during teardown => SIGKILL escalation => result normal (no step) => after 5s grace => bounded wall rewind 60s (pre-fix) => after 5s + 60s (hangs) => delayed by step wall rewind 60s (post-fix) => after 5s grace => bounded ``` **Regression test** uses #174151's two-clock separation: `vi.setSystemTime(Date.now() - 60_000)` rewinds only wall clock; `performance.now()` keeps advancing on the fake-timer clock. **macOS real-behavior proof (single continuous capture)** — GHA `macos-14` arm64, through the shipped `recoverManagedLlamaServer` (`extensions/llama-cpp`) entry: real managed llama-server asset at its pinned install path (root PPID=1), TERM-resistant descendant, real native inspection + real `process.kill`. 60s wall-clock rollback injected at the remaining-budget read (CI pins clock via NTP; monotonic clock + transport stay real). Full run: [oc-macos-proof run 39908934594](https://github.com/SunnyShu0925/oc-macos-proof/actions/runs/39908934594). ```console ===== CASE: FIXED (wall jump -60000ms) ===== [DIAG] platform=darwin arch=arm64 state_dir=/tmp/llama-state [DIAG] findManagedLlamaServerAsset(command) => llama-b10809-bin-macos-arm64.tar.gz [FIXED] before recovery: 4964 1 /tmp/llama-state/tools/llama.cpp/b10809/darwin-arm64/llama-server [llama-cpp] reaping orphaned managed llama-server: pid=4964 port=19321 [FIXED] recoverManagedLlamaServer returned elapsed_monotonic=5634ms root_alive_after=false VERDICT[FIXED] wall_jump=-60000ms elapsed_monotonic=5634ms reaped_root=4964 root_alive_after=false PROOF PASS: FIXED escalated to SIGKILL in ~5s monotonic under wall-clock rollback ===== CASE: OLD (wall jump -60000ms) ===== [DIAG] platform=darwin arch=arm64 state_dir=/tmp/llama-state [DIAG] findManagedLlamaServerAsset(command) => llama-b10809-bin-macos-arm64.tar.gz [OLD] before recovery: 5509 1 /tmp/llama-state/tools/llama.cpp/b10809/darwin-arm64/llama-server [llama-cpp] reaping orphaned managed llama-server: pid=5509 port=19321 [OLD] recoverManagedLlamaServer returned elapsed_monotonic=65290ms root_alive_after=false VERDICT[OLD] wall_jump=-60000ms elapsed_monotonic=65290ms reaped_root=5509 root_alive_after=false PROOF EXPECTED-OLD: OLD delayed SIGKILL by ~|rollback| (bug reproduced on real tree) ``` | variant | deadline clock | SIGKILL escalation (rollback −60s) | |---|---|---| | OLD (pre-fix) | `Date.now()` | **65.29s** = 5s grace + 60s rollback | | FIXED (this PR) | `performance.now()` | **5.63s** = bounded grace | Real managed-service recovery on a real macOS orphan tree, matching already-landed sibling #174151. **Open PR collision check**: no open/merged PR touches `orphaned-process.ts` clock domain; sibling #174151 landed for `exec-spawn.ts`/`exec-termination.ts`, no overlap. Co-authored-by: SunnyShu <[email protected]> (cherry picked from commit f9a17a7) Co-authored-by: RomneyDa <[email protected]>
What Problem This Solves
When a worker/sandbox environment exits,
reapOrphanedProcessesclears its orphaned process tree. A child that ignoresSIGTERMis escalated toSIGKILLafter a 5-second grace window — but that window is measured on the wall clock. If the wall clock steps backward during teardown (NTP correction, VM or laptop resume), the escalation is delayed by the length of the step, so the stuck child stays alive for5s + stepinstead of5s. This change keeps the reap grace deadline on the monotonic clock.waitForExitseeds and compares its 5s grace deadline withDate.now()(wall clock), whilesleepWithAbortconsumes the remaining budget throughsetTimeout(monotonic). A backward wall-clock step during teardown inflates the grace window, delaying theSIGKILLescalation of a child that ignoresSIGTERM; a forward step escalates early.performance.now()for the deadline seed, the exit-condition comparison, and the remaining-budget calculation.performance.now()is monotonic and unaffected by wall-clock adjustments.src/process/orphaned-process.ts— thewaitForExitgrace deadline now lives entirely on the monotonic clock (3 lines).src/process/orphaned-process.test.ts— a regression test that rewinds the wall clock mid-teardown and asserts theSIGKILLescalation stays bounded at 5s.sleepWithAbortconsumer is untouched —resolveSleepDelayMsalready floors internally, so noMath.flooris needed here.Why This Change Was Made
reapOrphanedProcessesis the canonical owner of the orphaned-process-tree reap grace deadline, so the fix lives there rather than at call sites. This is the same cross-clock-domain pattern already fixed for command process-group teardown in #174151 (exec-spawn.ts/exec-termination.ts);orphaned-process.tswaitForExitis the sibling site in the same module that was left on the wall clock. Node 16+ exposes a globalperformance, so no import is required.User Impact
Environment exit and resource reclamation are no longer delayed or spuriously accelerated by wall-clock adjustments. An orphaned child that ignores
SIGTERMis always force-killed after a bounded 5s monotonic grace window, regardless of NTP corrections or machine resume.Evidence
Pre-fix RED — source reverted to
Date.now(), regression test kept: the 60s wall rewind inflates the grace window, tree never reaped in 5s monotonic, test times out at 120s.Post-fix GREEN — with
performance.now(), same test completes in 56ms, suite 22/22.Behavior matrix (wall rewound 60s during teardown, child ignores
SIGTERM):Regression test uses #174151's two-clock separation:
vi.setSystemTime(Date.now() - 60_000)rewinds only wall clock;performance.now()keeps advancing on the fake-timer clock.macOS real-behavior proof (single continuous capture) — GHA
macos-14arm64, through the shippedrecoverManagedLlamaServer(extensions/llama-cpp) entry: real managed llama-server asset at its pinned install path (root PPID=1), TERM-resistant descendant, real native inspection + realprocess.kill. 60s wall-clock rollback injected at the remaining-budget read (CI pins clock via NTP; monotonic clock + transport stay real). Full run: oc-macos-proof run 39908934594.Date.now()performance.now()Real managed-service recovery on a real macOS orphan tree, matching already-landed sibling #174151.
Open PR collision check: no open/merged PR touches
orphaned-process.tsclock domain; sibling #174151 landed forexec-spawn.ts/exec-termination.ts, no overlap.