Skip to content

fix(gateway): cancel in-flight environment reconciliation on suspend - #178538

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-inflight-environment-reconcile-at-shutdown-20260919
Oct 20, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-inflight-environment-reconcile-at-shutdown-20260919

Conversation

@steipete

@steipete steipete commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes a shutdown delay when worker-environment reconciliation starts immediately before suspension and a provider inspection remains pending. Follow-up to #177756.

User Impact

Suspension now cancels an already-running environment reconciliation. Read-only inspections have a 15-second budget and late replies cannot hold the reconcile guard or change environment state. Resuming permits the next sweep to retry the same environment.

Why This Change Was Made

The startup/60-second sweep already stopped admitting new candidates during suspension, but did not cancel admitted work. Provider inspection also shared the mutation queue's requirement to retain the underlying promise after cancellation. The service now releases cancelled observations while retaining actual provider mutations until settlement.

Maintenance interruption has a distinct cancellation reason: it preserves the lease and provisioning operation ID instead of recording user-requested destruction. Runtime installation keeps its existing transfer-sized budget. Pending-operation traces now distinguish bootstrap and transport teardown.

The original production trace did not identify the exact nested await. This change repairs the reproduced inspection and suspension gaps; it does not claim that every write-capable cleanup can be abandoned or that all possible shutdown delays are eliminated.

Evidence

An 85-second HTTP inspection was already active before suspension. Two concurrent baseline/candidate pairs ran under an exclusive measurement lock, swapping the 16-core CPU halves between pairs, with separate synthetic state and ports.

Pair Baseline sidecar shutdown Fixed sidecar shutdown Paired difference Fixed suspension cancellation
1 84,008.73 ms 13.39 ms −83,995.34 ms 4.39 ms
2 (CPU halves swapped) 84,010.12 ms 8.15 ms −84,001.96 ms 3.32 ms

Baseline reconciliation remained active one second after suspension and waited for the HTTP reply. The fixed reconcile guard took 0 ms in both runs; both active placements and their manifests were retained. The baseline produced the same pending-span pattern as the reported stall. These timings cover the real placement/environment sidecars, not full Gateway process shutdown.

  • The new suspend regression failed on main because the active inspection signal remained un-aborted.
  • Focused service lifetime, provisioning replay, provider reconciliation, and placement guard checks passed (67 tests). The final lifetime rerun passed all 39 tests; the two closure guard files passed 22 tests. Export-name collision checks passed.
  • Changed-file formatting/lint and one core typecheck lane passed; PR CI owns broad validation.
  • CI caught an immediate spy assertion in the reversible-suspension scheduler test. Environment sweeps intentionally run outside the scheduler tick. The test now settles baseline work and waits for scheduled post-rollback inspection through a deferred signal; it neither sleeps nor directly triggers the resumed reconciliation. The focused file passes all 9 tests (35.29 s wall time, including compiler preparation), and the isolated repair review found no actionable P0–P2 issues.
  • Final isolated Codex review of the diff and relevant lifecycle source: no actionable P0–P2 findings. Paired measurements and the full-Gateway cancellation-aware provider reproduction passed.

Full-Gateway proof on 91fd772d21b8f63fc2cc7e1e345fce1a55a84dde: an actual CLI Gateway loaded a registered provider and a persisted ready environment, began an 85-second HTTP inspection, received gateway.suspend.prepare, then SIGTERM. The cancellation-aware provider exited cleanly in 563.63 ms after SIGTERM; the suspend RPC took 46.96 ms. The remote response never completed. Reopening SQLite confirmed the same ready environment/lease with no destruction request.

A second full-process case deliberately ignored the inspection signal. Reconciliation and its guard still released immediately, but the plugin lifetime retained its raw call for the existing five-second retirement grace. Final cleanup traced 5,131.8 ms, narrowly missing the five-second full-process target; exact process-exit latency was not captured because the deadline assertion fired. The lease was preserved. This plugin ownership boundary is unchanged.

No Testbox/Crabbox lease was allocated. Both harnesses used synthetic state in a Linux sandbox. The full-process fixture used a real Gateway, registered provider, SQLite, and HTTP transport; its unreachable remote was seeded and had no running worker process. Production restart verification and identification of the original production nested await remain outstanding.

Production source: +102/−35 lines (net +67); the stat excluding tests/docs also includes +54/−19 benchmark lines, for net +102. Tests: +104/−20 lines (net +84). Production code grows to carry cancellation lifetime and reason, enforce the read budget, and expose the previously opaque wait. No new user configuration, schema, dependency, or compatibility path.

@clawsweeper

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

@steipete
steipete marked this pull request as ready for review October 20, 2026 16:05
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T17:08:50.475791Z 3c9c30f New commits
ℹ️ 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.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime scripts Repository scripts size: M labels Oct 20, 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. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. 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 clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 20, 2026
@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Cancel active environment reconciliation during suspension, bound provider inspections, and preserve interrupted provisioning for recovery.

