Skip to content

fix: prevent SQLite lock races in chat metadata tests - #166520

Merged
steipete merged 1 commit into
mainfrom
fix/chat-metadata-fixture-maintenance-drain
Oct 7, 2026
Merged

steipete merged 1 commit into
mainfrom
fix/chat-metadata-fixture-maintenance-drain

Conversation

@steipete

@steipete steipete commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Closes #166505.

What Problem This Solves

Fixes SQLite lock races in the chat.metadata exact/full cache tests when fixture seed writes leave automatic maintenance active.

User Impact

No production behavior changes. The metadata tests reliably exercise their intended raw-write and external-connection invalidation boundaries. Whole-owner Bun CI qualification remains a separate step.

Why This Change Was Made

Automatic maintenance can hold BEGIN IMMEDIATE while awaiting a host admission grant. The fixture's synchronous raw SQL blocks that same host while waiting for the maintenance writer's WAL lock. The retained connection waits its existing 5000 ms; a separately opened external connection has SQLite's default zero timeout and fails immediately. A controlled Linux probe reproduced both outcomes on both Bun pins and Node 24/26.

Drain setup-owned database work before reopening and warming either cache, using the same lifecycle boundary already present in this file's second test group. Assert that setup leaves no asynchronous database resources. Raw SQL, the foreign handle, and the existing metadata/authorization assertions remain intact; no timeout, retry, skip, or production change is introduced. This does not establish a link to the Windows/Feishu reply-loss report in #157939.

Evidence

  • Original Linux source 497de2e869: untouched 667c run failed raw sibling/exact and sibling cache replacement/exact with error 5 after about 5.68 seconds. The first isolated next-pin and Node controls passed, confirming timing dependence rather than proving a fix.
  • Controlled maintenance-admission probe: Bun 667c4ab22c, Bun fc53bf8c0f, Node 24.21.0, and Node 26.10.0 all fail the retained raw write after about 5006 ms and the zero-timeout external write in under 1 ms. WAL/normal mode and the separate maintenance handle are confirmed in the trace.
  • Regression negative control: removing only the drain makes the new lifecycle assertion fail on next Bun and Node 24 before the unsafe raw write.
  • Complete fixed-file Linux runs on the original source: 89/89 on all four runtimes. Process wall time: 97.6 s (667c), 96.5 s (next), 145.2 s (Node 24), 134.3 s (Node 26), including wrapper preparation. No full-run skips.
  • Final main-based candidate (191ebcef6aa14 + this patch): 89/89 on both Bun pins and Node 24, with matching candidate file checksum. Process wall time: 97.6 s (667c), 95.3 s (next), 144.6 s (Node 24).
  • node scripts/check-changed.mjs -- src/gateway/server-methods/chat-metadata-session-writes.test.ts passed, including typecheck/lint and repository guards; git diff --check passed. Independent autoreview found no actionable issues.

The focused command used the repository wrapper: node scripts/run-vitest.mjs run --config test/vitest/vitest.gateway-methods.config.ts src/gateway/server-methods/chat-metadata-session-writes.test.ts --maxWorkers=1, with OPENCLAW_VITEST_RUNTIME=bun and the verified fork binary on PATH for Bun runs, isolated state, and the same eight-CPU envelope.

Setup upserts can leave automatic maintenance holding the WAL write lock while it waits for the host's admission grant. Raw fixture writes on that same host can then block the grant. Drain setup-owned database work before warming exact or full caches, and assert that the fixture has no asynchronous database resources left.\n\nThe lock window reproduces on both Bun pins and Node 24/26. No production SQLite policy changes.
@clawsweeper

clawsweeper Bot commented Oct 7, 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.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS labels Oct 7, 2026
@steipete
steipete marked this pull request as ready for review October 7, 2026 08:17
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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. labels Oct 7, 2026
@clawsweeper

clawsweeper Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge. Reviewed October 7, 2026, 4:31 AM ET / 08:31 UTC (Revision 2).

