Skip to content

fix: stop updates failing with "database is locked" when another OpenClaw process is writing - #200742

Merged
steipete merged 2 commits into
mainfrom
codex/issue-197083-lock
Nov 18, 2026
Merged

steipete merged 2 commits into
mainfrom
codex/issue-197083-lock

Conversation

@steipete

@steipete steipete commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Related: #197083

What Problem This Solves

Updating OpenClaw can fail at the very end with "database is locked" even though the update's data migration already finished. This happens when another OpenClaw process, such as a status check, the old updater, or a CLI command, briefly holds the state database. The update then reports post-update-failed. In some runs the Gateway is also left stopped with "data at risk", and the user has to run openclaw update repair.

User Impact

User impact: updates and openclaw doctor --fix no longer fail when another OpenClaw process holds the state database for a few seconds, and a step that finished its work is no longer reported as failed.

Why This Change Was Made

Doctor's maintenance steps (media persistence, plugin session repair, transcript directives) run while holding a state lease. Releasing that lease retried SQLite lock contention for only 2 seconds, then threw the raw lock error. Every other shared-state writer waits 5 seconds. So a step whose work had already committed failed with a bare "database is locked". The lease row also stayed behind, so the next step waited on it and failed with "lease … is held by …".

Releasing a lease now waits the same 5-second busy bound as any other writer, but only for plain SQLite lock contention; offline-maintenance contention keeps its 2-second window. If a writer still holds the lock past that, lease cleanup retries in the background, as it already did for offline-maintenance contention, so an unreleased lease no longer blocks later steps until it expires.

Evidence

  • Reproduced on Crabbox AWS Linux. Each lane updated from published 2026.11.4 to main 237e3e4fe0d through the Gateway's update.run, with a 400 MB state database and an outside writer holding the write lock.
    • Writer holding 2 s out of every 3 s: 1 of 14 lanes failed. Transcript directives failed with "database is locked", ending in state-migrated-no-rollback with the Gateway stopped.
    • Writer holding 2 s out of every 2.2 s: 9 of 14 lanes failed post-update-failed, with "Plugin session repair did not settle: database is locked" or "plugin lifecycle lease … is held by …".
  • Instrumented build. In each failed lane, the same Doctor process logged the lease-release lock error about a second before the step failed.
  • Fix lanes: the same high-contention lanes on the fixed build (base 237e3e4fe0d plus this patch) passed 11 of 12, against 12 of 18 failing on base. The one remaining failure has a different cause: the old 11.4 Gateway acquired a plugin lifecycle lease during its own startup maintenance and was stopped for the update without releasing it, so main's Doctor met a lease held for up to 300 s. It is tracked as a separate follow-up.
  • Latest stable: the same lanes from published 2026.11.5 failed 4 of 4 on base. In each, the lease-release lock error was followed by "Plugin session repair did not settle" in the same Doctor process. With the fix they passed 4 of 4: core ready, Gateway active, config intact, and all 1,024 synthetic state rows present in the 425 MB database.
  • Regression tests in src/state/openclaw-state-lease-contention.test.ts:
    • A writer holding the lock longer than 2 seconds during release. Red on base with the same bare database is locked (errcode 5); green with the fix.
    • Background retry releasing the lease after the writer commits.
  • Other checks:
    • Lease suites pass, including contention, maintenance-retry, native-cleanup, and worktree capacity.
    • pnpm tsgo:core, oxlint, and oxfmt are clean.
  • Reviews: autoreview came back scoped-clean at P2. An independent codex review found nothing.
  • Size: production +16/−9; tests +97.
  • Head: rebased onto current main only to resolve an append-only conflict in the test file; the production diff is unchanged from the build the lanes ran.

@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. 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. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug 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: needs maintainer review before merge.

What this changes

This PR extends SQLite lock-contended lease release to five seconds and includes exhausted releases in the existing background cleanup.

Example: Another process holds the state database write lock for three seconds while Doctor releases its lease.

  • Before: Release exhausts its two-second budget and can report already-completed maintenance as failed.
  • After: Release waits within the five-second budget and completes after the writer releases the lock.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, correct repair supported by distinct real-SQLite regressions and a detailed public latest-stable upgrade report.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The public maintainer report maps the changed release owner to Gateway update.run under real external writer contention on AWS Linux, including latest-stable base failures and four successful patched upgrades; evidence is the published run account, not independently replayed artifacts.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: An update can report failure after Doctor's work commits and leave the Gateway stopped because lease release gives up during brief database contention.
Reason: The maintainer-authored direction addresses a reproduced update failure using existing shared owners; the broader related issue remains Related rather than a closing claim.

Merge readiness

✅ Ready for maintainer review

This PR remains useful: current main retains the shorter release budget and maintenance-only background retry; the reviewed patch fixes both without a blocking correctness or security finding.

Priority: P0
Reviewed head: 684b19ddb6ddeec19ed7ebf9d93cbfef0c2cdf44

Before merge

None.

Findings

None.

Agent review details

How this fits together

