Skip to content

fix(gateway): preserve clean exit while releasing retained state leases - #200330

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-shutdown-singleton-reset-after-sqlite-close-20260919
Nov 17, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-shutdown-singleton-reset-after-sqlite-close-20260919

Conversation

@steipete

@steipete steipete commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an orderly Gateway stop exiting with failure when a retained state lease needs its final SQLite cleanup worker.

User Impact

Completed orderly cleanup preserves exit status 0, so service managers and deployment tooling do not classify it as a crash. Real cleanup failures still propagate. No configuration or schema changes are required.

Why This Change Was Made

The broker marked itself draining before canonical state cleanup finished. Releasing a retained lease then tried to open its cleanup worker and failed with SQLite worker host is closing | closed. The broker now seals ordinary clients and cancels queued admissions immediately, finishes canonical state cleanup, and only then closes host admission and workers. Accepted work still settles.

The faulty callback ordering is present in #188496 (83064b3ae1155); the implicated owner files are unchanged between 4bce7604 and 344cc75d. This identifies the current faulty implementation, not the first historical occurrence or the production trigger.

Evidence

The retained-lease regression fails on base 2cc6988bc28bd through drainGlobalSingletonLifecycleState("shared-state") with the reported error, while direct database close passes. It passes with the fix and verifies that accepted work settles and the lease row is removed.

Focused broker, lifecycle, capacity, state-lease and Gateway state-ownership tests passed (73 tests). A production-shaped synthetic owner harness uses real SQLite workers, an admitted lease, final singleton drain and the Gateway cleanup exit classifier: base exits 1 with the exact cleanup error; candidate exits 0. No live data was used. Fixture: 5,000 sessions, 35.1 GiB SQLite database, 62,000 workspace files / 1.1 GB on APFS. This does not claim Linux/systemd or full channel-drain proof.

The same synthetic harness also reproduces the failure on 4bce7604, confirming that this defect predates the supplied release range.

Five alternating pairs: base/fixed median owner elapsed 9.814/51.674 ms, process CPU 13.768/54.821 ms, sampled main-isolate allocation 2.26/2.95 MB. All seven base runs (two controls plus five measured) exit 1; all seven fixed runs exit 0. A/A elapsed controls were base 40.836/8.753 ms and fixed 37.268/53.203 ms. One fixed outlier was 598.619 ms under shared-host load. These are failing-path versus completed-cleanup measurements, not a speedup claim.

Production delta: +3 lines (25 added, 22 removed). The shared sealing loop preserves queued-write cancellation; successful registry clearing remains with the lifecycle owner. No new policy/state/cache was introduced. The implementation and final bookkeeping change passed independent Codex autoreview at P0–P2; the final braces-only measurement-script lint follow-up also passed scoped review.

pnpm check:changed completed: types, lint, format, import cycles and all structural/schema/worker guards pass. Its sole remaining failure is the unused-export scan: 115 identical findings on exact parent 2cc6988bc28bd and this PR; zero PR-only findings. The initial baseline scan lacked workspace dependency links and was discarded; the qualified rerun has no resolution errors. No skip or allowlist was added.

These are recorded main regressions, not noise. The historical consumer-removal patches account for 100 findings across #198933 (9bfad2b180b4d), #198936 (a0423637196f7) and #198943 (b90c5739fa5cb). The remaining 15 exports trace to compaction 472cd0a2f542, Talk thinking 94e34f956969a, package release directories #198835 (b868ed4c21ba0), desktop protocol movement #193487 (8af714898fa6e), subagent presentation #198133 (18136c960eaf7), and browser approval #200088 (1ce9df608b43b). This PR leaves those unrelated owners unchanged. Exact-head PR CI remains the broad landing gate.

Keep cleanup-worker admission open until canonical state lease finalizers finish, while sealing ordinary clients and canceling queued writes immediately. Then close the SQLite host and clear its successful retirement bookkeeping.