ClawSweeper review

What this changes

The PR drains setup-owned database work before warming chat metadata caches and asserts that no asynchronous database resources remain.

Merge readiness

⛔ Needs changes before merge - 1 item remains

Keep open: this is a focused, useful fixture repair that current main does not contain. No introduced correctness or security defect was found; the prior flake-qualification requirement remains outstanding.

Priority: P2
Reviewed head: baa3475838b594e5d4c1b8aada110cac63135352

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A narrow repair with a credible root cause, negative control, and cross-runtime validation; required flake qualification is still unrecorded.
Proof confidence 🌊 off-meta tidepool Not applicable: The maintainer-authored PR changes fixture setup only; no production behavior requires demonstration. Recorded native SQLite failures, negative controls, and fixed-file runs support the repair, while repeated flake qualification remains separate. No stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The maintainer-authored PR changes fixture setup only; no production behavior requires demonstration. Recorded native SQLite failures, negative controls, and fixed-file runs support the repair, while repeated flake qualification remains separate. No stored-data contract changes.
Evidence reviewed 7 items Pinned introduced change: The merge-base-to-head patch adds only the async database close, resource assertion, and explanatory comment before reopening the fixture database. Raw mutations and metadata assertions remain intact.
Existing lifecycle owner: The async close fences the selected root, joins resource drainage and pending opens, and awaits WAL-maintenance shutdown before closing native handles. The second test group already uses this boundary before raw SQLite access.
Current main still needs the fixture repair: Fetched main has the same test file as the pinned merge base and still opens the database immediately after setup, without the added drain. The verified test-merge comparison contains only the intended fixture change.
Findings None None.
Security None None.

How this fits together

Gateway chat metadata supplies session-specific commands and model choices to clients. These tests mutate SQLite session rows during metadata preparation to verify cache freshness and access checks.

flowchart TD
  A[Seed session rows] --> B[Drain setup database work]
  B --> C[Reopen database and warm caches]
  C --> D[Prepare chat metadata]
  D --> E[Mutate session rows]
  E --> F[Check freshness and access]
  F --> G[Return metadata or reject changed access]
Loading

Before merge

  • Complete next step (P2) - Complete and record the testing guide's flake qualification: 20 clean standalone file runs, three clean original-shard runs, and owner sibling coverage; account separately for known inherited failures under repository testing policy.
Agent review details

Security

None.

PR surface

Tests +3. Total +3 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 1 3 0 +3
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 3 0 +3

Review metrics

None.

Technical review

Best possible solution:

Keep raw-write cache coverage deterministic through the existing database lifecycle owner, without changing production locking, retries, or metadata authorization.

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

Yes: the recorded maintenance-admission probe identifies the lock cycle, and current main retains the undrained fixture setup. This review inspected the source but did not execute a current-main reproduction.

Is this the best way to solve the issue?

Yes: draining setup work before cache capture repairs the fixture's lifecycle boundary while preserving its intentional raw writes and freshness checks.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 66d898f6662a.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: This repairs a bounded test-fixture race affecting runtime qualification, with no demonstrated production regression.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The maintainer-authored PR changes fixture setup only; no production behavior requires demonstration. Recorded native SQLite failures, negative controls, and fixed-file runs support the repair, while repeated flake qualification remains separate. No stored-data contract changes.

Evidence

