Skip to content

bb plugin reload retains the previous module graph for the lifetime of the server (~73 MB per reload for a path: plugin) #4159

Closed

Description

@mattwyckhouse

Environment. bb 0.43.3 (desktop, macOS arm64), server running under ELECTRON_RUN_AS_NODE=1
(Node 24 / V8 15.2 in Electron 44). Plugin installed from a path: source (the Factory plugin, github.com/mattwyckhouse/bb-plugin-factory); server entry is a
TypeScript tree of 112 modules (5.15 MB of source) loaded through jiti.

Symptom. Server heap grows by a fixed step on every POST /api/v1/plugins/reload (and on
every disable + enable) of a path-installed plugin and never comes back after a full GC. On a
host that reloaded the plugin 40 times in 18.9 h the server died with
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed after a last-resort Mark-Compact that freed
nothing (3038.2 (3192.8) -> 3038.0 MB). A process restart is the only way to reclaim.

Evidence (installed bundle server/dist/start-server.js).

  • Path and source-builtin plugins are loaded with createJiti(import.meta.url, { moduleCache: false, alias })
    and await jiti.import(entry) (334276–334281) behind a registerHooks resolve hook that stamps
    ?bbPluginLoad=<rootId>.<epoch> onto every file: URL under the plugin root (333099–333125,
    stamp at 333120). bumpMutableRootGeneration (333173) mints a new epoch per load. The source
    comment (plugin-runtime.ts) explains why: "Node's ESM registry keys modules by resolved URL
    forever", so without the stamp a reload would return the stale module.
  • The cost is the mirror image: every generation's modules stay in the ESM registry with no
    eviction API. disposePluginInstance (334391–334440) closes websockets, disposes the host
    worker, aborts tool calls and interactions, stops services, runs dispose hooks, drains
    invocations and closes every tracked database handle — and cannot touch the registry.
    evictCommonJsCache (333161) does reclaim the CommonJS half. setEnabled(id, false) →
    disposeOne (334441) never calls forgetMutableRoot; remove (336472) only deregisters the
    resolve hook. So disable/enable retains exactly like a reload.
  • resolveServerEntry (333775–333779) returns manifest.serverEntry for path: sources and
    only checks dist/server.js for other source kinds, so a path: plugin is always transpiled
    from source.

Measurement (out of process, reproducible). Harness: bb's bundled jiti 2.7.0, bb's exact
options, a faithful copy of the resolve hook, --expose-gc, eight forced GCs before each sample,
metric used_heap_size + external_memory. Node 26.8.2.

Entry Steady-state retention per load
server.ts source tree (112 modules), hook active 73.5 MB (loads 3–4; 73.4–73.5 across two independent 4–10 load runs)
prebuilt single-file ESM dist/server.js (4.7 MB), hook active 6.4 MB
same source tree, resolve hook made a pass-through (control) 0.1 MB after load 2 — the registry returns the cached module
prebuilt bundle as CommonJS (no "type":"module") 0.1 MB

40 × 73.5 MB = 2.94 GB, within 3 % of the 3,038 MB live heap at the failing GC. (The reload term
is not the whole story: a reload-free process on the same host also grows under load, so the
issue is a pace-setter, not the sole retainer.)

Reproduction. Install any path: plugin whose server entry imports a few MB of TypeScript.
Call POST /api/v1/plugins/reload 20 times. Watch process.memoryUsage().heapUsed (or the
process footprint) step up by a constant each time and never drop after a forced GC. Repeat with
disable then enable: same step.

Proposed fix (any one helps).

  1. Key the generation stamp on a per-file content hash instead of a per-reload counter, so a
    reload mints new URLs only for files whose content changed. Keeps the freshness guarantee and
    turns a ~73 MB reload into roughly the size of the changed files.
  2. Honour a prebuilt dist/server.js for path: sources too (opt-in manifest flag), since
    resolveServerEntry already prefers it for every other source kind and it retains 11× less.
  3. Structurally: evaluate the plugin server graph in a worker or child process whose termination
    frees everything, the way disposePluginHost already frees the host artifact.

Also useful: count generations per plugin, warn past a threshold, and expose the count in
GET /api/v1/plugins.


Filed from an audit of bb 0.43.3 by the Factory plugin (github.com/mattwyckhouse/bb-plugin-factory). Method, re-derived numbers and the adversarial review of this report: plans/076-host-sustainable-line/bb-core-issues/REVIEW.md in that repository.

Activity

  1. added theissue type on Sep 23, 2026
  2. added
    plugins
    partial-repro
    on Sep 23, 2026
  3. bb-slop-cop commented on Sep 23, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    Bug · High priority · High effort. Applied plugins, perf, and partial-repro; no existing classification was replaced.

    Reproduction and root-cause report: PARTIALLY REPRODUCED, high confidence in the native ESM root cause. Generation-stamped URLs retain prior module state after root deregistration. Two clean-checkout loader runs retained eight distinct states (~16.36 MiB); the control retained one (~2.2 MiB). The report includes the repeatable test and failing reclamation assertion.

    The reported TypeScript amplification and Electron crash remain unverified; the small TypeScript fixture did not reproduce retention on Node 22. No PR: reclaiming module graphs safely requires a lifecycle/architecture decision beyond the simple-fix gate.

  4. luketraas commented on Sep 29, 2026

    @luketraas
    Contributor

    Re-checked on main 66bbc1a with Node 22.22.3 on macOS: in-process createPluginService, a generated path plugin of 110 TypeScript modules, --expose-gc, 8 forced GCs per sample.

    • Default compiled loader (Move plugin compilation off the server event loop #3681), unchanged reloads: about +0.1 MiB per reload.
    • Legacy JITI loader: +0.1 MiB (unchanged) and +0.2 MiB (edited) per reload, so I could not reproduce the 73.5 MB per load on Node 22.
    • Default loader, plugin edited before each reload, data directory behind a symlink: +11.3 MiB per reload and out of memory under a 230 MiB heap cap within 30 reloads. The loader evicted the compiled server.cjs from require.cache by its unresolved path while Node keys the cache by real path. Evict compiled plugin modules by their resolved filename #4494 fixes that. With a non-symlinked data directory the same capped run completes.

    So the mechanism reported here no longer applies to the default loader on Node 22. The leak I could reproduce is the symlink case. I did not test the Electron runtime from the report.

    AGENT GENERATED

  5. ymichael commented on Sep 30, 2026

    @ymichael
    Collaborator

    Resolved for the default plugin loader by #3681 and #4494, both present in adbce96.

    Verified on Node 22.23.1:

    • All 8 prebuilt-server-loading regression tests passed through Turbo, including cache eviction with a symlinked data directory.
    • Re-ran the 110-module TypeScript heap harness with a symlinked data directory, source edits before every cycle, forced GC, and a 230 MiB heap cap.
    • 30 reloads completed: memory plateaued around 180 MiB without sustained per-reload growth.
    • 30 disable/enable cycles completed: memory plateaued around 179 MiB without sustained per-cycle growth.

    The original Electron runtime and the optional legacy JITI loader were not verified in this run. Closing based on the default-loader fixes and verification above; a reproduction on the current default loader would warrant reopening.

    AGENT GENERATED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    partial-reproperfplugins

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions