Skip to content

fix(doctor): update fails when another OpenClaw process has an agent database open - #172772

Merged
RomneyDa merged 3 commits into
mainfrom
fix/doctor-agent-lease-wait
Oct 17, 2026
Merged

RomneyDa merged 3 commits into
mainfrom
fix/doctor-agent-lease-wait

Conversation

@RomneyDa

@RomneyDa RomneyDa commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Depends on #172636; merge after it. This branch is stacked on fix/doctor-maintenance-contention-retry (head 0f241fc4106). Once that PR merges, rebase this onto main. Until then, the diff against main includes #172636's commits.

What Problem This Solves

Fixes: an update fails or leaves maintenance pending with "Doctor could not enter maintenance. An agent database is in use." when another OpenClaw process, such as a plugin command run while the Gateway is stopped, has an agent database open at the moment update-time Doctor enters maintenance.

User Impact

User impact: update-time Doctor now waits for that process to close its agent database, printing the holder's PID, how long it will wait, and progress every 30 seconds, then continues. The wait ends at the same deadline as #172636's state wait (up to 20 minutes by default from Doctor's start, less on a slow host), so time already spent waiting for state ownership shortens it. Before this change, the measured outcomes were:

  • openclaw update from published 2026.10.3: the update rolled back to 2026.10.3.
  • openclaw update repair: exited 2, with Doctor and plugin maintenance left pending.

If the database is still open at the deadline, Doctor refuses with the same deferred refusal (agent-database-in-use, fact code agent-database-lease-active). The message now names the holder's PID: "Waited 1200 s; OpenClaw process N still has an agent database open."

Update behavior: the change lives in the new version's Doctor, so it applies to any update to a release that contains it, from any installed updater, including the published 2026.10.3 driver (cell below). It changes no config, schema, lease rows, or installed-updater code. Unchanged cases, which still refuse immediately:

  • manual openclaw doctor --fix
  • a lease held by Doctor's own process (including its worker threads, which share the PID), so Doctor never waits on itself
  • any lease while a live Gateway still owns the state (the external-policy preflight), because that Gateway may own the lease

Why This Change Was Made

Where it sat. The lease check (assertDoctorAgentLeaseAdmission) runs only after Doctor holds state ownership (sqlite-maintenance). The leases live in the shared state database. To close its database, a holder has to write that database, and while Doctor's maintenance owner exists that write is refused with "undergoing offline maintenance". So waiting while holding ownership would leave the holder unable to finish. Retrying by re-acquiring ownership on each poll would refuse its close intermittently in the same way.

Shape. Admission now goes through one owner, admitDoctorAgentLeases in src/commands/doctor-agent-lease-refusal.ts. In each attempt it does the following:

  1. Acquire maintenance ownership (the existing path, including fix(doctor): update repair fails when a short-lived OpenClaw command holds state #172636's contention wait).
  2. Run the authoritative lease check.
  3. If the check fails in update-repair mode, and every live lease belongs to another PID, release everything the attempt acquired. This uses the same state.release() that legacy state-dir migration already uses mid-admission. Service-stop custody stays held.
  4. Poll the read-only lease observation that the external preflight already uses. It holds no lock and grants nothing.
  5. Once no foreign lease remains, or at the deadline, repeat the attempt. Only the check under held ownership admits repair.

Kinds of holder:

  • Stale leases (dead PID, or a changed start time) were already filtered out by the observation. That handling is unchanged.
  • A Gateway holder is handled earlier: by state contention (unchanged), or by the external-policy preflight, which still refuses while a live Gateway owns state (see below).

Budget and messaging. The lease wait has no budget of its own. On the first foreign lease it resolves #172636's resolveDoctorMaintenanceWaitDeadlineMs (30 min default step allowance less max(10 min, 2 × the rehearsal candidate-doctor time), on Doctor's process clock) and ends there. Because the deadline is absolute, a long state wait before it leaves correspondingly less for the lease wait, and the worst case for the whole Doctor run stays one allowance: about 1,200 s of waiting plus the slowest measured Doctor (258 s), ~24.3 minutes, instead of the previous 2 × 660 s + 258 s ≈ 26.3 minutes. If no allowance is left at the first foreign lease, Doctor refuses immediately with the unchanged message. The progress cadence, formatDoctorWaitSeconds, and wording come from the same owner; "Waiting up to N s" shows the remaining allowance. Polling backs off from 100 ms to 2 s. SIGINT/SIGTERM interrupts the wait through Doctor's maintenance exit signal. (Published drivers give activation Doctor no step timeout unless the operator passes --timeout; see #172636 for the facts behind the rule.)

Published updaters. Published drivers (2026.10.3 included) run activation Doctor with OPENCLAW_SERVICE_REPAIR_POLICY=external, after stopping the Gateway themselves. That policy runs a read-only lease preflight before any lock, which refused at once. In update-repair mode the preflight now refuses only while a live Gateway owns the state (readActiveGatewayLockIdentity; an unreadable lock refuses too). Otherwise it leaves the lease to the waiting admission above. Without this, the first after-build still rolled back on the published-driver cell.

Small contract change. OpenClawAgentDatabaseLeaseActiveError now carries the observed live owner PIDs as diagnostic evidence, never as authority. Lease semantics are unchanged.

Evidence

Published driver 2026.10.3 × candidate (Docker openclaw-published-driver-update-e2e, repo published-driver-update-docker.sh cell with a lease holder added; candidate packed from 8fa7e9c, which differs from the current head only in a comment and docs). openclaw update --tag <candidate> --yes. The holder is a separate process that waits until the driver has recorded the rehearsal in the update run, inserts a live agent_database_leases row for main with its own PID and start time, holds it until the driver stops the Gateway and 120 s more, then deletes it. Times PT, 2026-10-16:

  • 16:49:28.295–16:49:36.599: the driver records candidate-doctor (8.3 s) in the running run, phase validating. That is the ledger fact the budget reads.
  • 16:49:36.652 lease claimed; 16:50:29.362 Gateway stopped; 16:52:29.482 lease released.
  • Activation openclaw doctor step: exit 0 in 99.5 s (12.1 s in the same cell without a held lease). The update exits 0 with status ok, the candidate build …-8fa7e9cb26af-… installed, /readyz 200, and the durable session marker still visible; the cell's own assertions pass.
  • The 2026.10.3 driver keeps only the last 8 KB of Doctor output, so the wait lines themselves are not in the cell artifacts; the duration, the release time, and the successful result show the wait. fix(doctor): update repair fails when a short-lived OpenClaw command holds state #172636's update repair cell shows the message text ("Waiting up to 1197 s …").

Before this PR (from the previous revision's cell, unchanged): the same holder made the update fail with "Doctor could not enter maintenance. An agent database is in use." and roll back to 2026.10.3; openclaw update repair exited 2 with maintenance pending.

Regression tests: src/commands/doctor-maintenance.agent-lease-wait.test.ts drives real beginDoctorMaintenance against a real state database, with a lease row owned by another live PID, using fake timers and an injected performance.now (no real sleeps). It covers:

  • a lease released partway through ("Waiting up to 1200 s", progress at 45 s, no state owner held while waiting, admitted at 1,192 s), for both update Doctor and a published updater's activation Doctor (external service policy, no live Gateway)
  • a lease held to the deadline: fails at exactly 1,200 s with the deferred agent-database-in-use refusal and one failure fact naming the PID
  • a shared deadline: a state holder keeps Doctor waiting until 900 s, then the lease wait says "Waiting up to 300 s" and the refusal comes at the same 1,200 s deadline
  • manual doctor --fix, a lease held by Doctor's own process, and a lease while a live Gateway owns state under the external policy, which all still refuse immediately with the unchanged message

pnpm test <file> --maxWorkers=1, Node, on a heavily loaded host (load average 14–30 from other sessions):

  • src/commands/doctor-maintenance.agent-lease-wait.test.ts: 7/7, 72 s wall, 51 s Vitest duration (about 20 s of test time here, a few seconds on an idle host). Bun (OPENCLAW_VITEST_RUNTIME=bun): 7/7, 39 s wall.
  • src/commands/doctor-maintenance.contention-wait.test.ts: 6/6, 55 s wall. Bun: 6/6, 14 s wall.
  • src/flows/doctor-health.migration-refusal.test.ts: 18/18, 116 s wall.
  • src/commands/doctor-maintenance.settlement.test.ts: 42/42, 57 s wall.
  • src/commands/doctor-maintenance.foreground-update.test.ts: 4/4, 67 s wall.
  • src/commands/doctor-maintenance.interruption.process.test.ts: 7/7, 40 s wall.

pnpm tsgo:core, pnpm tsgo:core:test, type-aware oxlint on the changed files, oxfmt, pnpm format:docs:check, and git diff --check are clean.

Docs:

  • docs/cli/update/how-updates-run.md
  • docs/install/update-troubleshooting.md
  • docs/cli/doctor/recovery.md

Gaps:

  • If a lease's PID is reused by an unrelated process on a platform without process start times, it is still treated as live. Doctor then waits until the deadline and refuses as before.
  • While a live Gateway owns state under the external policy, Doctor refuses at once even if the lease belongs to another process; the Gateway's own contention would refuse anyway.
  • The openclaw update repair lease cell was not rerun for the shared deadline (its behavior is covered by the unit cases and fix(doctor): update repair fails when a short-lived OpenClaw command holds state #172636's repair cell); in the previous revision's run, repair left the hand-stopped Gateway stopped both before and after, which is unrelated.
  • The published-driver cell could not capture the wait lines themselves (8 KB Doctor output tail).

Autoreview and Testbox were skipped (release follow-up work, targeted local checks only).

@clawsweeper

clawsweeper Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper reviews are opt-in for maintainers. If a review is required, finish the work and validation first, then comment @clawsweeper review. Please avoid repeated requests while a review is queued or running; agents should not request reviews automatically.

@RomneyDa
RomneyDa force-pushed the fix/doctor-agent-lease-wait branch from 38ac589 to 43fc290 Compare October 17, 2026 00:27
@RomneyDa
RomneyDa enabled auto-merge (squash) October 17, 2026 00:49
@RomneyDa
RomneyDa merged commit ec38f53 into main Oct 17, 2026
132 checks passed
@RomneyDa
RomneyDa deleted the fix/doctor-agent-lease-wait branch October 17, 2026 00:55
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 17, 2026
…database open (openclaw#172772)

* fix(doctor): wait for transient agent database leases before update maintenance

* fix(doctor): wait for agent database leases under the published updater's external policy

* fix(doctor): end the agent lease wait at the shared update Doctor deadline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations docs Improvements or additions to documentation size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant