Skip to content

fix(gateway): retain shared state until cleanup finishes - #142286

Merged
steipete merged 2 commits into
mainfrom
codex/split-host-cleanup-drain
Sep 8, 2026
Merged

steipete merged 2 commits into
mainfrom
codex/split-host-cleanup-drain

Conversation

@steipete

@steipete steipete commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Related: #140674

What Problem This Solves

Fixes an issue where Gateway shutdown or restart could close shared SQLite and reset plugin state while a slow cleanup callback was still using it. The initial timeout allowed shutdown to continue, but did not cancel the callback.

Why This Change Was Made

The existing shutdown owners retain actual hook and runtime cleanup work through their grace periods, let independent teardown proceed, and join that work before releasing dependent shared state. Active plugin host cleanup now finishes before the shared database closes. Registry retirement still revokes runtime authority immediately.

User Impact

Slow cleanup can finish using its existing resources without racing a closed database. A callback that never settles can hold graceful in-process shutdown open; existing forced process-exit behavior and initial grace periods are unchanged.

Evidence

Five native SQLite regressions failed before the repair: shutdown and pre-restart hooks, harness cleanup, and plugin lifecycle callbacks and their tracked child work. All 135 focused tests passed after the repair, along with the full changed-file gate and builds.

An isolated compiled Gateway with a synthetic plugin served a successful health check, then shut down through its actual signal handler. Shutdown, harness, and lifecycle callbacks remained pending beyond their five-second grace periods. Each performed a successful late SQLite read; independent service teardown advanced; the process then exited successfully and the captured connection closed. A read-only reopen preserved the schema version and passed the integrity check. This used the existing private-QA observation build and task-owned state; it does not claim live failed-startup or external MCP/LSP service coverage. Cleanup from a registry retired by an earlier hot reload remains a separate follow-up; this slice joins the active-registry close owner.

The focused suite took 43.449 seconds, the final changed-file gate 298.109 seconds, the ordinary build 258.446 seconds, and the compiled Gateway case 20.258 seconds. Initial test-type and synthetic-fixture failures were corrected without changing assertions, trust rules, or product timeouts.

@clawsweeper

clawsweeper Bot commented Sep 8, 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 docs Improvements or additions to documentation gateway Gateway runtime size: M maintainer Maintainer-authored PR labels Sep 8, 2026
@steipete
steipete marked this pull request as ready for review September 8, 2026 15:49
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. 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. labels Sep 8, 2026
@clawsweeper

clawsweeper Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 8, 2026, 12:16 PM ET / 16:16 UTC.

ClawSweeper review

What this changes

The PR keeps shared Gateway state available until shutdown callbacks and plugin cleanup actually finish, with regression tests and lifecycle documentation.

Merge readiness

⛔ Blocked before merge - 1 item remains

This remains a useful, focused repair: current main and the latest release retain the premature shared-state closure. No blocking patch defect was found. The related aggregate explicitly delegates this work to smaller PRs, and the protected maintainer submission should remain open.

Priority: P2
Reviewed head: d8e8bca0ec5d2cd9b51cd79d298936599ee8d6ef

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level repair with meaningful native SQLite regressions, explicit runtime observations, and a documented shutdown tradeoff.
Proof confidence 🌊 off-meta tidepool Not applicable: The ordinary contributor proof gate is exempt for this maintainer submission. Its supplied body reports the changed Gateway shutdown owner executing delayed callbacks against real SQLite through the compiled signal-handler path; no new authority boundary requires additional final-effect proof.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The ordinary contributor proof gate is exempt for this maintainer submission. Its supplied body reports the changed Gateway shutdown owner executing delayed callbacks against real SQLite through the compiled signal-handler path; no new authority boundary requires additional final-effect proof.
Evidence reviewed 9 items Current main still needs the repair: Fetched main closes the plugin state database before clearing the active plugin registry, and its finalization does not join the new cleanup scope.
Latest release retains the earlier ordering: The supplied latest-release revision for v2026.9.3 also closes the database before active-registry cleanup; the requested repair is not present there.
Actual completion precedes shared-state closure: Finalization drains tracked cleanup and media work, then awaits active-registry cleanup before closing SQLite. The introduced delta was inspected from the pinned merge base, separately from base-branch drift.
Findings None None.
Security None None.

How this fits together

Gateway shutdown coordinates hooks, plugin runtimes, network listeners, and shared SQLite state. Cleanup may outlast its initial grace period; this change lets independent teardown proceed while delaying dependent resource closure.

flowchart TD
  A[Stop or restart request] --> B[Start cleanup callbacks]
  B --> C[Wait through grace period]
  C --> D[Close independent listeners]
  D --> E[Join actual cleanup]
  E --> F[Finish active plugin cleanup]
  F --> G[Close shared state]