The retained-lease singleton regression is red on the base; 73 focused tests pass with the fix. A synthetic owner process reproduces exit 1 on the base and 4bce760, and exit 0 with the fix. The current faulty callback ordering comes from 83064b3 (#188496), before the reported release range. Production delta: +3 lines. Codex autoreview: scoped-clean at P0-P2.
@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.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime scripts Repository scripts size: M labels Nov 17, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper review

Please review final head 8720b39, especially retained lease cleanup before broker admission closes, immediate queued-write cancellation, and propagation of genuine cleanup failures. The owner-level regression is red on the base and green here; 73 focused tests and two independent Codex review passes are clean. The full final changed-file check remains in progress while this PR is draft.

@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@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. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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 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

This PR lets retained state leases finish cleanup before closing SQLite worker admission.

Example: An orderly Gateway stop releases a retained state lease that needs a cleanup worker.

  • Before: Cleanup encounters “SQLite worker host is closing” and the exit classifier returns 1.
  • After: Lease cleanup completes before worker shutdown and the exit classifier returns 0.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Focused, source-supported repair with a meaningful real-worker harness and no findings; evidence stops short of a shipped Gateway stop.
Proof confidence 🦐 gold shrimp (3/6) The maintainer-authored PR is outside the contributor proof gate and records the explicitly accepted real-worker owner harness: retained lease, singleton drain, cleanup exit classifier, and base exit 1 versus fixed exit 0; the inspected screenshots belong to unrelated linked UI work.
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 orderly Gateway stop can be classified as a crash because retained lease cleanup cannot open its final worker.
Reason: The maintainer-authored change restores the documented clean-exit contract with a focused repair and preserves actual cleanup failures.

Regression provenance

Regression provenance — suspected predecessor, not a causality claim: source commit 40d57f8dc8f5 by Peter Steinberger; no PR verified.

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

✅ Ready for maintainer review

This PR fixes a source-confirmed shutdown ordering defect that remains on main; no actionable introduced defects were found. Exact-head CI is still pending.

Priority: P2
Reviewed head: e1803f00f10a6c2d1a7f70581b31401907d19a6b

Before merge

None.

Findings

None.

Agent review details

How this fits together

The SQLite broker manages worker admission, clients, and native worker retirement; global singleton shutdown joins canonical shared-state cleanup before retiring those workers.

flowchart TD
  A[Gateway shutdown] --> B[Global singleton drain]
  B --> C[Seal ordinary clients and cancel waiters]
  C --> D[Canonical state lease cleanup]
  D --> E[Close worker admission and drain workers]
  E --> F[Classify cleanup result]
Loading

Technical review

Best possible solution:

Keep canonical lease cleanup ahead of host admission shutdown, using the existing sealing and lifecycle owners to preserve accepted settlement and genuine failure reporting.

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

Source confirms the cleanup-worker admission failure, and the contributor records a failing base regression and real-worker harness; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The patch fixes ordering at the existing broker boundary without adding policy, persistent state, or a competing cleanup path.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Broker shutdown ordering and successful cleanup bookkeeping keeps the original intent (83064b3: Join canonical state resource cleanup before retiring shared actors, reject queued admissions, and preserve custody after failed cleanup.)
  • Clean shutdown documentation keeps the original intent (a25c1f3: Bound residual handles after successful finalization while retaining existing failure and retained-cleanup handling.)
  • Accepted lease close regression keeps the original intent (53c0abe: Consolidate tests while retaining lifecycle, persistence, and cleanup contracts; the patch preserves the direct-close case and adds the distinct singleton entry point.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 40d57f8 adds src/infra/sqlite-worker-broker-lifecycle.ts:132 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 40d57f8dc8f5; files: src/infra/sqlite-worker-broker-lifecycle.ts)

PR surface

Source +3, Tests +6, Docs +2, Other +136. Total +147 across 5 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 25 22 +3
Tests 1 45 39 +6
Docs 1 3 1 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 1 136 0 +136
Total 5 209 62 +147

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 retained-lease shutdown can incorrectly report an orderly stop as a failure, affecting service-manager and deployment classification.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

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-11-17T00:48:03.044Z sha 8720b39 :: needs maintainer review before merge. :: none

Reviewed November 16, 2026, 7:57 PM ET / November 17, 2026, 00:57 UTC (Revision 2).

@steipete
steipete marked this pull request as ready for review November 17, 2026 00:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T00:54:58.693492Z e1803f0 Draft marked ready
ℹ️ 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.

@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Final head: e1803f0. Your prior review has zero findings and no open checklist items. The only subsequent diff adds braces around the existing conditional await in the measurement script (lines 69–71); production code and the 73-test proof are unchanged, and the lint-only delta passed Codex autoreview.

The PR is now ready. Final pnpm check:changed passes all types, lint, formatting and structural guards; its sole failure reproduces as the same 115 unused exports on exact parent 2cc6988, with zero PR-only entries. The updated body records their history and measurement limits. The explicitly requested owner-level proof is complete; a full shipped-CLI/systemd stop remains outside the claimed evidence.

@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@steipete
steipete enabled auto-merge (squash) November 17, 2026 01:13
@steipete
steipete merged commit f5d74a5 into main Nov 17, 2026
201 of 203 checks passed
@steipete
steipete deleted the steipete/perf-micro-shutdown-singleton-reset-after-sqlite-close-20260919 branch November 17, 2026 01:22
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash auto-merge.

RomneyDa added a commit that referenced this pull request Nov 17, 2026
…200334)

