Skip to content

perf(gateway): release snapshot tracking and retired plugin resources - #166683

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-diagnostics-gc-and-buffer-side-effects-20260919
Oct 7, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-diagnostics-gc-and-buffer-side-effects-20260919

Conversation

@steipete

@steipete steipete commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Heap snapshots leave a long-lived Gateway doing extra work on every moving GC, and plugin replacement can retain retired plugins' subscriptions and buffers by skipping their runtime cleanup callbacks.

User Impact

Diagnostic snapshots release V8 tracking before returning. Reloading or updating a plugin runs its registered runtime/session cleanup before disposing the old instance, allowing the replacement to acquire shared resources and release retained buffers.

Clearing V8's object-ID map means separate RPC snapshots support individual retaining-path analysis, but not identity-based cross-snapshot comparisons. Use one continuously attached debugger in an isolated process for stable IDs. Snapshot files remain private and local, with the existing heap limit and cooldown. Snapshot capture now shares the existing profiler conflict checks and mutual exclusion. No configuration or schema changes.

Why This Change Was Made

writeHeapSnapshot() releases the in-memory snapshot object after serialization but leaves HeapObjectsMap and object-move tracking active. HeapProfiler.disable clears both (Node 26.10.0 V8 snapshot owner, inspector cleanup). The snapshot owner now uses the existing diagnostic capture lifecycle, including disable/disconnect on failure, instead of maintaining a separate capture lock. Post-capture heap metadata is still sampled synchronously before file metadata I/O.

Plugin reload previously called instance.dispose() before registry retirement supplied the host cleanup callback; retirement then saw an already-disposing instance and skipped that callback. The first disposal now invokes the existing lifecycle callback loop through one extracted owner operation. Scheduler and persistent-state retirement retain their later ordering, when the successor's declarations are available.

Production delta: +45 lines (147 added / 102 removed, excluding tests, test support, and docs). The lifecycle callback loop is moved, not duplicated. Growth is required to separate early callback cleanup from later scheduler/persistent-state retirement and pass that cleanup into first disposal; combining these phases would change retained-state semantics. The independent snapshot lock/cleanup path is removed.

Evidence

Real dist Gateways on Node 26.10.0, with a 512 MiB synthetic numeric heap plus retained objects and steady allocation churn. Linux AWS measurements use jemalloc 5.3.0. No production snapshots or profiles were taken.

Experiment Original Fixed
Moving-GC cost after one snapshot 0.334 s/min baseline → 4.616 / 4.582 s/min 0.328 baseline → 0.494 / 0.283 s/min
V8 marker object ID after snapshot Nonzero; explicit disable changes it to zero and restores 0.239 s/min GC Already zero before the rig performs any cleanup; native regression also passes
Five reloads + two local-path install updates 0 cleanup calls; 8 retained registration states/modules; ArrayBuffers 77.52 → 532.85 MiB 7 cleanup calls; only current registration/module retained; ArrayBuffers 77.46 → 84.78 MiB

The snapshot comparison keeps allocation throughput steady (57.4–59.4 million objects/min). A literal-allocation control becomes dominated by major collections, consistent with pretenuring, and shows a smaller effect (0.355 → 0.426 / 0.387 s/min); the persistent young-allocation fixture exposes the moving-GC cost directly.

A separate 60-second sampling heap profile did not reproduce a leak: external memory 10.235 → 10.233 MB, ArrayBuffers 5.700 → 5.700 MB, returned profile 25,337 bytes, and the isolate-global sampler reports inactive after completion. No sampling production change is made. The earlier observed external-memory step remains unattributed. ESM module-level allocations can also remain in Node's module cache; explicit cleanup of plugin-owned payloads is still necessary.

Validation provider/lease: aws, cbx_b57cabd385a4 (r7a.large, 16 GiB), following a Blacksmith sync timeout. Initial native probes, build, and control rigs ran on blacksmith-testbox, tbx_01m4bcfj1vp6rmvq1x0a1xz6ar (runner).

  • Original-code negative controls fail for both defects: snapshot marker ID 7167 instead of 0; replacement retains the old shared-resource owner instead of the new one.
  • Focused snapshot, heap-profile, reload recovery, and sibling tests: 288 passed. pnpm test <file> --maxWorkers=1 remote wall times: snapshot 32.93 s, heap profile 31.47 s, recovery 62.88 s. Added native-state case 0.54 s and added runtime-lifecycle case ~0.3 s of test time; no new suite/process boots for the reload case. Hosted CI run 37662483327 passed on head 905cef204b41df607ce6690d986fec29eb9af8e9 in 784 wall seconds (13m 04s); its 34 Node jobs used 9,781 aggregate job seconds, with a 659-second longest shard. These are the full selected CI workload costs, not the incremental cost of the new assertions.
  • node --import ./scripts/tsx.mjs scripts/check-export-name-collisions.mts: passed. node scripts/run-vitest.mjs test/scripts/eager-import-closure.test.ts test/scripts/pr-wrapper-source-closure.test.ts: 20 passed (included above). No new test file.
  • Final snapshot metadata negative control: 8192-byte delayed reading fails; synchronous 1024-byte reading passes. All eight final snapshot cases and focused lint pass.
  • node scripts/check-changed.mjs --staged: passed on the final source, including production types, all 27 test type graphs, source guards, dead-export scans, lint, and import cycles. pnpm build: passed in 4m 11.9s on the final source. The first gate passed types/source guards and found one missing-braces lint error in the new test; that error is fixed.
  • Independent Codex autoreview: scoped-clean, no accepted/actionable P0–P2 findings. The only subsequent edit adds lint-required braces to the test environment loop.

The final combined AWS run (run_cae9451499155d52cd6bbdc0c1ac3c25) completed with exit 0. Its initial coordinator receipt POST returned HTTP 500; crabbox receipt subsequently returned the signed terminal record confirming exit 0 and an untruncated retained log. No workload rerun was needed. Both final rigs passed after the build: node --import ./scripts/tsx.mjs .local/lane609/rig-moving.mjs snapshot … and node --import ./scripts/tsx.mjs .local/lane609/plugin-rig.mjs cjs true … runtime. The isolated synthetic fixtures were task-local.

Release V8 object-move tracking through the shared diagnostic capture owner after heap snapshots. Run runtime lifecycle cleanup at the first plugin instance disposal so reload and install replacement do not retain old subscriptions and buffers.

Document that RPC snapshots reset object IDs and preserve snapshot-time heap metadata. Native regressions, plugin recovery coverage, the changed-file gate, build, and Node 26.10.0 synthetic Gateway measurements passed remotely.
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime plugin: file-transfer size: M labels Oct 7, 2026
@steipete
steipete marked this pull request as ready for review October 7, 2026 17:52
@steipete
steipete enabled auto-merge (squash) October 7, 2026 17:55
@steipete
steipete merged commit 32f13ef into main Oct 7, 2026
213 of 216 checks passed
@steipete
steipete deleted the steipete/perf-micro-diagnostics-gc-and-buffer-side-effects-20260919 branch October 7, 2026 18:07
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 8, 2026
…openclaw#166683)

Release V8 object-move tracking through the shared diagnostic capture owner after heap snapshots. Run runtime lifecycle cleanup at the first plugin instance disposal so reload and install replacement do not retain old subscriptions and buffers.

Document that RPC snapshots reset object IDs and preserve snapshot-time heap metadata. Native regressions, plugin recovery coverage, the changed-file gate, build, and Node 26.10.0 synthetic Gateway measurements passed remotely.
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 plugin: file-transfer size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant