Repository navigation
fix(infra): state-lifecycle lock stays held when a command reaches a closed SQLite worker actor - #158202
Conversation
…losed In `executeCommand`, `await prepareLifecycle()` acquired the state-lifecycle coordinator (BEGIN EXCLUSIVE on the shared lock file) before the `actors.get(request.actor)` check. When the actor had already been closed, the "SQLite worker actor is closed" throw escaped ahead of the `try`/`finally` that calls `releaseLifecycle`, and the failure reply did not set `retire`. The broker therefore kept a live worker that owned state-lifecycle, and every later acquire in the process, from the main thread or a reclamation worker, failed with `StateDatabaseCoordinatorContentionError: another OpenClaw process owns state-lifecycle` until the gateway was restarted. Check for the closed actor before acquiring, matching the order the `close` branch already uses, so nothing can throw between acquisition and the releasing `finally`. Add a focused test that drives the worker with an execute request for a never-opened actor through a broker-style lifecycle preparation port and then asserts the main thread can still take the state-lifecycle coordinator. On the previous code it fails with the contention error naming the test's own pid as holder. Related openclaw#158095 Related openclaw#157325 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 25, 2026, 12:25 PM ET / 16:25 UTC. ClawSweeper reviewWhat this changesThe branch makes the SQLite store worker reject a command for a closed actor before acquiring the shared state-lifecycle lock, and adds a worker-thread regression test. Merge readiness✅ Ready for maintainer review Current main still has the lock leak, and this PR addresses a concrete worker exit path. The related gateway outage has another proposed cause, so this branch is a scoped fix rather than a complete resolution of that report. Priority: P0 Review scores
Verification
How this fits togetherThe SQLite store worker receives database commands from the broker and acquires a shared lifecycle lock around admitted work. Its replies and lock release determine whether later agent turns and maintenance can use the state database. flowchart LR
A[Database command] --> B[SQLite worker]
B --> C{Actor open?}
C -- No --> D[Failure reply]
C -- Yes --> E[Acquire lifecycle lock]
E --> F[Execute command]
F --> G[Release lock and reply]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +1, Tests +76. Total +77 across 2 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep the worker's closed-actor rejection ahead of lock acquisition, and track the separate worker-retirement investigation in the linked gateway issue. Do we have a high-confidence way to reproduce the issue? Yes for the narrow worker defect: source shows the pre-fix throw outside the releasing finally, and the PR reports a before-and-after worker-thread lock check. The full gateway incident was not independently reproduced. Is this the best way to solve the issue? Yes for this lock leak: rejecting the missing actor before acquisition is a narrow repair. It does not address the separately reported worker-retirement wait. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against f4151933b8ff. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
|
CI note: the only red shard is Field data since opening: the same reorder applied to a 2026.9.6 gateway that had been re-wedging on |
|
Additional maintainer validation on unchanged head The same private fixture exercised the production suspension entry point with a dirty task registry and the updater's completed-step receipt writer. Parent Local checks on this head passed all 8 cases: the new worker regression in 1.423 seconds and 7 existing lifecycle-cleanup cases covering framed results, queued followers, revoked authority, nested scopes and preparation refusal. The first adjacent-suite attempt did not collect because a filtered install omitted QuickJS; restoring frozen workspace dependencies resolved that setup failure. The new 76-line test was audited in full. Independent AutoReview reported no P0/P1 findings. Sanitized raw evidence · Artifact manifest This proves the repaired transition. The cause of the historical operator incident remains unconfirmed. The separate worker-retirement path remains open. The matched Windows file diagnostic passed all 11 tests, including the timed-out case in 95.619 seconds with the unchanged 120-second deadline: https://github.com/openclaw/openclaw/actions/runs/36213627830. The original full shard then passed on unchanged head aa3c100: 16 files across five projects, 383 tests passed and 14 existing platform skips, with the timed-out case completing in 102.130 seconds. This establishes observed timing variability under the original workload; no timing defect was patched, and the original failed attempt remains recorded. No test or deadline changed. Full shard result. Both CI-gate contexts passed before native merge. Artifact links expire October 3. |
|
Merged via squash.
|
…losed (openclaw#158202) In `executeCommand`, `await prepareLifecycle()` acquired the state-lifecycle coordinator (BEGIN EXCLUSIVE on the shared lock file) before the `actors.get(request.actor)` check. When the actor had already been closed, the "SQLite worker actor is closed" throw escaped ahead of the `try`/`finally` that calls `releaseLifecycle`, and the failure reply did not set `retire`. The broker therefore kept a live worker that owned state-lifecycle, and every later acquire in the process, from the main thread or a reclamation worker, failed with `StateDatabaseCoordinatorContentionError: another OpenClaw process owns state-lifecycle` until the gateway was restarted. Check for the closed actor before acquiring, matching the order the `close` branch already uses, so nothing can throw between acquisition and the releasing `finally`. Add a focused test that drives the worker with an execute request for a never-opened actor through a broker-style lifecycle preparation port and then asserts the main thread can still take the state-lifecycle coordinator. On the previous code it fails with the contention error naming the test's own pid as holder. Related openclaw#158095 Related openclaw#157325
Related #158095
Related #157325
What Problem This Solves
Fixes: a gateway stops running agent turns, failing every one with
StateDatabaseCoordinatorContentionError: another OpenClaw process owns state-lifecyclewhile health stays ok, when a SQLite store worker receives a command for an actor that has already been closed.User Impact
User impact: a closed-actor command no longer leaves the shared
state-lifecyclelock held inside the gateway, so later agent turns, idle cleanup, WAL maintenance and cron reconciliation keep acquiring it instead of failing until someone restarts the gateway.This PR addresses only the first of the two paths listed in #158095. The second (worker retirement awaiting
waitForWorkerNativeSectionswithout a timeout inworker-task-pool) is not touched here, so the issue should stay open.Why This Change Was Made
In
src/infra/sqlite-store.worker.ts,executeCommandranawait prepareLifecycle()(which acquires the state-lifecycle coordinator, i.e.BEGIN EXCLUSIVEon the shared lock file) before checkingactors.get(request.actor). When the actor was already closed, the"SQLite worker actor is closed"throw happened before thetry/finallythat callsreleaseLifecycle, and the failure reply did not setretire. The broker kept a live, idle worker that still owned the lock, and since the lock is process-wide, every later acquire in the gateway failed.The fix moves the closed-actor check ahead of the acquisition, which is the order the
closebranch already uses. Nothing else can throw betweenprepareLifecycle()resolving and the releasingfinally(acquireSqliteWorkerLifecyclereleases its own coordinator on internal failure, and theexecute-framepath only reachesexecuteCommandafterexecute-startverified the actor whilependingInputblocks an interleavedclose), so this closes the gap without reshaping the function.Evidence
Production observation (the motivating incident). On 2026.9.6 (eb377ac), arch-desktop, the gateway logged
another OpenClaw process owns state-lifecycleat roughly 1/s from 02:54 on 2026-09-25 until a restart at 11:30 (~915 per 15 min).state_leases/agent_database_leasesshowed only the live gateway pid as owner and no other openclaw process was alive. Sessions failed withAgent database execution admission is closed(home:main 69, finance:main 19) while health stayed ok; the restart cleared it immediately. This matches the report in #158095 (same version, lock held by the gateway's own pid, health green, restart is the only remedy).Focused test (
src/infra/sqlite-worker-closed-actor.test.ts): spawns the realsqlite-store.worker.tsin aWorker, sends anexecuterequest for a never-opened actor with a broker-stylelifecyclePreparationport (acceptingcheckwith no borrowed lifecycle so the worker acquires the coordinator itself, then acceptingacquired), asserts the reply is the closed-actor failure withoutretire, and then asserts the main thread can takeacquireStateDatabaseCoordinatorfor the same database/runtime directory.vitest run --config test/vitest/vitest.unit-fast-root.config.ts src/infra/sqlite-worker-closed-actor.test.ts(Node v26.8.1, vitest 5.0.0).Lint/format:
oxlintandoxfmt --checkpass on both touched files. Only the focused test was run locally, not the full suite.🤖 Generated with Claude Code