* fix: stabilize release qualification prerequisites

Constrain the live output-limit stimulus while preserving actual provider streams and recovery assertions. Wait for native lazy-question and cold Control UI readiness, and remove the retired RFB re-export.

Co-authored-by: RomneyDa <[email protected]>

* fix: retry transient state owner exit observations

Co-authored-by: RomneyDa <[email protected]>

* fix: preserve admitted restart widget authoring

Co-authored-by: RomneyDa <[email protected]>

* fix: recover restart dashboards and stabilize release qualification

OpenClaw-Publication: 5ae6496f-a9a5-4e37-a9af-79ec1af4bbb4

---------

Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 89c9e82)

Release adaptation: preserve the release inline RFB banner parser and its rfb-preauth consumer; the removed main-only re-export does not exist here. No newer RFB refactor imported. Later #200330 overlaps documentation only and is not a dependency of this patch.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 17, 2026
…es (openclaw#200330)

* fix(gateway): preserve clean exit while releasing retained state leases

Keep cleanup-worker admission open until canonical state lease finalizers finish, while sealing ordinary clients and canceling queued writes immediately. Then close the SQLite host and clear its successful retirement bookkeeping.

The retained-lease singleton regression is red on the base; 73 focused tests pass with the fix. A synthetic owner process reproduces exit 1 on the base and 6199e71, and exit 0 with the fix. The current faulty callback ordering comes from 4807fa9 (openclaw#188496), before the reported release range. Production delta: +3 lines. Codex autoreview: scoped-clean at P0-P2.

* style(perf): brace the shutdown measurement abort wait
RomneyDa added a commit that referenced this pull request Nov 17, 2026
…es (#200330)

Backport f5d74a5 after #188496. Keep cleanup-worker admission open until retained state lease finalizers finish, then drain the host and discard successful retirement bookkeeping.

Regression first: singleton close failed with SQLite worker host is closing, while database close passed. Fixed selected lease, capacity, queued-write, and cleanup checks: 6 passed. Format, staged test-line ratchet, and diff checks passed. Lint/typecheck blocked by linked dependency declaration boundaries.

Co-authored-by: RomneyDa <[email protected]>
RomneyDa pushed a commit that referenced this pull request Nov 19, 2026
…es (#200330)

* fix(gateway): preserve clean exit while releasing retained state leases

Keep cleanup-worker admission open until canonical state lease finalizers finish, while sealing ordinary clients and canceling queued writes immediately. Then close the SQLite host and clear its successful retirement bookkeeping.

The retained-lease singleton regression is red on the base; 73 focused tests pass with the fix. A synthetic owner process reproduces exit 1 on the base and 4bce760, and exit 0 with the fix. The current faulty callback ordering comes from 83064b3 (#188496), before the reported release range. Production delta: +3 lines. Codex autoreview: scoped-clean at P0-P2.

* style(perf): brace the shutdown measurement abort wait

(cherry picked from commit f5d74a5)

Co-authored-by: RomneyDa <[email protected]>

Release adaptation: none; source change applies to the pinned beta baseline.

Follow-up disposition: bounded pinned-main same-path audit recorded in the lane report; cross-lane changes remain parent-owned.

(cherry picked from commit afe57940d76947a5931805bce036549abbb6491e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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