Skip to content

fix(state): retire successor readers before upgrade swap - #200656

Merged
obviyus merged 1 commit into
mainfrom
fix/state-upgrade-reader-swap
Nov 17, 2026
Merged

obviyus merged 1 commit into
mainfrom
fix/state-upgrade-reader-swap

Conversation

@obviyus

@obviyus obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Problem

When a Gateway upgrades existing state at startup (copy-and-swap from #198389), a later read through the shared-state read worker can fail with disk I/O error (ERR_SQLITE_ERROR, errcode 1802 = SQLITE_IOERR_FSTAT).

The failing component depends on which reader runs next:

  • secret activation: Startup failed: required secrets are unavailable ... disk I/O error. The Gateway does not start.
  • host-link repair: Could not repair OpenClaw host links: Error: disk I/O error. Warning only.

Fresh state (no upgrade) does not fail.

Cause

  1. Before the swap, admitConfig validates the shadow config. That reads plugin metadata from the shadow state/openclaw.sqlite through a read worker, which retains the connection (keyed by physical identity).
  2. beforeSwap closes only the predecessor (live) identity, so the shadow reader survives.
  3. The swap renames the shadow inode into the live path. The retained reader still records the shadow pathname.
  4. The upgrade left the database in rollback-journal mode; startup's first writer switches it back to WAL. The retained reader then looks for WAL/SHM at the vanished shadow pathname and fails.

Fix

In upgradeStateDirectory, close the shadow shared-state path through the existing closeOpenClawStateDatabaseByPathAsync in a finally right after config admission, before refusal cleanup or rename.

Evidence

  • Regression test state-upgrade.test.ts › "reopens shared-state workers after the copy-and-swap upgrade": old-schema state, lock inspection, shadow metadata read in admitConfig, real predecessor close in beforeSwap, swap, first writer WAL transition, then a worker read of the newly written row.
    • Red on merge base 1f75f9ce: Error: disk I/O error, { code: 'ERR_SQLITE_ERROR', errcode: 1802 }.
    • Green with the fix (12/12 in the file).
  • End to end (Gateway from source): state written by released [email protected], then the same pristine copy started on the merge base and on this branch. Both run the state upgrade. Merge base: 1 disk I/O error after the upgrade. This branch: 0.
  • tsgo:core, oxfmt, oxlint on changed files: pass.

- Config admission reads plugin metadata from the shadow state database through a read worker, which retains that connection.
- The swap renamed the shadow inode live while the reader kept the shadow pathname; after the first writer re-enabled WAL, its next read failed with SQLITE_IOERR_FSTAT (disk I/O error).
- Close the shadow shared-state path right after config admission, before refusal cleanup or rename.
@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 17, 2026
@clawsweeper

clawsweeper Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge.

What this changes

This PR closes shadow shared-state database readers after config admission and before upgrade publication or cleanup.

Example: The first Gateway start upgrades 2026.11.4 state and config admission reads plugin metadata.

  • Before: A retained shadow connection survives the rename and can fail with SQLITE_IOERR_FSTAT 1802 when startup enables WAL.
  • After: The shadow connection is retired, allowing subsequent worker reads to reopen the live database.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused implementation and regression are sound on inspection, but required shipped-entry-point and latest-stable upgrade evidence remain unverified.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: The first Gateway start after upgrading existing state can exit with a SQLite disk I/O error during secrets activation.
Reason: The maintainer-authored change restores successful startup without changing configuration, schema, retention, or admission policy.

Regression provenance

Regression provenance — verified: source commit ef930ebbad77 by Ayaan Zaidi; canonical PR #198389 (blame-to-merge-commit).

Merge readiness

⛔ Blocked before merge - 1 item remains

Keep open: this maintainer-authored PR fixes a concrete reader-lifecycle defect still present on main; no introduced code defect was found.

Priority: P1
Reviewed head: 00c34fd70e3d17895385602213382d7649a5518a

Before merge

  • Add data-model compatibility proof - The review found that existing stored data may not work after upgrade. Show that existing data still loads and works with this change.

Findings

None.

Tests

  • Missing end-to-end proof: Provide redacted terminal output or logs showing the first patched Gateway start reaches readiness and reads persisted state after the reported upgrade, plus latest-stable 2026.11.5 upgrade compatibility.
Agent review details

How this fits together

Gateway startup copies outdated state into a shadow directory, runs migrations and config admission, then publishes the upgraded files; shared-state workers must release shadow connections before those files move.

flowchart TD
A[Gateway startup] --> B[Copy and migrate shadow state]
B --> C[Admit shadow config]
C --> D[Retire shadow readers]
D --> E[Publish upgrade files]
E --> F[Open live state workers]
Loading

Technical review

Best possible solution:

Retire shadow readers through the existing cleanup owner before publication, and coordinate with #200596 so one repair lands.

Do we have a high-confidence way to reproduce the issue?

The production admission callback can retain metadata readers, the reader cache follows physical identity across rename, and the added regression reproduces that sequence; no target code was executed during this read-only review.

Is this the best way to solve the issue?

This is the smallest complete lifecycle repair: it reuses the existing path-close owner, covers admission success and failure, and adds no parallel state or recovery mechanism.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 20248a9ec7c3.

Provenance checked

  • Shadow config admission before upgrade publication keeps the original intent (ef930eb: Reject invalid upgraded config before publishing files, preserving the previous installation's live state.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Ayaan Zaidi: Raw commit ef930eb adds src/state/state-upgrade.ts:571 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: ef930ebbad77; files: src/state/state-upgrade.ts)
  • Peter Steinberger: Raw commit 618bf31 adds src/state/state-upgrade.ts:578 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 618bf31e4e84; files: src/state/state-upgrade.ts)

PR surface

Source +8, Tests +72. Total +80 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 9 1 +8
Tests 1 73 1 +72
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 82 2 +80

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Root-cause cluster

Relationship: same_root_cause
Canonical: #200583
Summary: The canonical report and both repair PRs identify retained shadow readers reused after copy-and-swap publication.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

  • add P1: The evidenced upgrade regression can terminate the first Gateway start during secrets activation, interrupting service until another start.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.

Label justifications:

  • P1: The evidenced upgrade regression can terminate the first Gateway start during secrets activation, interrupting service until another start.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Attach the Gateway startup and upgrade-compatibility evidence described above.

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 execution lanes are retired.

Reviewed November 17, 2026, 10:29 AM ET / 15:29 UTC.

@obviyus
obviyus merged commit f8038cd into main Nov 17, 2026
125 checks passed
@obviyus
obviyus deleted the fix/state-upgrade-reader-swap branch November 17, 2026 15:32
@obviyus

obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Landed as f8038cd.

  • Bug: after the startup copy-and-swap state upgrade (feat(state): upgrade state by copy and swap at Gateway startup #198389), a reader retained on the shadow openclaw.sqlite during config admission survived the rename. Once the first writer re-enabled WAL, its next read failed with disk I/O error (SQLITE_IOERR_FSTAT), fatal when secret activation read first.
  • Fix: close the shadow state path right after config admission, before refusal cleanup or rename.
  • Proof: regression test red on the merge base with errcode 1802 and green with the fix; Gateway from source on state written by [email protected] logged one disk I/O error on the merge base and none with the fix; local ClawSweeper review clean on this head; CI green.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 18, 2026
…0656)

## Problem

When a Gateway upgrades existing state at startup (copy-and-swap from openclaw#198389), a later read through the shared-state read worker can fail with `disk I/O error` (`ERR_SQLITE_ERROR`, errcode 1802 = `SQLITE_IOERR_FSTAT`).

The failing component depends on which reader runs next:

- secret activation: `Startup failed: required secrets are unavailable ... disk I/O error`. The Gateway does not start.
- host-link repair: `Could not repair OpenClaw host links: Error: disk I/O error`. Warning only.

Fresh state (no upgrade) does not fail.

## Cause

1. Before the swap, `admitConfig` validates the shadow config. That reads plugin metadata from the **shadow** `state/openclaw.sqlite` through a read worker, which retains the connection (keyed by physical identity).
2. `beforeSwap` closes only the predecessor (live) identity, so the shadow reader survives.
3. The swap renames the shadow inode into the live path. The retained reader still records the shadow pathname.
4. The upgrade left the database in rollback-journal mode; startup's first writer switches it back to WAL. The retained reader then looks for WAL/SHM at the vanished shadow pathname and fails.

## Fix

In `upgradeStateDirectory`, close the shadow shared-state path through the existing `closeOpenClawStateDatabaseByPathAsync` in a `finally` right after config admission, before refusal cleanup or rename.

## Evidence

- Regression test `state-upgrade.test.ts` › "reopens shared-state workers after the copy-and-swap upgrade": old-schema state, lock inspection, shadow metadata read in `admitConfig`, real predecessor close in `beforeSwap`, swap, first writer WAL transition, then a worker read of the newly written row.
  - Red on merge base `44ee25b8`: `Error: disk I/O error`, `{ code: 'ERR_SQLITE_ERROR', errcode: 1802 }`.
  - Green with the fix (12/12 in the file).
- End to end (Gateway from source): state written by released `[email protected]`, then the same pristine copy started on the merge base and on this branch. Both run the state upgrade. Merge base: 1 `disk I/O` error after the upgrade. This branch: 0.
- `tsgo:core`, oxfmt, oxlint on changed files: pass.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 18, 2026
## Problem

When a Gateway upgrades existing state at startup (copy-and-swap from #198389), a later read through the shared-state read worker can fail with `disk I/O error` (`ERR_SQLITE_ERROR`, errcode 1802 = `SQLITE_IOERR_FSTAT`).

The failing component depends on which reader runs next:

- secret activation: `Startup failed: required secrets are unavailable ... disk I/O error`. The Gateway does not start.
- host-link repair: `Could not repair OpenClaw host links: Error: disk I/O error`. Warning only.

Fresh state (no upgrade) does not fail.

## Cause

1. Before the swap, `admitConfig` validates the shadow config. That reads plugin metadata from the **shadow** `state/openclaw.sqlite` through a read worker, which retains the connection (keyed by physical identity).
2. `beforeSwap` closes only the predecessor (live) identity, so the shadow reader survives.
3. The swap renames the shadow inode into the live path. The retained reader still records the shadow pathname.
4. The upgrade left the database in rollback-journal mode; startup's first writer switches it back to WAL. The retained reader then looks for WAL/SHM at the vanished shadow pathname and fails.

## Fix

In `upgradeStateDirectory`, close the shadow shared-state path through the existing `closeOpenClawStateDatabaseByPathAsync` in a `finally` right after config admission, before refusal cleanup or rename.

## Evidence

- Regression test `state-upgrade.test.ts` › "reopens shared-state workers after the copy-and-swap upgrade": old-schema state, lock inspection, shadow metadata read in `admitConfig`, real predecessor close in `beforeSwap`, swap, first writer WAL transition, then a worker read of the newly written row.
  - Red on merge base `1f75f9ce`: `Error: disk I/O error`, `{ code: 'ERR_SQLITE_ERROR', errcode: 1802 }`.
  - Green with the fix (12/12 in the file).
- End to end (Gateway from source): state written by released `[email protected]`, then the same pristine copy started on the merge base and on this branch. Both run the state upgrade. Merge base: 1 `disk I/O` error after the upgrade. This branch: 0.
- `tsgo:core`, oxfmt, oxlint on changed files: pass.

Co-authored-by: Ayaan Zaidi <[email protected]>

Release adaptation retains existing broader deletion tests; adds only the new reader-replacement regression.

(cherry picked from commit f8038cd)

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant