Skip to content

fix(state): release async context when lease timers stop - #149645

Merged
Patrick-Erichsen merged 5 commits into
mainfrom
pe/plugin-reload-lease-cleanup
Sep 16, 2026
Merged

Patrick-Erichsen merged 5 commits into
mainfrom
pe/plugin-reload-lease-cleanup

Conversation

@Patrick-Erichsen

@Patrick-Erichsen Patrick-Erichsen commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Landed: merged into main as 8ee2e683ea97fb249ce8702b318017ba8d9f1155 on September 16, 2026. Required CI passed on the final PR head. All five stack layers are merged; final receipts and before/after proof. The accepted partial repair boundary is unchanged: ESM forced-GC growth remains 36.65 MiB against the 32 MiB limit.

What Problem This Solves

Completed or paused leases kept canceled native timer handles and their async context reachable.

User Impact

Stopped lease owners release their timer and worker references, allowing retired plugin generations to be collected.

This is one layer of a partial memory repair: the combined ESM forced-GC endurance result is still 36.65 MiB against a 32 MiB limit (failed).

Why This Change Was Made

Centralize timer stop-and-release for pause and final cleanup, then drop the stopped worker reference. Document the same handle-release obligation for plugin authors. No schema, persistence or configuration changes.

Evidence

Landing CI exposed a Reef test teardown bug: native reset preceded temporary-file deletion without draining async database admissions. Both fixtures now await the existing lifecycle close. The real Linux SQLite/inode probe changed from 511 failures to zero under the same 511 inode reuses; the complete Reef file passed 30/30 locally. No production changes, timeout increases or relaxed assertions. Local extension lint could not run through the borrowed-dependency guard; current hosted CI remains required. Reef cleanup proof.

The combined plugin source passed 356/356 tests across 18 files before this test-only cleanup; every production byte remains identical. Integration results.

The four remaining plugin layers were refreshed onto main 08584b3d34c526ad438b39b32931a64a6c61a992. Every authored plugin-layer added/removed line is unchanged. Temporary Goal-test, planner-fixture and chat-renderer corrections were removed because main now owns their repairs: planner fixture, Goal read reuse and UI cleanup. The three files now exactly match main. The original stricter Goal bound passes all12 cases; removing canonical entry reuse still fails at four reads against its three-read limit. Fresh hosted CI is required for the refreshed heads. Refresh proof and identities.

All three collection regressions failed before repair and passed afterward: completed timers, completed worker heartbeat and pause after renewal. 101 distinct lease/lifecycle cases passed. Paired five-reload Gateway heaps showed cleared lease fields after repair and removal of the three diagnosed owner paths.

Proof source: 6320346a6d281d4ab570cd67b241ec5d9a74ced8; the originally published stack top 14a29985cfbc1b6f5e379b225ae7c00078061c16 had the identical Git tree (bcdb4a87fb86add0a49982acf26af4568056f61b). The source-built live runs completed all four 20-warmup/500-measured lanes with functional checks passing and stable Gateway PID/socket. Real CLI install/reinstall auto-application and a separate physical shutdown/relaunch also passed. The 500-update trigger was source edits plus explicit plugins.reload; HTTP/tool checks were sampled, with full per-update tuples verified in the short diagnostics.

Layer proof and reproduction commands · Before/after log excerpts · Live reload/restart measurements · Fresh review and commit provenance.

Fresh publication validation: P0–P2 cumulative review found no actionable defects. At publication, the stack plus main b036baa80649 passed 99 tests across 11 files, zero failed/skipped, including cache, registry-retention and state-lease owner/sibling coverage. The first integration attempt lacked package-local dependency links; the same command passed after restoring the existing links without an install or source changes. This combined-tree check supplements the exact-source build and live evidence above.

Landing follow-up: two test-only corrections now distinguish retained task bytes from Vite source-map text and bundled companion reuse from installed-plugin capture. The paired reproductions passed 36/36 lifecycle tests and 8/8 companion cases; both scoped P0–P2 reviews and full changed-file gates passed. These follow-up commits change tests only; the five reviewed production patches are preserved. CI repair proof and commands. Those original test repairs remain in their owning layers; their historical results retain their source identities.

