Skip to content

fix(gateway): own restart signals in the run-loop context - #169167

Merged
steipete merged 2 commits into
mainfrom
codex/stab-inprocess-restart-lock
Oct 11, 2026
Merged

steipete merged 2 commits into
mainfrom
codex/stab-inprocess-restart-lock

Conversation

@steipete

@steipete steipete commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

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 with failed 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.ts now 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

  • Regression src/cli/gateway-cli/run-loop.state-ownership.test.ts drives 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.
  • Live A/B (dev Gateway, real OpenAI provider, two operators creating/deleting agents concurrently to trigger the reload-recovery restart):
    • main c2e1f0f60557: "restarting gateway now" → in-process restart → failed to reacquire gateway lock → exit 1.
    • this branch: "restarting gateway now" → in-process restart → listener bound again 7 s later, 0 reacquire failures, Gateway serving.
  • Production +11/−7. Tests +126/−2. Docs: docs/gateway/gateway-lock.md (+2). The branch merges current main once, to pick up the unrelated build fix 38f456226f62.
  • Not addressed here: why overlapping agent edits request this restart at all (tracked separately).

Landing verification (2026-10-11)

ClawSweeper completed review of 569e63eaa37c826f7a95c72eec7578e5e3a0770b with no findings; independent Codex autoreview also found no actionable defects. CI run 38148891310, attempt 1, tested merge 9ad246fd6031568b36e358dd159ffed7df441c5e over main 4f4741bfe2b6b49e75af89107be87224b5fcfc9e. 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:

  • checks-node-changed-1: all 17 cases in src/cli/gateway-cli/pre-bootstrap.process.test.ts fail with ERR_MODULE_NOT_FOUND for worker-heap-flag.mjs. The fixture in doctor-config-preflight.process.test-support.ts omits that root module, imported by worker-cpu.ts since main commit 5ed6a305669a. The same 17 failures reproduce on main 4f4741bfe2b6 with pnpm test src/cli/gateway-cli/pre-bootstrap.process.test.ts --maxWorkers=1 (38.67 s); failure occurs during bootstrap import, before changed signal handling.
  • check-test-types-core-3: test/scripts/managed-child-process.windows.test.ts:83 references testing.stopGatewayProcess, removed from test/helpers/openclaw-test-instance.ts by main commit ebdc7d54c084. The same TS2339 reproduces on that main baseline with node 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.

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.
@clawsweeper

clawsweeper Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review in progress

ClawSweeper is reviewing this revision. This supersedes any previous blocked status.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime cli CLI command changes size: S labels Oct 11, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 11, 2026
@clawsweeper

clawsweeper Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This 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.

  • Before: Shutdown closes the reload scope, lock reacquisition inherits its aborted signal, and the Gateway exits with code 1.
  • After: Restart handling retains the loop’s context and can reacquire state ownership after the reload scope closes.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The focused patch appears correct and has meaningful owner-boundary coverage; independently inspectable shipped-entry-point evidence is limited.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the ordinary contributor proof gate; the real-owner regression supports the change, while the described development-Gateway A/B is reported rather than independently captured evidence.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A Gateway restarting after hot-reload recovery can exit instead of returning to service.
Reason: The maintainer-authored direction addresses an evidenced availability failure through the existing lifecycle owner with a small production change.

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
Reviewed head: 569e63eaa37c826f7a95c72eec7578e5e3a0770b

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The regression uses a fixture server; the reported live CLI reload-recovery A/B has no attached runtime capture.
Agent review details

How this fits together

The 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]
Loading

Technical review

Best 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

  • Signal consumption and shutdown settlement keeps the original intent (40d57f8: Move restart-intent work into workers while retaining ordered signal consumption, current authority, and accepted-work settlement.)
  • In-process restart lock-reacquisition failure keeps the original intent (9b3f350: Preserve foreground-update ownership and recovery when runtime files are replaced; the error branch reports reacquisition failure and terminates the loop.)
  • Lock-reacquisition regression test keeps the original intent (edaa5ef: Simplify restart flow and expand lock tests; the existing failure-outcome assertion remains while diagnostics now cover nested causes.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

  • Verified PR scope: The pinned introduction diff changes signal context binding, error formatting, focused regressions, documentation, and test routing; unrelated base movement is excluded. (src/cli/gateway-cli/run-loop-signals.ts:174, 569e63eaa37c)
  • Current-main failure path: Current main registers unbound signal handlers. Owner-lease inspection combines getAsyncWorkSignal() with its cancellation signal and rejects aborted work, supporting the reported emitter-scope failure. (src/infra/gateway-owner-lease.ts:98, 4f4741bfe2b6)
  • Meaningful regression coverage: The new test uses real signal admission, run-loop handling, lock acquisition, lease publication, and worker cleanup; it checks retained custody during delayed close and a fresh owner after the emitter scope retires. (src/cli/gateway-cli/run-loop.state-ownership.test.ts:42, 569e63eaa37c)
  • Existing ownership intent preserved: The ordered signal-consumption and settlement owner introduced in fix(gateway): settle restart intents and update reports in workers #167572 remains intact, including iteration checks, restart authorization, and shutdown joins. (src/cli/gateway-cli/run-loop-signals.ts:105, 40d57f8dc8f5)
  • Reported runtime evidence: The complete PR body reports a failing base regression, passing candidate suites, and a live development-Gateway A/B ending in a rebound listener on the candidate; discussion supplies no attached runtime capture. (569e63eaa37c)
  • History and checkout limits: History searches covered signal extraction, earlier SIGUSR1 handling, restart refactors, and the former docs/gateway-lock.md path. Some historical content traversal reached unavailable local objects; current source and the introduced diff were readable. The checkout remained clean and diff whitespace validation passed. (569e63eaa37c)

Likely related people:

  • Peter Steinberger: Raw commit 40d57f8 adds src/cli/gateway-cli/run-loop-signals.ts:166 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 40d57f8dc8f5; files: src/cli/gateway-cli/run-loop-signals.ts)
  • Shakker: Raw commit e123606 adds src/cli/gateway-cli/run-loop.ts:340 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: low; commits: e123606b46b5; files: src/cli/gateway-cli/run-loop.ts)

PR surface

Source +3, Tests +125, Docs +2. Total +130 across 6 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 2 10 7 +3
Tests 3 127 2 +125
Docs 1 2 0 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 139 9 +130

Labels

Label changes:

  • add P1: A config-recovery restart can stop an unmanaged Gateway permanently, interrupting its agent and channel workflows.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P1: A config-recovery restart can stop an unmanaged Gateway permanently, interrupting its agent and channel workflows.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/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.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only; repair and merge need explicit maintainer commands such as @clawsweeper autofix or @clawsweeper automerge.

Reviewed October 11, 2026, 10:56 AM ET / 14:56 UTC.

@steipete

Copy link
Copy Markdown
Contributor Author

@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.

@clawsweeper

clawsweeper Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

@clawsweeper maintainer-approve-automerge is retired; ClawSweeper now only reviews.

@steipete
steipete merged commit 80e1084 into main Oct 11, 2026
134 of 140 checks passed
@steipete
steipete deleted the codex/stab-inprocess-restart-lock branch October 11, 2026 15:11
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 12, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes docs Improvements or additions to documentation gateway Gateway runtime P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant