Skip to content

fix(gateway): leave a replaced installation to the update run that holds it - #200389

Merged
obviyus merged 2 commits into
mainfrom
fix/gateway-stale-install-update-owner
Nov 17, 2026
Merged

obviyus merged 2 commits into
mainfrom
fix/gateway-stale-install-update-owner

Conversation

@obviyus

@obviyus obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Problem

During openclaw update --no-restart with the Gateway running, the Gateway's installation check (src/gateway/server-maintenance.ts, every 30 s) sees the new build-info.json and restarts the Gateway into the new package while the update still owns the installation. The update's own Gateway start then waits on state ownership held by the self-restarted process, stalls for 120 s, and the update fails with activated-gateway-startup-failed and rolls back (exit 1). This happens on plain package installs and on the release-directory layout from #198835. The natural rate is about the race window divided by 30 s. #198835 measured that window at 1.75–1.88 s, which gives about 6% per run; I did not re-measure the window.

Change

The update run is the one owner of the installation while it holds the install lease (the same managed-update-handoffs row the updater's executor takes). checkGatewayInstallationReplacement (src/gateway/stale-install.ts) now skips the self-restart while a live process holds that lease. A Gateway the run leaves stale restarts on a later check, after the lease is released.

  • The lease key is captured with the installation root when the observer registers.
  • Lease reads and process-identity probes are synchronous, so they run in the existing handoff worker (prepareManagedHandoffParentStore), never on the serving thread. They run only when a changed build is observed.
  • An unreadable lease keeps today's behavior (self-restart).
  • No new gate in the updater, no schema or config change.

Impact

--no-restart updates with a running Gateway no longer race the update's Doctor and Gateway start. Behavior outside an update run is unchanged.

Evidence

Unit: src/gateway/stale-install.test.ts › "leaves a replaced installation to the update run that holds it". It holds a real lease, checks that a changed build does not restart the Gateway, releases the lease, then checks that the next tick does restart it. Red on 2435c1faa791 (handoff called once while the lease is held), green on this head; the other 14 tests in the file pass.

E2E (mbp-m5, real openclaw update --no-restart against a launchd Gateway, isolated --profile): the reproduction makes the installation check fire every 250 ms in the running build. Only server-maintenance changes in a packed copy; the build ID differs, the code is otherwise identical. Each arm: install, start the Gateway service, then update --no-restart --tag <next build> with the Gateway running.

Arm Layout Result Update wall time
main 2435c1fa release dirs (install-cli.sh prefix, adopted) fail: self-restart at T+0.25 s, activated-gateway-startup-failed, rolled back 351 s
main 2435c1fa plain user npm prefix fail: same 455 s
this PR d6f23896 release dirs ok, no self-restart signal, Gateway healthy on the new build 133 s
this PR d6f23896 plain user npm prefix ok, no self-restart signal, Gateway healthy on the new build 188 s

Gates: pnpm tsgo:core, pnpm check:test-types, oxlint and oxfmt on changed files all pass.

Local ClawSweeper review (gpt-6.1-sol, normal speed) at d6f23896: patch is correct, 0 findings. The previous head's single finding (synchronous lease reads on the Gateway thread) is fixed by the worker move.

Not covered: the missing-runtime-chunk path in classifyGatewayStaleInstall still restarts on a request failure during an in-place swap. That path triggers on a real load failure and was left as is.

obviyus and others added 2 commits November 17, 2026 08:09
…lds it

While an update run holds the install lease it stops and restarts the
Gateway itself. The Gateway's installation check could restart it into the
new package first, racing the update's Doctor and Gateway start; with
--no-restart that failed the update and rolled it back. The check now skips
the self-restart while a live update run holds the installation, and a later
check restarts a Gateway the run leaves stale.

Co-authored-by: obviyus <[email protected]>
The installation check runs on the serving Gateway thread. Lease reads and
process probes are synchronous, so they now run through the existing
handoff worker, and the lease key is captured with the installation root.

Co-authored-by: obviyus <[email protected]>
@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.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S labels Nov 17, 2026
@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. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. 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: needs maintainer review before merge.

What this changes

The Gateway defers automatic installation-replacement restart while a live updater holds the existing installation lease.

Example: Run openclaw update --no-restart with a launchd Gateway running when its installation check observes the replacement build.

  • Before: The Gateway can restart ahead of the updater, causing activated-gateway-startup-failed and rollback.
  • After: The installation check leaves the handoff to the live updater and checks again after its lease is released.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) A focused, source-supported repair with a meaningful lease regression; the runtime evidence remains a reported summary.