Doctor and other state operations acquire persisted leases; the shared lease owner drains work and heartbeats, then deletes its own lease through native or worker storage.

flowchart TD
  A[Doctor or state operation] --> B[Shared lease owner]
  B --> C[Operation and heartbeat settlement]
  C --> D[Bounded lease release]
  D --> E[SQLite state leases]
  D --> F[Retained background cleanup]
  F --> E
Loading

Technical review

Best possible solution:

Use the existing lease-release and retained-cleanup owners to absorb transient SQLite contention while preserving ownership, expiry, and error boundaries.

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

The source exposes the two-second release failure, the added regressions exercise real SQLite contention through the lease owner, and the public report records failing base upgrades; this review ran no target code.

Is this the best way to solve the issue?

The patch is a focused repair at the shared owner, with separate regressions for foreground release and eventual cleanup and no additional lifecycle machinery.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 00d105d9fa7c.

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned introduced change: The introduced diff changes only release timing, cleanup contention classification, and two distinct release regressions; the existing renewal regressions remain present. (src/state/openclaw-state-lease-contention.test.ts:250, 684b19ddb6dd)
  • Current-main behavior: Fetched main still uses the two-second lease-release deadline and schedules background cleanup only for maintenance contention. (src/state/openclaw-state-lease-cleanup.ts:60, 00d105d9fa7c)
  • Release boundary remains bounded: SQLite BUSY/LOCKED uses the existing five-second writer constant, maintenance keeps its two-second budget, and other errors retain their existing handling. (src/state/openclaw-state-lease-storage.ts:323, 684b19ddb6dd)
  • Cleanup retains original ownership: Cleanup joins heartbeat and worker settlement, uses the committed expiry observation, and retains deletion predicates for scope, lease key, and owner. (src/state/openclaw-state-lease-store.ts:273, 684b19ddb6dd)
  • Public update and upgrade evidence: The maintainer's public run report describes Gateway update.run on Crabbox AWS Linux under external writer load; published 2026.11.5 upgrades failed four of four on base and passed four of four with the patch, preserving config and all 1,024 synthetic rows in a 425 MB database. (684b19ddb6dd)
  • Earlier review action resolved: GitHub reports the reviewed head mergeable with a clean merge state; both renewal and release regressions are present, resolving the earlier conflict-related action. (684b19ddb6dd)

Likely related people:

  • Dallin Romney: Raw commit 06951a9 adds src/state/openclaw-state-lease-cleanup.ts:54 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 06951a90db7f; files: src/state/openclaw-state-lease-cleanup.ts)

PR surface

Source +7, Tests +97. Total +104 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 16 9 +7
Tests 1 97 0 +97
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 113 9 +104

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.

Labels

Label changes:

  • add P0: The demonstrated managed-update failure can leave the Gateway stopped and require terminal-based update repair, meeting the specified update-recovery UX override.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • remove P1: Current review triage priority is P0, so this older priority label is no longer current.
  • remove rating: 🧂 unranked krab: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P0: The demonstrated managed-update failure can leave the Gateway stopped and require terminal-based update repair, meeting the specified update-recovery UX override.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

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.

History

Review history (1 earlier review cycle)
  • reviewed 2026-11-17T18:54:42.005Z sha 8112a05 :: needs changes before merge. :: none

Reviewed November 17, 2026, 7:14 PM ET / November 18, 2026, 00:14 UTC (Revision 2).

… contention

A state lease's release gave up after 2 s of SQLite lock contention and,
for worker-cleanup leases, threw the raw lock error. Doctor maintenance
steps that had already committed their work (media persistence, plugin
session repair, transcript directives) then failed with a bare
"database is locked", leaving updates post-update-failed and sometimes
the Gateway stopped. The unreleased lease also blocked the next acquirer
until its stored expiry.

Lock contention during release now waits the same 5 s busy bound every
shared-state writer uses, and a release that still loses to a writer is
retried in the background like offline-maintenance contention.

Refs #197083
@steipete
steipete force-pushed the codex/issue-197083-lock branch from 8112a05 to 684b19d Compare November 17, 2026 20:38
@steipete
steipete marked this pull request as ready for review November 17, 2026 21:50
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T21:52:40.748874Z 684b19d Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The merge conflict is resolved: rebased onto main, keeping both main's renewal tests and this PR's release tests. The body now includes the latest-stable proof. From published 2026.11.5 under writer load, base failed 4 of 4 and the fix passed 4 of 4, with config and data intact.

@clawsweeper

