Repository navigation
perf(plugins): bound retained heap across runtime reloads - #166751
Conversation
Release retired registry and cache graphs held by inherited async scopes, closed schedulers, cancellation stacks, and lifecycle lease closures. Keep selected generations only through each admitted call or stream's lifetime, including nested calls under long-running consumers. Preserve adopted-owner runtime access, prepared selections, expired-lease refusals, and resolved plugin values while reducing reload heap retention.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed October 7, 2026, 7:15 PM ET / 23:15 UTC (Revision 2). ClawSweeper reviewWhat this changesThe PR releases retired plugin registries and cache inventories after runtime work settles while preserving pending calls, streams, and adopted runtime access. Merge readiness✅ Ready for maintainer review This remains a useful, distinct retention repair that current main does not provide. No actionable introduced correctness or security defect was found. Priority: P2 Review scores
Verification
How this fits togetherOpenClaw’s plugin runtime selects registered capabilities for Gateway requests and background services. Reloads replace registry generations while admitted operations finish against their selected generation. flowchart TD
A[Gateway requests and services] --> B[Live plugin admission]
B --> C[Selected registry generation]
C --> D[Calls and streams]
E[Plugin reload] --> F[Replacement registry]
F --> B
D --> G[Settlement and cleanup]
G --> H[Release retired references]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +401, Tests +1388, Docs +8. Total +1797 across 33 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep registry custody with finite invocation, stream, and cleanup owners while detached resources retain only borrowed views and live plugin identity. Do we have a high-confidence way to reproduce the issue? Yes: current-main strong ambient references provide a clear retention path, and the captured body reports a real Gateway reload reproduction. This reviewer did not execute that reproduction. Is this the best way to solve the issue? Yes: releasing references at existing settlement boundaries repairs the retaining edges without adding another lifecycle owner or changing stored configuration. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against b300e0edea5e. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
Keep direct registry-scope callbacks alive through their returned completion, then release custody so detached native resources cannot retain the registry. Reuse runtime namespace facades per source while selecting the current registry at invocation time. This preserves cold native-command authorization and Discord STT interception across runtime adoption and source replacement.
…66751) * perf(plugins): bound retained heap across runtime reloads Release retired registry and cache graphs held by inherited async scopes, closed schedulers, cancellation stacks, and lifecycle lease closures. Keep selected generations only through each admitted call or stream's lifetime, including nested calls under long-running consumers. Preserve adopted-owner runtime access, prepared selections, expired-lease refusals, and resolved plugin values while reducing reload heap retention. * fix(plugins): preserve pending scopes and runtime facade identity Keep direct registry-scope callbacks alive through their returned completion, then release custody so detached native resources cannot retain the registry. Reuse runtime namespace facades per source while selecting the current registry at invocation time. This preserves cold native-command authorization and Discord STT interception across runtime adoption and source replacement.
What Problem This Solves
Repeated plugin runtime reloads retained retired registries and cache inventories through long-lived native async resources.
User Impact
Retired inventories can be collected after admitted work and cleanup settle. Pending calls and streams keep their selected generation across adoption, while completed nested calls release their own hold even when an outer monitor remains active. Expired callbacks continue to fail instead of falling back to a newer registry.
Why This Change Was Made
Host-created scopes borrow their registry; invocation and stream owners retain it through settlement. Preparation scopes and closed service schedulers release their references. Cancellation paths reuse the shared error-stack materializer, and provider-policy caches use their existing publication revision instead of retaining a registry solely for identity.
Service startup drops caller generation ancestry before entering its own context. Lifecycle-lease capabilities are constructed outside the cache-owning function, preventing escaped guard closures from retaining an operation's inventory. Lease validation, refusal, and cleanup behavior remain unchanged. HTTP registration resolves a handler's adopted owner before reading an expired ambient scope.
Direct registry-scope callbacks retain their selection until their returned operation settles. Runtime namespace facades remain stable for their source surface while each call resolves the current registry and enforces revocation. This preserves cold command-router startup and Discord STT interception without extending detached-resource lifetimes.
Production delta: +401 lines. The growth preserves mutable/enumerable scope behavior, explicit prepared selections (including empty views), pending invocation/iterator lifetimes, and typed runtime APIs after adoption. The superseded strong-reference paths and cache-specific error helper are removed; each logical invocation uses one finite registry holder.
Evidence
The reproduction uses a built Gateway, real
plugins.reloadcalls, and a process-owned filesystem watcher pool held open across reloads. Every cycle verifies startup, stop, and disposal; heap samples run after full GC.The final six reloads stayed within 230.57–230.76 MiB. Closing the watcher pool released 0.95 MiB, versus 147.76 MiB immediately before the lease fix. Two extra fixture roots remain, with no count growth after reload 2. The original baseline was
de38a045; an intermediate candidate at7a8e5a19still grew 230.30 MiB over 12 reloads. Final proof was rebuilt fromc410e1a2with the reviewed follow-up repair; its source hashes were verified before execution.Initial validation used authorized AWS Crabbox fallback
cbx_bb265f4188f7, Node 26.8.2, after Blacksmith admission stalled. Earlier controls usedcbx_d323bc860f88.pnpm test src/plugins/plugin-instance.retention.test.ts --maxWorkers=1: 13 passed, 25.74s wall. Other measured file costs: value views 23.20s, HTTP registry 25.90s, service context 25.45s, lifecycle lease 47.89s.pnpm build: passed in 5m 11s, including CLI bootstrap and SDK export checks. Export-name collision guard and both eager-import/PR-wrapper source-closure files: passed, 20 closure tests.node scripts/check-changed.mjs --staged: passed, including all 27 test typecheck groups, lint, dead exports, import cycles, and storage/security guards. The last lint-first run also passed all 181 targeted tests across 10 files after the final local-state and fixture cleanup. Independent review completed two passes with no accepted P0–P2 findings.The initial staged gate checked exactly the 33 patch files. Initial validation run:
run_d3fbfbfb247407dd582c38e67a101710, 16m 18s command time. File-length advisories remain in the registry runtime, instance, and HTTP-test owners; no file-length suppression or baseline exception was added. The initial hosted run exposed two task-caused regressions, both reproduced and repaired at their owners: premature collection during cold/compactrouting and unstable runtime-facade identity in Discord STT capture. No CI reruns were used.Follow-up validation on AWS
cbx_af517903d88b, runrun_72ad335650ad6838a0fa2b36cf003b7f: 308 tests across 22 files passed, including the exact command-router test under the pinned Bun runtime and the real Discord capture E2E. The new scope regression fails before the fix in all three completion modes; the STT regression fails before the facade fix.pnpm test <file> --maxWorkers=1measured 23.21s for Gateway request scopes and 28.86s for runtime config. The 18-file sibling run took 125.43s. Independent follow-up review found no actionable P0–P2 defects. The fresh 24-reload measurement above is from this repaired candidate.The final
node scripts/check-changed.mjs --stagedrun passed on the same AWS lease:run_6bfa2d64bc5851ae4d2a84439ad0f8f4, 11m 20s command time. All 27 test typecheck groups, core types, lint, dead exports, import cycles, and storage/security guards passed. The final test-only type narrowing was rechecked with all 15 scope tests, and a fresh independent review was clean. The lease was released after validation.This reproduction isolates plugin-generation retention. It does not establish that these paths explain all long-running Gateway heap growth. No patched production deployment or long-duration patched production measurement is included.
Hosted CI run 37700574898 passed for
7b8c1c1293aa: 678 seconds wall time, with 19,021 summed job-seconds across 79 executed jobs. The PR merged as40d735e451b1; its 33-file scope and presence onmainwere verified. No CI rerun or inherited-failure admin exception was used.