Repository navigation
fix: recover Gateway ownership after sleep and clock changes - #170790
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 36de469a2d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| Atomics.store(lastBeat, 0, BigInt(renewalStartedAt)); | ||
| return 60_000; |
There was a problem hiding this comment.
Restore worker renewal after parent recovery
When the heartbeat worker exits cleanly or stops after a transient renewal failure, this recovery updates lastBeat once but never starts a replacement worker, leaving all subsequent renewal to the 60-second main-thread watcher. A later synchronous main-thread stall can therefore exceed the 90-second stale threshold, allowing a foreign-namespace process to reclaim the lock while database workers still consider this Gateway authoritative; restart the worker after successful physical recovery, or fence instead of permanently degrading to main-thread renewal.
AGENTS.md reference: AGENTS.md:L70-L70
Useful? React with 👍 / 👎.
Reverify physical lock custody before renewing expired or future heartbeat stamps. Preserve authorized restart recovery when the fenced state database cannot supply the durable restart request, and name actual ownership loss. Fixes #170525. Fixes #170597. Fixes #169829. Supersedes #170020, thanks @jjjhenriksen. Release notes: Gateway state ownership survives host sleep and backward clock changes while the original locks remain held; real ownership loss can recover through an authorized restart even when the state database is fenced. Co-authored-by: Jacqueline Henriksen <[email protected]>
36de469 to
03a625d
Compare
Portable workers start the state-owner heartbeat during SQLite admission, but the archive omitted its entry. Build and stage the existing worker, register its sealed sibling URL, and resolve that URL through the existing runtime owner. Keep custody and error handling intact. The existing relocated authorization regression now uses a dist/worker path and existing artifact fixtures include the heartbeat. Culprit: 99fccf4 (#164236), exposed by d0554f4 (#170790). Scoped Codex review and changed-file format/lint checks pass; focused hosted execution is pending.
…backport The #170790 backport (b085d30) is a faithful port: its gateway-state-owner*.ts hunks match main d0554f4 line for line. It deliberately renamed local ownership-loss errors from "could not be verified; retry after maintenance finishes" to "was lost ...; restart the Gateway", and main updated its surviving expectation in gateway-state-owner.test.ts the same way. The read-window and expiry timing are unchanged. Two release-only suites still asserted the old text. Main no longer has them: #168859 (19fe879) removed the read-verification window and gateway-state-owner.read.test.ts, and #166619 (c9acd71) deleted openclaw-state-read-worker.ownership.test.ts. Neither change was backported. Point those assertions at the #170790 wording and keep their timing and no-dispatch checks. The expired-read path also interpolated the recorded loss Error into a second "; restart the Gateway." template, which printed "was lost: Error: <lock>: ...; restart the Gateway.; restart the Gateway.". Rethrow a recorded loss unwrapped, as the strict access path already does. The queued-read regression anchors the message end, so it fails on the doubled text. Main d0554f4 has the same doubled-message bug.
…170790) The state-owner heartbeat fenced on wall-clock age alone, so macOS lid-close sleep, DarkWake mid-startup, or a WSL2 backward clock step made a live Gateway fence itself out of its own state DB; on WSL2 the SIGUSR2 restart then could not read its intent from the fenced DB and only logged, leaving a Gateway that answered /health while every DB-backed job failed. Both heartbeat paths now re-verify the held lock file (nlink, dev/ino, exact bytes) after an expired or future timestamp and renew an unchanged lock; removed or replaced locks still fence. State-owner reclaim still requires a definitely-dead owner PID, so renewal cannot split ownership. An authorized local restart proceeds when the durable restart intent cannot be read, and errors name ownership loss and say to restart the Gateway. Fixes #170525. Fixes #170597. Fixes #169829. Supersedes #170020, thanks @jjjhenriksen. (cherry picked from commit d0554f4) Backport note: the release line has no run-loop-restart-ownership.test-support.ts (split out on main by #168587); registerGatewayRestartOwnershipTests still lives in run-loop.test-support.ts, so the moved SIGUSR2 regression and the new owner-loss restart regression were applied there. The release-only "bounded Gateway state reads" test now expects the new ownership-loss messages. The restart message in assertStateDatabaseAccessAllowed is held in a local so gateway-state-owner.ts stays within the 700-line lint limit; wording is unchanged. Co-authored-by: Jacqueline Henriksen <[email protected]>
…penclaw#170790) The state-owner heartbeat fenced on wall-clock age alone, so macOS lid-close sleep, DarkWake mid-startup, or a WSL2 backward clock step made a live Gateway fence itself out of its own state DB; on WSL2 the SIGUSR2 restart then could not read its intent from the fenced DB and only logged, leaving a Gateway that answered /health while every DB-backed job failed. Both heartbeat paths now re-verify the held lock file (nlink, dev/ino, exact bytes) after an expired or future timestamp and renew an unchanged lock; removed or replaced locks still fence. State-owner reclaim still requires a definitely-dead owner PID, so renewal cannot split ownership. An authorized local restart proceeds when the durable restart intent cannot be read, and errors name ownership loss and say to restart the Gateway. Fixes openclaw#170525. Fixes openclaw#170597. Fixes openclaw#169829. Supersedes openclaw#170020, thanks @jjjhenriksen. Co-authored-by: Jacqueline Henriksen <[email protected]>
Portable workers start the state-owner heartbeat during SQLite admission, but the archive omitted its entry. Build and stage the existing worker, register its sealed sibling URL, and resolve that URL through the existing runtime owner. Keep custody and error handling intact. The existing relocated authorization regression now uses a dist/worker path and existing artifact fixtures include the heartbeat. Culprit: 0625edd (openclaw#164236), exposed by 1fa2181 (openclaw#170790). Scoped Codex review and changed-file format/lint checks pass; focused hosted execution is pending.
Portable workers start the state-owner heartbeat during SQLite admission, but the archive omitted its entry. Build and stage the existing worker, register its sealed sibling URL, and resolve that URL through the existing runtime owner. Keep custody and error handling intact. The existing relocated authorization regression now uses a dist/worker path and existing artifact fixtures include the heartbeat. Culprit: 99fccf4 (#164236), exposed by d0554f4 (#170790). Scoped Codex review and changed-file format/lint checks pass; focused hosted execution is pending. (cherry picked from commit 9e52f76)
The release-only read-worker ownership test still expected the retired 'could not be verified; retry after maintenance' wording. #170790 changed the owner contract: a removed or replaced lock now reports ownership loss and tells the operator to restart the Gateway. Main deleted this test in #168859, so assert the new messages here instead of weakening the checks.
…penclaw#170790) The state-owner heartbeat fenced on wall-clock age alone, so macOS lid-close sleep, DarkWake mid-startup, or a WSL2 backward clock step made a live Gateway fence itself out of its own state DB; on WSL2 the SIGUSR2 restart then could not read its intent from the fenced DB and only logged, leaving a Gateway that answered /health while every DB-backed job failed. Both heartbeat paths now re-verify the held lock file (nlink, dev/ino, exact bytes) after an expired or future timestamp and renew an unchanged lock; removed or replaced locks still fence. State-owner reclaim still requires a definitely-dead owner PID, so renewal cannot split ownership. An authorized local restart proceeds when the durable restart intent cannot be read, and errors name ownership loss and say to restart the Gateway. Fixes openclaw#170525. Fixes openclaw#170597. Fixes openclaw#169829. Supersedes openclaw#170020, thanks @jjjhenriksen. Co-authored-by: Jacqueline Henriksen <[email protected]> (cherry picked from commit d0554f4) Backport adaptation: ported the fenced-intent restart fallback into the release branch's inline SIGUSR2 handler in run-loop.ts (no run-loop-signals.ts) with a run-loop.test.ts regression, and dropped the main-only session-startup-migration test race fix.
What Problem This Solves
Fixes Gateway state ownership being permanently fenced after host sleep or a backward clock change, leaving database-backed work unavailable while health checks still succeed.
Fixes #170525. Fixes #170597. Fixes #169829. Supersedes #170020, thanks @jjjhenriksen.
User Impact
Release-note context: the Gateway renews ownership after sleep and backward clock changes when its original locks remain held. Real ownership loss still fences database access, but an authorized recovery restart proceeds even when the fenced database cannot supply its restart request. Errors identify ownership loss and tell the operator to restart the Gateway.
No configuration, command-line, dependency, or database-schema changes are required. Update and Doctor maintenance retain their existing ownership and release contracts.
Why This Change Was Made
Wall-clock age does not prove that another process owns the state. The heartbeat owner now verifies the held file, inode, and acquisition bytes before renewing expired or future timestamps. Parent inspection shares the worker's physical renewal primitive; removed/replaced locks and failed renewal remain failures. Authorized local process control can recover without reading the fenced database.
A bounded fixture repair avoids loading the TypeScript handoff resolver for pure SQLite revision-probe children, which never access the handoff store. Real revision hashes and all actual handoff-consumer isolation guards remain.
The patch also fixes a startup-migration test race by checking preserved history after awaited maintenance for all three store layouts. The old asynchronous projection rebuild could race that assertion. An existing unauthorized-SIGUSR2 regression was moved into the run-loop ownership support file without removing coverage.
Production LOC: +128/-98, net +30. Test/support LOC: +272/-149, net +123. Documentation: +8. Dead time-only fencing guards and ignored diagnostic-drain bookkeeping were removed.
Evidence
git diff --checkpassed. Prior Knip scans reported no unused exports; Madge reported 0 cycles.node scripts/run-vitest.mjs src/commands/doctor-maintenance.installation.test.ts --maxWorkers=1, sequentially with identical dependencies and the parent pinned at203fefe8832af7bc18973d64d8c98e5f98277170. The clean detached base passed 21/21 in 465.01 seconds wall time. The rebased candidate initially passed 16/21 and timed out in five cases (writer-unavailable, no-consent, slow-admission, paired-device stop failure, competing update); its wrapper took 931.48 seconds. These failures are not classified as inherited.await admission: 210 real synchronous SQLite revision probes consumed 104.28 seconds before the 120-second timeout, with no parent heartbeat renewals observed. The fixture's handoff resolver was being booted for every unrelated pure probe. After removing that overhead, the entire installation file passed 21/21 in 565.93 seconds wall time (554.32 seconds Vitest duration; 95% tests). Writer-unavailable fell from 141.26 to 101.18 seconds; no-consent from 142.29 to 75.60; slow-admission from 123.73 to 72.40; paired-device stop failure from 128.51 to 85.77. The heartbeat implementation, deadlines, and assertions were unchanged during this repair. The file remains slow; broad CI is the remaining proof.