What I checked:

  • Pinned introduced change: The merge-base-to-head patch adds only the async database close, resource assertion, and explanatory comment before reopening the fixture database. Raw mutations and metadata assertions remain intact. (src/gateway/server-methods/chat-metadata-session-writes.test.ts:133, baa3475838b5)
  • Existing lifecycle owner: The async close fences the selected root, joins resource drainage and pending opens, and awaits WAL-maintenance shutdown before closing native handles. The second test group already uses this boundary before raw SQLite access. (src/state/openclaw-agent-db-lifecycle.ts:653, baa3475838b5)
  • Current main still needs the fixture repair: Fetched main has the same test file as the pinned merge base and still opens the database immediately after setup, without the added drain. The verified test-merge comparison contains only the intended fixture change. (src/gateway/server-methods/chat-metadata-session-writes.test.ts:133, 66d898f6662a)
  • Recorded root cause and validation: The captured PR body reports controlled maintenance-admission failures on both Bun pins and Node 24/26, a negative control that fails without the drain, and 89/89 fixed-file passes. The linked issue's author comment corroborates the fixture ordering mechanism and explicitly leaves Windows/Feishu reply loss unconnected. These are contributor-recorded observations; this read-only review did not execute tests. (baa3475838b5)
  • Prior qualification request remains applicable: The testing guide requires 20 clean standalone file runs, three clean original-shard runs, and owner sibling tests for flake repairs. The supplied body records focused runtime runs but not this qualification. The prior completed review requested the same evidence, and the live body remains unchanged. Public docs: docs/help/testing/writing-tests.md. (docs/help/testing/writing-tests.md:187, baa3475838b5)
  • Merged feature-history routing: Targeted blame identifies the metadata-read source line with e1cb2b3. Inspection of that commit and its recorded parent shows the read module was added there; file history also records multiple earlier metadata and database-lifecycle contributions by Peter Steinberger. (src/gateway/server-methods/chat-metadata-session-read.ts:113, e1cb2b3c22ac)

Likely related people:

  • Peter Steinberger: Raw commit e1cb2b3 adds src/gateway/server-methods/chat-metadata-session-read.ts:113 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: e1cb2b3c22ac; files: src/gateway/server-methods/chat-metadata-session-read.ts)

Rank-up moves

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

  • Record 20 clean standalone file runs, three clean original-shard runs, and owner sibling coverage required by the testing guide.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-07T08:23:53.721Z sha baa3475 :: needs changes before merge. :: none

@steipete

steipete commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Accepting the deterministic proof for this one-file fixture repair. The controlled maintenance window reproduces the same WAL write-lock failure on Bun 667c, Bun fc53, Node 24, and Node 26. Removing only the drain makes the new lifecycle regression fail on next Bun and Node 24; the drained fixture passes all 89 cases on all four runtimes, with final main-based proof also green on both Bun pins and Node 24. This directly checks the unsafe ordering rather than relying on nondeterministic repetitions.

Additional repetition evidence will come from the planned Gateway-methods admission qualification: the original owner, including this file, will run five times per runtime on the next pin with this fix included. A separate 20 standalone / 3 shard campaign is not required for this fixture-only repair; the deterministic proof is accepted, and the planned qualification avoids duplicate CI spend. No whole-owner qualification is claimed by this PR; the fixture repair is ready to land.

@steipete
steipete merged commit fa420c6 into main Oct 7, 2026
176 checks passed
@steipete
steipete deleted the fix/chat-metadata-fixture-maintenance-drain branch October 7, 2026 08:35
@steipete

steipete commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

steipete added a commit that referenced this pull request Oct 7, 2026
Main now drains setup maintenance before warming the metadata caches.
Remove the redundant write-admission wrapper and retain the raw fixture
mutation covered by the upstream fix in #166520.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 8, 2026
…penclaw#166520)

Drain setup-owned session maintenance before warming the chat metadata fixture's exact or full cache, and assert that asynchronous database resources have settled. This prevents raw fixture SQL from blocking the host admission grant needed by a maintenance worker holding the WAL write lock.

The controlled lock window reproduces across both Bun pins and Node 24/26. The negative control fails without the drain, and the complete fixed fixture passes 89/89 across all four runtimes. Final main-based Node/Bun proof, changed-file checks, independent review, and exact-head CI passed. Production SQLite policies are unchanged.

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

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS 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.

Bun/Vitest: two chat.metadata exact-cache tests fail with SQLite locks while Node passes

1 participant