Repository navigation
fix(gateway): stop post-ready sidecar timers on close - #117242
Conversation
|
Codex review: needs real behavior proof before merge. Reviewed August 1, 2026, 1:57 AM ET / 05:57 UTC. ClawSweeper reviewWhat this changesThe PR registers delayed post-ready gateway sidecars and startup-hook timers with the gateway generation so close cancels them before they can run into an in-process replacement gateway. Merge readiness⛔ Blocked until real behavior proof is added - 3 items remain Keep this maintainer-labeled PR open: the lifecycle fix follows the existing gateway close-owner boundary and has focused test coverage, but it still needs inspectable after-fix runtime proof before merge. Priority: P2 Review scores
Verification
How this fits togetherGateway startup schedules post-ready maintenance, cleanup, and internal-hook work after the server is ready. The gateway lifecycle stores those handles and stops them during close, while the SIGUSR1 run loop starts the next gateway in the same process. flowchart LR
A[Gateway ready] --> B[Post-ready sidecars]
B --> C[Delayed hook and sentinel timers]
C --> D[Gateway close or SIGUSR1 restart]
D --> E[Registered stop handles]
E --> F[Old generation work cancelled]
F --> G[Replacement gateway]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +46, Tests +60. Total +106 across 2 files. View PR surface stats
Review metricsNone. Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep the generation-owned timer and sidecar registration approach, then attach redacted shutdown or SIGUSR1 restart evidence demonstrating that the old generation cannot execute delayed startup work after close. Do we have a high-confidence way to reproduce the issue? Yes, at source level: start post-ready work, begin close before its deferred callback fires, then advance the timer or initiate SIGUSR1 restart and verify no callback reaches the old generation. The PR's focused fake-timer regression test exercises that cancellation boundary, but this review did not execute it. Is this the best way to solve the issue? Yes. Registering each delayed callback with the existing post-ready lifecycle owner is narrower and more maintainable than adding downstream guards or allowing old-generation timers to survive close. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 0cd33bad88c7. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
Runtime proof: close racing the deferred timers — scheduled callbacks stay suppressed (redacted transcript)Real-behavior proof per review: ephemeral gateway (integration build containing this PR; isolated state dir, loopback) killed with SIGTERM 40 ms after ready — inside both deferred windows this PR governs (the 250 ms Two suppression facts in one artifact:
Complementing the earlier steady-state proof on the same build (clean 44 ms shutdown with zero post-close task output after the timers had already fired), and the fake-timer regression tests asserting close-before-fire for all three handles. 🤖 Generated with Claude Code |
|
Merged via squash.
|
* fix(gateway): stop post-ready sidecar timers on close * fix(gateway): return promise from generation timer admission callback
What Problem This Solves
Gateway post-ready background work could outlive the gateway that scheduled it. In
src/gateway/server-startup-post-attach.ts:schedulePostReadySidecarTaskcall sites (sidecars.session-locks,sidecars.restart-sentinel) discarded their returned{stop}handles, making them unreachable fromstopRegisteredPostReadySidecars— the tasks kept running after gateway close.scheduleRestartSentinelWakeAfterReadyfired a bare 750 mssetTimeoutwith no retained handle, nounref(), and no closing check — launched from one of the discarded sidecars, it is concrete "background work that begins after shutdown".gateway:startupinternal hook fired via an unretained 250 mssetTimeoutwith no closing guard.This matters because SIGUSR1 restarts a fresh gateway in the same process (
src/cli/gateway-cli/run-loop.ts): old-generation timers must not fire into the replacement instance.Additionally, the
agentRuntimePluginPrewarmoption was dead config:finishGatewayStartup(the sole production caller) never passed it, so the guard?.enabled !== falsewas always true.Why This Change Was Made
All post-ready work now belongs to the gateway generation that scheduled it: the two discarded handles are registered in
postReadySidecars, and both raw timers go through a small sharedscheduleGatewayGenerationTimerhelper (retained handle,unref, stopped-check, cancel-on-close). The deadagentRuntimePluginPrewarmoption is deleted rather than kept: prewarm behavior is unchanged (still enabled, same config fallback chain) — the option provably had no production caller onorigin/main, andstartGatewayPostAttachRuntimeis gateway-internal, not shipped SDK.Structured review raised preserving the option's opt-out; rejected after verification: zero production callers exist, the repo's policy is to remove stale internal args cleanly, and hypothetical callers do not warrant compat.
User Impact
No behavior change during normal operation. On shutdown and in-process restart (SIGUSR1), stale session-lock cleanup, restart-sentinel work, and the startup hook can no longer execute into a closing or replacement gateway — eliminating a class of post-shutdown races.
Evidence
node scripts/run-vitest.mjs src/gateway/server-startup-post-attach.test.ts— 63/63 pass (also run as autoreview parallel tests), including new close-before-fire coverage for all three handles.setTimeoutblocks.🤖 Generated with Claude Code
Runtime proof (added post-review)
Close-race transcript on an integration build containing this PR: SIGTERM 40 ms after ready (inside both deferred-timer windows, with
gateway-start-hooks=scheduledrecorded), no callback output after "shutting down", and process exit in 111 ms — impossible pre-fix, when the 750 ms sentinel wake held the event loop with no handle or unref. Full transcript: #117242 (comment)