Repository navigation
fix(gateway): never leave the restart admission fence closed without a restart (permanent GatewayDrainingError wedge) - #107339
Merged
Merged
Conversation
obviyus
force-pushed
the
fix/gateway-drain-wedge
branch
from
July 14, 2026 10:27
f6b6ee6 to
1254002
Compare
obviyus
force-pushed
the
fix/gateway-drain-wedge
branch
from
July 14, 2026 10:38
1254002 to
3d1f5ec
Compare
…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
obviyus
force-pushed
the
fix/gateway-drain-wedge
branch
from
July 14, 2026 10:42
3d1f5ec to
252cf15
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Problem This Solves
Fixes #107322 — a gateway can get permanently stuck in "draining" mode, rejecting every new agent task with
GatewayDrainingErrorwhile 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 (
restartSignalPendinginsrc/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 });emitGatewayRestartWithSignalAdmissionthen stored it over the live lease, so every later rollback no-oped.emitPreparedGatewayRestarthad no try/finally around the fenced body — a throw after the fence was set skipped every explicit rollback branch, and the function's own outercatchthen swallowed the error because the stuck fence itself makesisGatewayRestartDraining()true, reporting success.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.
beginGatewayRestartSignalAdmission()now returnsnullwhen 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.emitPreparedGatewayRestartis 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.catchinvokes its cancel hook before clearing it, and the refused-signal path falls back torollbackGatewayRestartSignalFence()when the stored lease is dead.admission closed/reopened: restart-signal fence | restart drain | suspend phase(gateway/admissionsubsystem). The incident was undiagnosable precisely because these transitions were silent.src/infra/restart.tsis an oversized legacy file, so the self-contained SQLite restart-intent persistence moved tosrc/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
admission closed:/admission reopened:log lines naming the reason, so any future admission problem is diagnosable from logs.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:changedall 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
restartDrainingis run-loop-owned and resets via in-process restart (unchanged, now logged); WS RPC and cron continuation admission paths unchanged.