clawsweeper Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed 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. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Nov 18, 2026
@steipete
steipete merged commit b1ec281 into main Nov 18, 2026
220 of 222 checks passed
@steipete
steipete deleted the codex/issue-197083-lock branch November 18, 2026 00:15
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 18, 2026
…Claw process is writing (openclaw#200742)

* fix(state): keep lease release waiting through ordinary SQLite writer contention

A state lease's release gave up after 2 s of SQLite lock contention and,
for worker-cleanup leases, threw the raw lock error. Doctor maintenance
steps that had already committed their work (media persistence, plugin
session repair, transcript directives) then failed with a bare
"database is locked", leaving updates post-update-failed and sometimes
the Gateway stopped. The unreleased lease also blocked the next acquirer
until its stored expiry.

Lock contention during release now waits the same 5 s busy bound every
shared-state writer uses, and a release that still loses to a writer is
retried in the background like offline-maintenance contention.

Refs openclaw#197083

* test(state): restore the renewal test close after rebase

Co-authored-by: Peter Steinberger <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 18, 2026
…Claw process is writing (#200742)

* fix(state): keep lease release waiting through ordinary SQLite writer contention

A state lease's release gave up after 2 s of SQLite lock contention and,
for worker-cleanup leases, threw the raw lock error. Doctor maintenance
steps that had already committed their work (media persistence, plugin
session repair, transcript directives) then failed with a bare
"database is locked", leaving updates post-update-failed and sometimes
the Gateway stopped. The unreleased lease also blocked the next acquirer
until its stored expiry.

Lock contention during release now waits the same 5 s busy bound every
shared-state writer uses, and a release that still loses to a writer is
retried in the background like offline-maintenance contention.

Refs #197083

* test(state): restore the renewal test close after rebase

Co-authored-by: Peter Steinberger <[email protected]>

(cherry picked from commit b1ec281)

Release adaptation: keep the maintenance-line native/worker lease owner and
existing-schema admission behavior; import only its existing busy-timeout constant.
Resolve the append-only contention-test conflict without unrelated renewal tests.
Retain the branch native best-effort fallback; exact-worker failed cleanup remains
with the existing resource owner. No schema, SDK, config, or dependency changes.

Companion audit: no later same-path commits or PR-number follow-ups for #200742
through main 74e5127. #200328 is already
implemented at cef3501, with predecessor-stop
custody retained; do not duplicate it or the separately owned #200659/#200805.
The 97-line allocation covers two distinct real-SQLite regressions: completed
work survives ordinary contention and failed release cannot strand its lease.

Proof: both new regressions fail on 0678cec; all 19 selected lease tests
pass after repair. The four #200328 owner suites pass 94 tests (3 skipped).
Formatting and git diff --check pass. Targeted lint/type wrappers refuse shared
symlinked dependencies; final isolated checks and full release validation remain
with the release owner. No live Gateway or installed state was modified.

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

* fix(update): keep the failure that started recovery in failure reports

After any update failure, the recovery steps that follow also fail
(gateway migration recovery, package-backup-retention, gateway recovery
verification). The failure-report sanitizer kept only the latest three
failed steps and pruned oldest first, so the step that started recovery
was always dropped, and users could not report what went wrong.

Keep the first failed step alongside the latest two, and when the byte
budget forces pruning, drop middle records before it. The latest failure
still wins when only two remain.

Refs #197083

* test(update): expect the triggering failure in recovery triage prompts

The recovery triage prompt fixture has five failed steps. Failure reports now keep the first failed step with the latest two, so the oldest failure replaces the second-latest one in the expected prompt details.

Refs #197083

* fix(update): protect the latest failure when pruning failure reports

Pruning removed the second record when the trigger came first, so a retained lint inventory record after the latest failure made the latest failure the one dropped. Protect the trigger and the latest failure by identity, drop other records first, and drop the trigger before the latest failure.

Refs #197083

(cherry picked from commit 5df28e7)

Backport review: source applies unchanged. #200322 is orthogonal SQLite diagnostic enrichment; #200742 repairs a separate lease owner. Both are outside this report-retention fix.

Co-authored-by: RomneyDa <[email protected]>
Co-authored-by: vincentkoc <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 19, 2026
…Claw process is writing (#200742)

* fix(state): keep lease release waiting through ordinary SQLite writer contention

A state lease's release gave up after 2 s of SQLite lock contention and,
for worker-cleanup leases, threw the raw lock error. Doctor maintenance
steps that had already committed their work (media persistence, plugin
session repair, transcript directives) then failed with a bare
"database is locked", leaving updates post-update-failed and sometimes
the Gateway stopped. The unreleased lease also blocked the next acquirer
until its stored expiry.

Lock contention during release now waits the same 5 s busy bound every
shared-state writer uses, and a release that still loses to a writer is
retried in the background like offline-maintenance contention.

Refs #197083

* test(state): restore the renewal test close after rebase

Co-authored-by: Peter Steinberger <[email protected]>
(cherry picked from commit b1ec281)
Release adaptation: Preserve baseline lease contention tests and append only the two source regressions; omit unrelated prior renewal tests from conflict context.:
Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 1d229c5e82fc20ca32c0c8625942391dfa0101f3)
tideclaw-bot added a commit that referenced this pull request Nov 24, 2026
…Claw process is writing (#200742)

Baseline adaptation: Use the .33 SQLite busy timeout for lock-contended release and retain aggregate-aware cleanup retry classification with exact lease ownership.

(cherry picked from commit b1ec281)

Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 465a2c0b9aba69a4187ee6366f83af0a3b9c7b59)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. 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