Repository navigation
fix(gateway): own restart signals in the run-loop context - #169167
Conversation
A config reload can emit SIGUSR2 inside a temporary async work scope. Shutdown retires that scope, so inherited cancellation made the next Gateway owner-lease inspection fail after a successful server close. Bind lifecycle signal handlers to the loop that owns their completion, and report nested causes when state-lock reacquisition fails. Prove delayed shutdown retains exclusive state custody and the real SIGUSR2 loop reacquires a fresh owner after its emitter closes. The regression fails on the base and passes with this fix. Unmanaged restarts retain the same process; update handoffs, contention, schemas, and persisted state keep their existing contracts.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review in progressClawSweeper is reviewing this revision. This supersedes any previous blocked status. |
|
Codex review: needs maintainer review before merge. What this changesThis PR binds Gateway signal handling to the run loop’s async context and includes nested causes in lock-reacquisition errors. Example: A foreground Gateway restarts after hot-reload recovery for a prepared model runtime change.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open: this maintainer-authored PR repairs a concrete restart failure still present on current main; no actionable introduced defects were found. Priority: P1 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherThe CLI run loop consumes process signals, drains and closes the current Gateway, then transfers ownership or reacquires the state lock before starting another server generation. flowchart TD
A[Config reload recovery] --> B[Restart signal admission]
B --> C[Loop bound signal handler]
C --> D[Drain and close server]
D --> E[Reacquire state ownership]
E --> F[Start next Gateway generation]
Technical reviewBest possible solution: Keep restart completion under the existing run-loop owner while preserving signal authorization, shutdown settlement, and exclusive state custody. Do we have a high-confidence way to reproduce the issue? Source inspection establishes the synchronous signal-emission path, inherited async-work cancellation, and post-close lease inspection; the author reports a failing baseline regression, but this read-only review executed no target code. Is this the best way to solve the issue? Binding the existing signal handlers at loop construction fixes lifetime ownership directly without retries, relaxed lock checks, or a competing restart path. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 4f4741bfe2b6. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +3, Tests +125, Docs +2. Total +130 across 6 files. Added test files: 1. View PR surface stats
LabelsLabel 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 Reviewed October 11, 2026, 10:56 AM ET / 14:56 UTC. |
|
@clawsweeper approve Maintainer decision (coordinator for Peter, 2026-10-11): bug fix - config-triggered in-process restarts exited instead of restarting; no change to lock contention or managed restarts. |
|
|
|
Merged via explicitly authorized admin squash with attributed pre-existing CI failures.
|
…69167) A config reload can emit SIGUSR2 inside a temporary async work scope. Shutdown retires that scope, so inherited cancellation made the next Gateway owner-lease inspection fail after a successful server close. Bind lifecycle signal handlers to the loop that owns their completion, and report nested causes when state-lock reacquisition fails. Prove delayed shutdown retains exclusive state custody and the real SIGUSR2 loop reacquires a fresh owner after its emitter closes. The regression fails on the base and passes with this fix. Unmanaged restarts retain the same process; update handoffs, contention, schemas, and persisted state keep their existing contracts.
What Problem This Solves
A config change that the Gateway applies through hot-reload recovery ("config change requires gateway restart (hot reload recovery: prepared model runtime reload)") restarts the Gateway in-process. On main
c2e1f0f60557, that restart shut down cleanly in ~10 s, then failed 14 ms later withfailed to reacquire gateway lock for in-process restart: GatewayLockError: failed to acquire gateway state ownership, and the process exited with code 1. An unmanaged Gateway (started from a terminal or a custom supervisor) was simply down. The logged error omitted its cause.Why This Change Was Made
No lock holder had leaked. The SIGUSR2 restart request carried the scope of the config reload that requested it into restart handling. Shutdown closes that scope, and the owner-lease inspection during reacquisition (
src/infra/gateway-owner-lease.ts) inherited its already-aborted signal and refused. A plain restart-required change (outside the reload-recovery scope) was unaffected, which is why this only appeared on the recovery path.src/cli/gateway-cli/run-loop-signals.tsnow runs restart handling under the Gateway loop's own lifetime, so closing the reload or request that triggered a restart cannot cancel reacquisition. Reacquisition errors now log their nested cause (run-loop.ts). Shutdown drainage, contention handling against a genuinely different owner, and the unmanaged in-process restart (PID preserved) are unchanged.User Impact
A Gateway that restarts itself after a config change comes back instead of exiting. If reacquisition does fail, the log now says why. Update/restart behavior: in-process restarts triggered by config reload recovery complete; managed (service) restarts are unchanged.
Evidence
src/cli/gateway-cli/run-loop.state-ownership.test.tsdrives the real run loop, signal handlers, and gateway-lock owner lease. It fails on base production code (23.16 s run) and passes after. Four scoped suites: 225 tests pass (144 s). Core and CLI test types, lint, format, cycles, docs links, and mock exports pass; autoreview clean.c2e1f0f60557: "restarting gateway now" → in-process restart →failed to reacquire gateway lock→ exit 1.docs/gateway/gateway-lock.md(+2). The branch merges current main once, to pick up the unrelated build fix38f456226f62.Landing verification (2026-10-11)
ClawSweeper completed review of
569e63eaa37c826f7a95c72eec7578e5e3a0770bwith no findings; independent Codex autoreview also found no actionable defects. CI run 38148891310, attempt 1, tested merge9ad246fd6031568b36e358dd159ffed7df441c5eover main4f4741bfe2b6b49e75af89107be87224b5fcfc9e. Restart test shard passed all seven invocations; the new ownership regression took 2.069 s (12.35 s including its wrapper).CI is red for two independently reproduced failures inherited from main, in files this PR does not change. Landing uses the maintainer-authorized native pre-existing-failure exception:
src/cli/gateway-cli/pre-bootstrap.process.test.tsfail withERR_MODULE_NOT_FOUNDforworker-heap-flag.mjs. The fixture indoctor-config-preflight.process.test-support.tsomits that root module, imported byworker-cpu.tssince main commit5ed6a305669a. The same 17 failures reproduce on main4f4741bfe2b6withpnpm test src/cli/gateway-cli/pre-bootstrap.process.test.ts --maxWorkers=1(38.67 s); failure occurs during bootstrap import, before changed signal handling.test/scripts/managed-child-process.windows.test.ts:83referencestesting.stopGatewayProcess, removed fromtest/helpers/openclaw-test-instance.tsby main commitebdc7d54c084. The same TS2339 reproduces on that main baseline withnode scripts/run-tsgo.mjs -p test/tsconfig/tsconfig.test.root.scripts.json.The failing inputs are byte-identical between main baseline and tested merge. Security-fast passed; no jobs were cancelled. The CI gate reflects the two failures above, not passing CI. The live A/B in the original evidence remains author-reported without an attached runtime capture.