Skip to content

fix(gateway): never leave the restart admission fence closed without a restart (permanent GatewayDrainingError wedge) - #107339

Merged
obviyus merged 1 commit into
mainfrom
fix/gateway-drain-wedge
Jul 14, 2026
Merged

obviyus merged 1 commit into
mainfrom
fix/gateway-drain-wedge

Conversation

@obviyus

@obviyus obviyus commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Fixes #107322 — a gateway can get permanently stuck in "draining" mode, rejecting every new agent task with GatewayDrainingError while never actually restarting, until an operator restarts the process. Observed in production for ~14 minutes with zero diagnostic output.

Root cause class: the reversible restart-signal admission fence (restartSignalPending in src/process/gateway-work-admission.ts) could be left closed with no live reopen path:

  • beginGatewayRestartSignalAdmission() handed concurrent callers a stand-in dead lease ({ rollback: () => false }); emitGatewayRestartWithSignalAdmission then stored it over the live lease, so every later rollback no-oped.
  • emitPreparedGatewayRestart had no try/finally around the fenced body — a throw after the fence was set skipped every explicit rollback branch, and the function's own outer catch then swallowed the error because the stuck fence itself makes isGatewayRestartDraining() true, reporting success.
  • After a delivered signal the fence is intentionally kept for the run loop, but the refused-signal reopen (clearPendingRestartSignalAdmission) depended on the possibly-dead stored lease.

All of these close admission silently — the incident produced no log line at onset, which is why the initiating emission could not be named from production logs (see the ranked RCA in #107322).

Why This Change Was Made

Invariant enforced: no admission-closing state may persist without either a completing restart (one-way drain → in-process reset) or a guaranteed reopen.

  • Single fence owner. beginGatewayRestartSignalAdmission() now returns null when the fence is already held or one-way drain is active — no stand-in leases exist anymore, so a dead handle can never displace the live one. Both emission paths (emitGatewayRestartWithSignalAdmission, emitPreparedGatewayRestart) refuse to run concurrently with another owner and recover an orphaned fence (pending with no lease and no unconsumed signal) before re-acquiring.
  • Guaranteed reopen. The fenced body of emitPreparedGatewayRestart is now try/finally: every non-delivery outcome (not idle, superseded, failed, thrown hook, cancelled deferral racing the body) rolls the fence back; only a delivered or already-in-flight signal keeps it, at which point the run loop owns reopen-or-drain. The deferral's .catch invokes its cancel hook before clearing it, and the refused-signal path falls back to rollbackGatewayRestartSignalFence() when the stored lease is dead.
  • Observability. Admission transitions now log once per state change — admission closed/reopened: restart-signal fence | restart drain | suspend phase (gateway/admission subsystem). The incident was undiagnosable precisely because these transitions were silent.
  • No watchdog timer. The state machine is made airtight instead of papering over it with a recovery timeout.
  • LOC ratchet: src/infra/restart.ts is an oversized legacy file, so the self-contained SQLite restart-intent persistence moved to src/infra/restart-intent.ts (mechanical move, import sites updated directly, no re-export shim): restart.ts 1303 → 1160 lines.

Alternatives rejected: a periodic self-heal/watchdog reopen (masks the state-machine hole and risks reopening mid-restart); logging-only (leaves the wedge); reverting the fence (it exists to block suspension across signal emission — contract preserved, see 1bcc4c5e706 / 3616fba951f).

User Impact

  • A failed, refused, superseded, or thrown restart emission can no longer freeze the whole gateway; admission reopens and the next restart attempt can proceed.
  • Operators get explicit admission closed:/admission reopened: log lines naming the reason, so any future admission problem is diagnosable from logs.
  • No config, API, or protocol changes. beginGatewayRestartSignalAdmission's nullable return is an internal contract; all callers updated in this PR.

Evidence

Incident RCA: #107322 (timeline, queue bracketing, ranked candidates; the stuck reversible fence is the only class fitting silent onset, zero run-loop activity, a cron heartbeat continuing under a pre-fence parent root, and recovery via process restart).

Regression tests (fail on old code):

  • src/process/gateway-work-admission.test.ts: second acquire returns null while held; orphan fence force-rollback reopens admission and kills stale lease handles; admission waiters wake on rollback.
  • src/infra/infra-runtime.test.ts: refused-handler rollback with no live lease reopens admission; deferred emission cancelled mid-prepare does not leave admission closed; deferred emission racing config supersession keeps admission open.
  • src/cron/service.every-jobs-fire.test.ts: null-safe lease handling for the new acquire contract.

Validation: Blacksmith Testbox (tbx_01kxfx7m023xz53275mnmt2tte, synced from this branch): pnpm check:changed all lanes green, plus the lifecycle test sweep (src/process, src/infra, src/cli/gateway-cli, src/cli/daemon-cli, config-reload, server-reload, cron interval tests): all green (exit 0; check lanes incl. LOC ratchet, format, tsgo typecheck core+tests, lint core, Plugin SDK baseline; 4 Vitest shards passed covering the full src/infra, src/process, gateway-cli and daemon-cli suites). Structured second-model review (codex): one accepted P2 in an intermediate revision (cancelled deferral could reopen admission over an in-flight SIGUSR1 — fixed by preserving the fence while a signal is unconsumed, with a regression test); final pass clean, "patch is correct", no actionable findings.

Sibling surfaces checked: the suspend-phase lease already had TTL auto-resume and invalidation (unchanged); one-way restartDraining is run-loop-owned and resets via in-process restart (unchanged, now logged); WS RPC and cron continuation admission paths unchanged.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime cli CLI command changes size: L maintainer Maintainer-authored PR labels Jul 14, 2026
@obviyus
obviyus force-pushed the fix/gateway-drain-wedge branch from f6b6ee6 to 1254002 Compare July 14, 2026 10:27
@openclaw-barnacle openclaw-barnacle Bot added the scripts Repository scripts label Jul 14, 2026
@obviyus
obviyus force-pushed the fix/gateway-drain-wedge branch from 1254002 to 3d1f5ec Compare July 14, 2026 10:38
…a restart

A failed, refused, superseded, or thrown restart emission could leave the
reversible restart-signal admission fence closed forever: concurrent emitters
could overwrite the live rollback lease with a dead stand-in, the fenced body
had no try/finally, and the outer catch swallowed errors precisely because the
stuck fence made isGatewayRestartDraining() true. The gateway then rejected
every new task with GatewayDrainingError - silently - until an operator
restarted the process.

beginGatewayRestartSignalAdmission now returns null instead of stand-in
leases (single fence owner), emitPreparedGatewayRestart reopens the fence on
every non-delivery path via try/finally while preserving it whenever a queued
SIGUSR1 is unconsumed, refused-signal cleanup force-clears orphaned fences,
and admission close/reopen transitions are logged with their reason. The
self-contained SQLite restart-intent persistence moves to restart-intent.ts
to keep restart.ts within the LOC ratchet.

Fixes #107322
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes gateway Gateway runtime maintainer Maintainer-authored PR scripts Repository scripts size: L

Projects

None yet

1 participant