Proof confidence 🦪 silver shellfish (2/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: An update can fail and roll back because the running Gateway restarts into the replacement package before the updater finishes its handoff.
Reason: The maintainer-authored direction addresses an independently documented failure while preserving the existing manual-replacement and suspension owners.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR addresses a documented update race still present on main, and the introduced diff has no actionable correctness or security findings.

Priority: P2
Reviewed head: d6f23896bd0bb98deeae12010966c0590929e383

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: Provide inspectable output from the reported running-Gateway update scenario and a latest-stable upgrade with settings and data preserved, redacting IPs, credentials, phone numbers, and non-public endpoints.
Agent review details

How this fits together

Gateway maintenance compares running and installed build identities; the existing update coordination worker supplies lease and process-liveness facts before the observer requests a restart.

flowchart TD
  A[Gateway maintenance tick] --> B[Read installed build identity]
  B --> C[Installation replacement observer]
  C --> D[Existing handoff worker]
  D --> E[Update lease and process identity]
  E --> C
  C --> F[Defer or request restart]
Loading

Technical review

Best possible solution:

Keep installation handoff with the existing live update-lease owner and retain automatic replacement detection after that owner releases it.

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

Current source exposes the competing restart path, and this PR plus #198835 report matching real-update failures; this read-only review did not execute the reproduction.

Is this the best way to solve the issue?

The patch is a focused repair of installation ownership: it reuses canonical root resolution, the existing lease store, and its worker rather than adding another coordinator or updater gate.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Manual installation-replacement detection keeps the original intent (dd04e91: Detect package replacement, drain active work, and restart through the existing lifecycle while preserving explicit updater ownership.)
  • Suspended installation handoffs keeps the original intent (5ad0819: Prevent installation observations from initiating a restart that competes with a host-controlled suspension.)
  • Nonblocking maintenance observations keeps the original intent (83e3641: Keep synchronous installation filesystem work off the serving Gateway thread.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

  • Introduced change: The pinned introduced diff adds a captured canonical lease key, worker-backed lease inspection, and one held-then-released lease regression without changing schemas or configuration. (src/gateway/stale-install.ts:207, d6f23896bd0b)
  • Current-main failure path: Current main still records a replacement immediately when build identity changes, without consulting the updater's installation lease. (src/gateway/stale-install.ts:202, b6db4fad70c4)
  • Existing coordination owner: The updater acquires the existing installation lease, and the reused handoff worker performs lease reads and process-identity checks away from the serving thread. (src/infra/update-managed-service-handoff-engine.worker.ts:64, d6f23896bd0b)
  • Independent prior reproduction: The merged release-layout PR explicitly documents this race on both release-bound and plain main installations, leaves it for a separate installation-check repair, and does not claim its layout change caused it. (b868ed4c21ba)
  • Behavioral regression: The added test acquires a real lease, verifies replacement remains pending while held, releases it, and verifies the next check requests handoff; the body reports red-on-base and green-on-head results. (src/gateway/stale-install.test.ts:220, d6f23896bd0b)
  • Proof and discussion inspected: The complete body reports accelerated-check launchd experiments for both layouts, but contains no runtime transcript or linked artifact; both discussion comments are review-status messages. (d6f23896bd0b)

PR surface

Source +45, Tests +22. Total +67 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 50 5 +45
Tests 1 22 0 +22
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 72 5 +67

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:

No label changes.

Label justifications:

  • P2: A documented intermittent race fails and rolls back an advanced CLI update workflow; the report does not establish a broadly unusable runtime or a blocked first-time setup.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rank-up moves

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

  • Attach the runtime and upgrade evidence identified in the proof assessment.

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 16, 2026, 10:40 PM ET / November 17, 2026, 03:40 UTC.

@obviyus
obviyus merged commit f31a0b1 into main Nov 17, 2026
143 checks passed
@obviyus
obviyus deleted the fix/gateway-stale-install-update-owner branch November 17, 2026 04:34
@obviyus

obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Landed as f31a0b1. While an update run holds the install lease, the Gateway no longer restarts itself. That self-restart was racing update --no-restart and failing the update with activated-gateway-startup-failed. Proof: the 250 ms repro fails on main and passes here, on both the release-dir and plain npm layouts. Unit test included.

RomneyDa added a commit that referenced this pull request Nov 18, 2026
…lds it (#200389)

Adapt existing managed handoff worker to inspect live update lease off the serving thread. Retain observer generation and suspension guards; preserve missing-runtime recovery and unreadable-lease fallback.

Backport of f31a0b1. Parent reviewed bounded worker operation, existing lease owner and observer effect check; reuse worker real-lease red/green13 and sibling3 proof.

Co-authored-by: RomneyDa <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 18, 2026
…lds it (openclaw#200389)

## Problem

During `openclaw update --no-restart` with the Gateway running, the Gateway's installation check (`src/gateway/server-maintenance.ts`, every 30 s) sees the new `build-info.json` and restarts the Gateway into the new package while the update still owns the installation. The update's own Gateway start then waits on state ownership held by the self-restarted process, stalls for 120 s, and the update fails with `activated-gateway-startup-failed` and rolls back (exit 1). This happens on plain package installs and on the release-directory layout from openclaw#198835. The natural rate is about the race window divided by 30 s. openclaw#198835 measured that window at 1.75–1.88 s, which gives about 6% per run; I did not re-measure the window.

## Change

The update run is the one owner of the installation while it holds the install lease (the same `managed-update-handoffs` row the updater's executor takes). `checkGatewayInstallationReplacement` (`src/gateway/stale-install.ts`) now skips the self-restart while a live process holds that lease. A Gateway the run leaves stale restarts on a later check, after the lease is released.

- The lease key is captured with the installation root when the observer registers.
- Lease reads and process-identity probes are synchronous, so they run in the existing handoff worker (`prepareManagedHandoffParentStore`), never on the serving thread. They run only when a changed build is observed.
- An unreadable lease keeps today's behavior (self-restart).
- No new gate in the updater, no schema or config change.

## Impact

`--no-restart` updates with a running Gateway no longer race the update's Doctor and Gateway start. Behavior outside an update run is unchanged.

## Evidence

**Unit:** `src/gateway/stale-install.test.ts` › "leaves a replaced installation to the update run that holds it". It holds a real lease, checks that a changed build does not restart the Gateway, releases the lease, then checks that the next tick does restart it. Red on `dd40e0123cdc` (handoff called once while the lease is held), green on this head; the other 14 tests in the file pass.

**E2E (mbp-m5, real `openclaw update --no-restart` against a launchd Gateway, isolated `--profile`):** the reproduction makes the installation check fire every 250 ms in the *running* build. Only `server-maintenance` changes in a packed copy; the build ID differs, the code is otherwise identical. Each arm: install, start the Gateway service, then `update --no-restart --tag <next build>` with the Gateway running.

| Arm | Layout | Result | Update wall time |
|---|---|---|---|
| main `dd40e012` | release dirs (`install-cli.sh` prefix, adopted) | **fail**: self-restart at T+0.25 s, `activated-gateway-startup-failed`, rolled back | 351 s |
| main `dd40e012` | plain user npm prefix | **fail**: same | 455 s |
| this PR `d6f23896` | release dirs | **ok**, no self-restart signal, Gateway healthy on the new build | 133 s |
| this PR `d6f23896` | plain user npm prefix | **ok**, no self-restart signal, Gateway healthy on the new build | 188 s |

Gates: `pnpm tsgo:core`, `pnpm check:test-types`, oxlint and oxfmt on changed files all pass.

Local ClawSweeper review (gpt-6.1-sol, normal speed) at `d6f23896`: patch is correct, 0 findings. The previous head's single finding (synchronous lease reads on the Gateway thread) is fixed by the worker move.

Not covered: the missing-runtime-chunk path in `classifyGatewayStaleInstall` still restarts on a request failure during an in-place swap. That path triggers on a real load failure and was left as is.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 19, 2026
…lds it (#200389)

## Problem

During `openclaw update --no-restart` with the Gateway running, the Gateway's installation check (`src/gateway/server-maintenance.ts`, every 30 s) sees the new `build-info.json` and restarts the Gateway into the new package while the update still owns the installation. The update's own Gateway start then waits on state ownership held by the self-restarted process, stalls for 120 s, and the update fails with `activated-gateway-startup-failed` and rolls back (exit 1). This happens on plain package installs and on the release-directory layout from #198835. The natural rate is about the race window divided by 30 s. #198835 measured that window at 1.75–1.88 s, which gives about 6% per run; I did not re-measure the window.

## Change

The update run is the one owner of the installation while it holds the install lease (the same `managed-update-handoffs` row the updater's executor takes). `checkGatewayInstallationReplacement` (`src/gateway/stale-install.ts`) now skips the self-restart while a live process holds that lease. A Gateway the run leaves stale restarts on a later check, after the lease is released.

- The lease key is captured with the installation root when the observer registers.
- Lease reads and process-identity probes are synchronous, so they run in the existing handoff worker (`prepareManagedHandoffParentStore`), never on the serving thread. They run only when a changed build is observed.
- An unreadable lease keeps today's behavior (self-restart).
- No new gate in the updater, no schema or config change.

## Impact

`--no-restart` updates with a running Gateway no longer race the update's Doctor and Gateway start. Behavior outside an update run is unchanged.

## Evidence

**Unit:** `src/gateway/stale-install.test.ts` › "leaves a replaced installation to the update run that holds it". It holds a real lease, checks that a changed build does not restart the Gateway, releases the lease, then checks that the next tick does restart it. Red on `2435c1faa791` (handoff called once while the lease is held), green on this head; the other 14 tests in the file pass.

**E2E (mbp-m5, real `openclaw update --no-restart` against a launchd Gateway, isolated `--profile`):** the reproduction makes the installation check fire every 250 ms in the *running* build. Only `server-maintenance` changes in a packed copy; the build ID differs, the code is otherwise identical. Each arm: install, start the Gateway service, then `update --no-restart --tag <next build>` with the Gateway running.

| Arm | Layout | Result | Update wall time |
|---|---|---|---|
| main `2435c1fa` | release dirs (`install-cli.sh` prefix, adopted) | **fail**: self-restart at T+0.25 s, `activated-gateway-startup-failed`, rolled back | 351 s |
| main `2435c1fa` | plain user npm prefix | **fail**: same | 455 s |
| this PR `d6f23896` | release dirs | **ok**, no self-restart signal, Gateway healthy on the new build | 133 s |
| this PR `d6f23896` | plain user npm prefix | **ok**, no self-restart signal, Gateway healthy on the new build | 188 s |

Gates: `pnpm tsgo:core`, `pnpm check:test-types`, oxlint and oxfmt on changed files all pass.

Local ClawSweeper review (gpt-6.1-sol, normal speed) at `d6f23896`: patch is correct, 0 findings. The previous head's single finding (synchronous lease reads on the Gateway thread) is fixed by the worker move.

Not covered: the missing-runtime-chunk path in `classifyGatewayStaleInstall` still restarts on a request failure during an in-place swap. That path triggers on a real load failure and was left as is.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit f31a0b1)
Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 0fe37f15175014cc29f37b7a2140e299c1c9f766)
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: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. 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