Example: Suspension begins while an 85-second provider inspection is pending.

  • Before: Reconciliation retains its guard while waiting for the inspection.
  • After: The inspection wait releases on suspension, and the lease remains available for reconciliation after resume.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Focused lifecycle repair with applicable Gateway proof and no actionable findings; exact-head CI is still pending.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The supplied Linux CLI Gateway scenario exercises suspension during real HTTP-backed provider inspection, cancellation, process exit, and SQLite lease preservation; matching production blobs make that proof applicable to this head. The separate cancellation-ignoring case demonstrates guard release and documents the unchanged plugin retirement limit.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Gateway suspension or restart can wait on an environment inspection admitted immediately before suspension.
Reason: The maintainer-authored direction addresses a reproduced lifecycle gap while preserving mutation settlement; the two added flags belong only to the measurement script.

Merge readiness

✅ Ready for maintainer review

This PR repairs a source-confirmed reconciliation cancellation gap; no actionable defect was found in the production change or scheduler-test repair.

Priority: P1
Reviewed head: 3c9c30fe2283d74206d9df233582a0d11133e282

Before merge

None.

Findings

None.

Agent review details

How this fits together

The Gateway environment service coordinates provider observations, provisioning, placement recovery, and shutdown through existing operation queues and lifecycle guards.

flowchart TD
  A[Gateway suspension or shutdown] --> B[Reconciliation cancellation]
  B --> C[Environment service]
  C --> D[Provider inspection]
  C --> E[Provider mutation queue]
  D --> F[Release cancelled observation]
  E --> G[Await producer settlement]
  F --> H[Preserve lease for recovery]
  G --> H
Loading

Technical review

Best possible solution:

Retain the existing lifecycle owners while cancelling observations promptly and joining write-capable producers through settlement.

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

Main checks suspension before admission but does not cancel admitted reconciliation, and its provider queue retains pending inspections; the supplied baseline and Gateway scenarios exercise that path.

Is this the best way to solve the issue?

The patch fixes cancellation at the existing service owner, separates observations from mutations, and keeps provisioning recovery identity without introducing a parallel lifecycle owner.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Provider operation queue changes intended behavior with a stated reason (82f6280: Retain actual provider operations after caller timeouts so replay and destruction cannot overlap setup.)
  • Suspension and reconciliation keeps the original intent (bf105d1: Pause periodic reconciliation during suspension and cancel device observations during restart drain.)
  • Provisioning cancellation and shutdown keeps the original intent (d01eb83: Cancel recovered provisioning through the service owner while preserving durable operation identity and awaiting actual producers.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 82f6280 adds src/gateway/worker-environments/service.ts:369 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 82f6280fa86a; files: src/gateway/worker-environments/service.ts)

PR surface

Source +67, Tests +84, Docs +4, Other +35. Total +190 across 12 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 8 102 35 +67
Tests 2 104 20 +84
Docs 1 7 3 +4
Config 0 0 0 0
Generated 0 0 0 0
Other 1 54 19 +35
Total 12 267 77 +190

Review metrics

Metric Value Why it matters
Production versus regression coverage Production +102/-35; tests +104/-20 Growth carries cancellation lifetime and reason, the inspection budget, and teardown diagnostics through existing owners; tests cover suspension, resume, late observations, and lease preservation.

Labels

Label changes:

No label changes.

Label justifications:

  • P1: An already-running provider inspection can substantially delay Gateway restart and suspension.
  • 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-10-20T16:29:59.649Z sha 91fd772 :: needs maintainer review before merge. :: none

Reviewed October 20, 2026, 1:11 PM ET / 17:11 UTC (Revision 2).

@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The CI failure in the reversible-suspension scheduler test is fixed on 3c9c30fe2283d74206d9df233582a0d11133e282. Environment reconciliation deliberately runs outside the scheduler tick; the test now settles baseline work and waits for the scheduled post-rollback inspector through a deferred signal. It neither sleeps nor directly triggers the resumed reconciliation. All 9 tests in the failing file pass locally, and the independent review of the repair is clean. The production fix is unchanged; the branch is rebased onto current main and the PR body records the failure, repair, and proof limits.

@clawsweeper

clawsweeper Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@steipete
steipete force-pushed the steipete/perf-micro-inflight-environment-reconcile-at-shutdown-20260919 branch from 91fd772 to 3c9c30f Compare October 20, 2026 17:05
@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. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 20, 2026
@steipete
steipete merged commit 5cedb0d into main Oct 20, 2026
138 checks passed
@steipete
steipete deleted the steipete/perf-micro-inflight-environment-reconcile-at-shutdown-20260919 branch October 20, 2026 17:30
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 21, 2026
…penclaw#178538)

* fix(gateway): cancel in-flight environment reconciliation on suspend

* test(gateway): await resumed environment inspection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. docs Improvements or additions to documentation gateway Gateway runtime P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: M 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