Loading

Before merge

  • Resolve merge risk (P1) - A cleanup callback that never settles can now keep an embedded Gateway close pending indefinitely; process-owned shutdown retains its existing forced-exit safeguards.
Agent review details

Security

None.

PR surface

Source +31, Tests +184, Docs +7. Total +222 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 3 95 64 +31
Tests 2 253 69 +184
Docs 2 7 0 +7
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 355 133 +222

Review metrics

None.

Merge-risk options

Maintainer options:

  1. Retain the documented shutdown tradeoff (recommended)
    Accept the explicitly stated wait for actual cleanup, with the existing process watchdog providing forced termination where OpenClaw owns the process.

Technical review

Best possible solution:

Retain the documented completion-before-release contract and existing process watchdog, while keeping previously retired-registry cleanup in its separately acknowledged follow-up.

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

Yes, source establishes the path: hold a cleanup callback beyond its grace period and let it access SQLite after teardown advances. The added regressions exercise this ordering; this review did not execute them.

Is this the best way to solve the issue?

Yes. Reusing the existing asynchronous work owner and joining cleanup before resource release addresses the lifecycle boundary without adding configuration, changing schemas, or extending initial grace periods.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: This repairs a concrete shutdown resource-lifetime race without evidence of a widespread urgent outage.
  • add merge-risk: 🚨 availability: The introduced final drains can prevent an embedded shutdown from completing when a cleanup callback never settles.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The ordinary contributor proof gate is exempt for this maintainer submission. Its supplied body reports the changed Gateway shutdown owner executing delayed callbacks against real SQLite through the compiled signal-handler path; no new authority boundary requires additional final-effect proof.

Label justifications:

  • P2: This repairs a concrete shutdown resource-lifetime race without evidence of a widespread urgent outage.
  • merge-risk: 🚨 availability: The introduced final drains can prevent an embedded shutdown from completing when a cleanup callback never settles.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The ordinary contributor proof gate is exempt for this maintainer submission. Its supplied body reports the changed Gateway shutdown owner executing delayed callbacks against real SQLite through the compiled signal-handler path; no new authority boundary requires additional final-effect proof.

Evidence

What I checked:

  • Current main still needs the repair: Fetched main closes the plugin state database before clearing the active plugin registry, and its finalization does not join the new cleanup scope. (src/gateway/server-close.ts:615, 512d2a0ad469)
  • Latest release retains the earlier ordering: The supplied latest-release revision for v2026.9.3 also closes the database before active-registry cleanup; the requested repair is not present there. (src/gateway/server-close.ts, 1391f7cd2d40)
  • Actual completion precedes shared-state closure: Finalization drains tracked cleanup and media work, then awaits active-registry cleanup before closing SQLite. The introduced delta was inspected from the pinned merge base, separately from base-branch drift. (src/gateway/server-close.ts:634, d8e8bca0ec5d)
  • Resource retention preserves registry retirement: Registry clearing still removes the active registry and retires its authority synchronously. The new drain delays physical reset; it does not reactivate the registry or introduce an authorization-bearing value. (src/plugins/runtime.ts:418, d8e8bca0ec5d)
  • Regression coverage exercises late native SQLite reads: Three Gateway cases cover shutdown hooks, pre-restart hooks, and harness disposal; two registry cases cover callbacks and tracked descendants. They assert successful late SQLite reads, delayed closure, and eventual cleanup rather than merely checking promise calls. Tests were inspected, not executed during this read-only review. (src/gateway/server-close.test.ts:270, d8e8bca0ec5d)
  • Documented availability tradeoff has an existing process owner: The CLI arms a shutdown timeout and process watchdog around server closure. Embedded callers can still wait indefinitely for nonsettling cleanup, as expressly documented in the PR body and hook documentation. (src/cli/gateway-cli/run-loop.ts:677, d8e8bca0ec5d)

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Josh Lehman: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete

steipete commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Retaining the documented shutdown tradeoff. A grace timeout permits independent teardown to advance; it cannot make a still-running cleanup callback safe to use after its database closes. Embedded close therefore waits for actual cleanup, and the existing process-owned watchdog remains unchanged. The native and compiled proofs cover continued teardown, late SQLite use, and eventual closure.

The review found no actionable patch defect. Earlier hot-retired registry cleanup remains the separately tracked follow-up described in this PR.

@steipete
steipete merged commit dbe985b into main Sep 8, 2026
253 checks passed
@steipete
steipete deleted the codex/split-host-cleanup-drain branch September 8, 2026 16:25
@steipete

steipete commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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 maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. 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