Repository navigation
Evict compiled plugin modules by their resolved filename - #4494
Merged
Merged
Conversation
After requiring a compiled plugin server artifact, the runtime deleted its require.cache entry using the artifact path as built from the data directory. Node keys require.cache by the resolved real path, so when the data directory path contains a symlink (for example /home on Fedora Atomic, a symlinked ~/.bb or BB_DATA_DIR, or macOS /var in tests) the delete missed. Every edited reload then kept the previous generation's module graph alive, and a reload with unchanged content returned the stale cached module without re-running its top-level code. Resolve the artifact filename with require.resolve before requiring it and evict that key. Measured with a 110-module, 4.8 MB TypeScript path plugin, symlinked data directory, edited before each reload: main retained 11.3 MiB per reload and died with "JavaScript heap out of memory" under a 230 MiB heap cap within 30 reloads; with this change the same run completes at 0.04 MiB per reload. The JITI and ESM registry retention reported in get-bb#4159 no longer applies to the default loader since get-bb#3681; this fixes the remaining CommonJS eviction gap. Refs get-bb#4159 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
Since #3681, path plugins compile to a content-addressed
server.cjsunder the data directory and load withrequire. After loading, the runtime deleted therequire.cacheentry using the artifact path as built fromBB_DATA_DIR, but Node keysrequire.cacheby the resolved real path. When the data directory path contains a symlink (for example Fedora Silverblue's/home→/var/home, a symlinked~/.bborBB_DATA_DIR, or macOS/varin tests), the delete missed. Each edited reload left the previous generation inrequire.cache, and a reload with unchanged content returned the stale cached module without re-running its top-level code.What changed
apps/server/src/services/plugins/plugin-runtime.ts: resolve the artifact withrequire.resolvebefore requiring it, and evict that key. A missing artifact still reaches the existing catch, which rolls back and keeps the previous instance.prebuilt-server-loading.test.ts: new case with a symlinked data directory. It checks that top-level code re-runs on every reload and that norequire.cacheentry or parentchildrenreference remains under the real cache root. (The existing eviction test builds its prefix from the unresolved temp path, so on macOS it cannot see this.)How you verified
expected 1 to be 3: two reloads reused the stale module) and passes with the fix.vitest run test/services/plugins: 50 files, 529 tests passed.turbo run typecheck --filter=@bb/serverpassed.createPluginService, a generated path plugin of 110 TypeScript modules,--expose-gc, 8 forced GCs per sample,heapUsed + external, Node 22.22.3 on macOS. Default compiled loader, symlinked data directory, plugin edited before each reload:--max-old-space-size=230, 30 reloads end inFATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory.Command, with the harness below saved as
apps/server/bench-4159.mts:node --expose-gc --max-old-space-size=230 --conditions=source --import tsx bench-4159.mts default 30 changedfromapps/server. The harness creates its own symlinked data directory;BENCH_SYMLINK=0uses the real path instead, andsamein place ofchangedreloads without edits.Heap harness
Follow-ups not changed here: source-module eviction under
--preserve-symlinksand the built-in Zod parent URL set also compare unresolved paths.Refs #4159