Scope and limitations
  • Earlier whole-session shared-OCM isolation failed; later private-store cleanup and named-epoch peer continuity passed separately. The initiating writer was not established.
  • Test counts from overlapping historical suites are not additive. No real external channel account or universal plugin compatibility claim.
  • Production +11/−6 (net +5); tests/support +111/−0; docs +13/−0. The shared stop helper replaces duplicated cancellation and drops handles.
  • No new configuration, public protocol or persistent-store contract.

Stack order (review bottom to top):

  1. fix(plugins): release retired callers retained by cached errors
  2. fix(plugins): release predecessor registries after replacement
  3. fix(state): release async context when lease timers stop ← this PR
  4. fix(plugins): reuse bundled setup code across reloads
  5. fix(plugins): reuse the published inbound registry during reload

@clawsweeper

clawsweeper Bot commented Sep 16, 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.

@Patrick-Erichsen
Patrick-Erichsen marked this pull request as ready for review September 16, 2026 03:42
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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. labels Sep 16, 2026
@clawsweeper

clawsweeper Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 16, 2026, 2:29 AM ET / 06:29 UTC (Revision 7).

ClawSweeper review

What this changes

Release canceled lease timer and worker references so retired callers can be collected, add collection regressions, and document timer cleanup for plugin authors.

Merge readiness

⛔ Blocked before merge - 1 item remains

This remains a useful, focused repair absent from current main and the latest release. No actionable patch defects were found, and the documented acceptance of partial memory improvement resolves the earlier scope concern.

Priority: P2
Reviewed head: 4e87cffda8a5523569e4a44caaa76ceea9e2ba69

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level repair with strong scoped proof and no actionable findings; the wider memory limitation is explicitly preserved.
Proof confidence 🦞 diamond lobster (5/6) ✨ media proof bonus Sufficient (linked_artifact): Inspected linked evidence maps the lease cleanup to matched five-reload foreground Gateway runs with cleared timer/worker fields and removed retaining paths, supplemented by three failing-before/passing-after collection cases. Broader live results retain their recorded source identity and failed ESM memory bound.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (linked_artifact): Inspected linked evidence maps the lease cleanup to matched five-reload foreground Gateway runs with cleared timer/worker fields and removed retaining paths, supplemented by three failing-before/passing-after collection cases. Broader live results retain their recorded source identity and failed ESM memory bound.
Evidence reviewed 8 items Introduced cleanup preserves lease semantics: The pinned delta centralizes cancellation and clears both timer variables during pause and final cleanup. The worker reference is released only after stop succeeds; closed-state checks, drainage, durable ownership verification, and guarded database release remain intact.
Current main still retains stopped handles: Inspection of fetched main found cancellation without clearing heartbeat and expiryTimer in pause/final cleanup, and no final worker-reference reset. The related merged cached-error repair addresses a different retaining path.
Latest release does not contain this cleanup: The lease implementation at supplied release v2026.9.4 likewise cancels timers without releasing their stored handles.
Findings None None.
Security None None.

How this fits together

OpenClaw's state leases serialize trusted operations such as plugin lifecycle changes. Their timers maintain ownership while work runs; stopping a lease must also release references that retain the caller's async context.

flowchart TD
  A[Plugin lifecycle work] --> B[State lease owner]
  B --> C[Heartbeat and expiry timers]
  B --> D{Pause or complete}
  D --> E[Cancel timers and release handles]
  E --> F[Caller context can be collected]
  D --> G[Existing ownership and release checks]
Loading

Before merge

  • Resolve merge risk (P1) - The combined reload stack still exceeds its ESM forced-GC memory bound: 36.65 MiB against 32 MiB. The recorded disposition accepts this partial repair; it does not establish complete reload-memory acceptance.
Agent review details

Security

None.

PR surface

Source +5, Tests +111, Docs +13. Total +129 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 1 11 6 +5
Tests 2 111 0 +111
Docs 1 13 0 +13
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 135 6 +129

Review metrics

Metric Value Why it matters
Regression coverage 3 collection scenarios Completed local timers, completed worker heartbeat, and pause after renewal each have recorded before-fail and after-pass results.
Production versus regression growth Production net +5 lines; tests/support +111 lines The small production increase replaces duplicated cancellation with one stop-and-release operation; the tests exercise collection through the real lease owner.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Keep resource release in the existing lease owner and preserve the remaining native ESM growth as separate work without relaxing its acceptance bound.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Keep resource release in the existing lease owner and preserve the remaining native ESM growth as separate work without relaxing its acceptance bound.

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

Yes: current-main source retains canceled timer handles, and the supplied collection regressions isolate completed, worker, and paused leases. This read-only review inspected the recorded failures and successes without executing them.

Is this the best way to solve the issue?

Yes: clearing references at the existing cancellation points addresses the retaining owner directly while preserving renewal, ownership verification, and database-release behavior.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P2: This is a bounded memory-retention repair for completed or paused leases, with no demonstrated urgent runtime outage.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (linked_artifact): Inspected linked evidence maps the lease cleanup to matched five-reload foreground Gateway runs with cleared timer/worker fields and removed retaining paths, supplemented by three failing-before/passing-after collection cases. Broader live results retain their recorded source identity and failed ESM memory bound.
  • proof: sufficient: Contributor real behavior proof is sufficient. Inspected linked evidence maps the lease cleanup to matched five-reload foreground Gateway runs with cleared timer/worker fields and removed retaining paths, supplemented by three failing-before/passing-after collection cases. Broader live results retain their recorded source identity and failed ESM memory bound.

Evidence

What I checked:

  • Introduced cleanup preserves lease semantics: The pinned delta centralizes cancellation and clears both timer variables during pause and final cleanup. The worker reference is released only after stop succeeds; closed-state checks, drainage, durable ownership verification, and guarded database release remain intact. (src/state/openclaw-state-lease.ts:416, 4e87cffda8a5)
  • Current main still retains stopped handles: Inspection of fetched main found cancellation without clearing heartbeat and expiryTimer in pause/final cleanup, and no final worker-reference reset. The related merged cached-error repair addresses a different retaining path. (src/state/openclaw-state-lease.ts:575, cda032ef33a1)
  • Latest release does not contain this cleanup: The lease implementation at supplied release v2026.9.4 likewise cancels timers without releasing their stored handles. (src/state/openclaw-state-lease.ts:578, 3a9d69db306c)
  • Node timer contract supports the retention mechanism: Inspected Node v24.19.0 through the GitHub contents API: timer initialization stores AsyncContextFrame.current() on the resource; clearTimeout clears its callback and unenrolls it. The affirmative dependency signal is the patch's native timer calls and AsyncLocalStorage collection tests. (lib/internal/timers.js:164, cdc1b38d40cb)
  • Inspected owner-specific live and regression evidence: Read 03-stopped-lease-handles.md, the stopped-lease historical log excerpts, and live-proof-summary.json from https://gist.github.com/Patrick-Erichsen/8ccf64da00f2486dcb05ebf0adc3e711 through the public GitHub API. All three collection cases fail before repair and pass afterward. Matched five-replacement foreground Gateway runs report cleared lease fields and removal of the three diagnosed retaining paths. Combined live evidence identifies source 6320346a6d281d4ab570cd67b241ec5d9a74ced8 on Node 24.19.0/macOS and explicitly preserves the failed ESM endurance result.
  • Prior review disposition and continuity: The supplied previous review contains no findings or published rank-up moves. GitHub contents comparisons confirmed that all four reviewed files are byte-identical to the previous reviewed head a099b9ddcf474478c4ee3827b72b1d63bed2d307. The discussion at fix(state): release async context when lease timers stop #149645 (comment) explicitly records acceptance of the focused stack with the unchanged 36.65 MiB versus 32 MiB failure. (4e87cffda8a5)

Likely related people:

  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; 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.

History

Review history (6 earlier review cycles)
  • reviewed 2026-09-16T03:48:05.897Z sha 6da8b2e :: needs maintainer review before merge. :: none
  • reviewed 2026-09-16T04:16:28.178Z sha c456b25bdc229c81e427b2328fef24eb4dd410c7 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-16T04:48:35.897Z sha c43309a2ac34df630eeeb7b97d0b216f2c973539 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-16T05:10:08.614Z sha caab29649a2b4b74aab6a6eaca506fc61bda8d8e :: needs maintainer review before merge. :: none
  • reviewed 2026-09-16T05:29:24.288Z sha caab29649a2b4b74aab6a6eaca506fc61bda8d8e :: blocked before merge. :: none
  • reviewed 2026-09-16T06:06:38.463Z sha a099b9ddcf474478c4ee3827b72b1d63bed2d307 :: needs maintainer review before merge. :: none

@Patrick-Erichsen
Patrick-Erichsen force-pushed the pe/plugin-reload-lease-cleanup branch from 6da8b2e to c456b25 Compare September 16, 2026 04:08
@Patrick-Erichsen
Patrick-Erichsen force-pushed the pe/plugin-reload-resolver-retention branch from a0f524e to ab6e5b7 Compare September 16, 2026 04:08
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation plugin: file-transfer size: S maintainer Maintainer-authored PR labels Sep 16, 2026
@Patrick-Erichsen Patrick-Erichsen self-assigned this Sep 16, 2026
@Patrick-Erichsen
Patrick-Erichsen force-pushed the pe/plugin-reload-lease-cleanup branch from c456b25 to c43309a Compare September 16, 2026 04:38
@Patrick-Erichsen
Patrick-Erichsen force-pushed the pe/plugin-reload-resolver-retention branch from ab6e5b7 to 9deac96 Compare September 16, 2026 04:38
@Patrick-Erichsen
Patrick-Erichsen force-pushed the pe/plugin-reload-lease-cleanup branch from c43309a to caab296 Compare September 16, 2026 05:01
@Patrick-Erichsen

Copy link
Copy Markdown
Contributor Author

Maintainer disposition for the review’s memory-limit item: land this focused repair as part of the five-PR retention stack, subject to CI and the native landing gates. The maintainer explicitly approved landing the stack with the documented remaining failure.

This accepts the independently demonstrated owner repairs; it does not claim complete reload-memory acceptance. The 500-update ESM forced-GC result remains 36.65 MiB against the unchanged 32 MiB limit. The remaining native ESM module-job growth stays open for further work; no threshold increase, idle subtraction, or plateau claim is being used to turn that result green.

The linked proof separately demonstrates caller collection, predecessor release, stopped-handle cleanup, bundled setup code reuse, and published-registry reuse. Functional reload/install/restart observations retain their recorded scope. Installed/source plugins keep their captured generations, and compiled bundled code replacement retains its documented build-and-restart requirement.

Proof: https://gist.github.com/Patrick-Erichsen/8ccf64da00f2486dcb05ebf0adc3e711

@Patrick-Erichsen
Patrick-Erichsen force-pushed the pe/plugin-reload-lease-cleanup branch from caab296 to a099b9d Compare September 16, 2026 05:55
@Patrick-Erichsen
Patrick-Erichsen force-pushed the pe/plugin-reload-resolver-retention branch from 40af823 to 04d8b22 Compare September 16, 2026 05:55
@Patrick-Erichsen
Patrick-Erichsen force-pushed the pe/plugin-reload-lease-cleanup branch from a099b9d to 4e87cff Compare September 16, 2026 06:13
Base automatically changed from pe/plugin-reload-resolver-retention to main September 16, 2026 06:36
@openclaw-barnacle openclaw-barnacle Bot added channel: reef Channel integration: reef size: M and removed size: S labels Sep 16, 2026
@Patrick-Erichsen
Patrick-Erichsen merged commit 8ee2e68 into main Sep 16, 2026
209 of 250 checks passed
@Patrick-Erichsen
Patrick-Erichsen deleted the pe/plugin-reload-lease-cleanup branch September 16, 2026 06:38
@Patrick-Erichsen

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 17, 2026
…9645)

* fix(plugins): isolate retained capability resolver scope

* test(plugins): run runtime registry state checks in host processes

* test(reef): drain state lifecycle before fixture removal

* fix(state): release stopped lease timer and worker handles

* docs(plugins): explain native timer handle cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: reef Channel integration: reef docs Improvements or additions to documentation maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. plugin: file-transfer proof: sufficient ClawSweeper judged the real behavior proof convincing. 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