|
| 1 | +// Flags: --expose-internals |
| 2 | +// Evaluates the hash_to_module_map memory behaviour across clearCache cycles. |
| 3 | +// |
| 4 | +// hash_to_module_map is a C++ unordered_multimap<int, ModuleWrap*> on the |
| 5 | +// Environment. Every new ModuleWrap adds an entry; the destructor removes it. |
| 6 | +// Clearing the Node-side loadCache does not directly touch hash_to_module_map β |
| 7 | +// entries are removed only when ModuleWrap objects are garbage-collected. |
| 8 | +// |
| 9 | +// We verify two invariants: |
| 10 | +// |
| 11 | +// 1. DYNAMIC imports: after clearCache + GC, the old ModuleWrap is collected |
| 12 | +// and therefore its hash_to_module_map entry is removed. The map does NOT |
| 13 | +// grow without bound for purely-dynamic import/clear cycles. |
| 14 | +// (Verified via checkIfCollectableByCounting.) |
| 15 | +// |
| 16 | +// 2. STATIC imports: when a parent P statically imports M, clearing M from |
| 17 | +// the load cache does not free M's ModuleWrap (the static link keeps it). |
| 18 | +// Each re-import adds one new entry while the old entry stays for the |
| 19 | +// lifetime of P. This is a bounded, expected retention (not an unbounded |
| 20 | +// leak): it is capped at one stale entry per module per live static parent. |
| 21 | + |
| 22 | +import '../common/index.mjs'; |
| 23 | + |
| 24 | +import assert from 'node:assert'; |
| 25 | +import { clearCache, createRequire } from 'node:module'; |
| 26 | +import { queryObjects } from 'v8'; |
| 27 | + |
| 28 | +const require = createRequire(import.meta.url); |
| 29 | +const { checkIfCollectableByCounting } = require('../common/gc'); |
| 30 | +const { internalBinding } = require('internal/test/binding'); |
| 31 | +const { ModuleWrap } = internalBinding('module_wrap'); |
| 32 | + |
| 33 | +const counterBase = new URL( |
| 34 | + '../fixtures/module-cache/esm-counter.mjs', |
| 35 | + import.meta.url, |
| 36 | +).href; |
| 37 | + |
| 38 | +const parentURL = new URL( |
| 39 | + '../fixtures/module-cache/esm-static-parent.mjs', |
| 40 | + import.meta.url, |
| 41 | +).href; |
| 42 | + |
| 43 | +// ββ Invariant 1: dynamic-only cycles do NOT leak ModuleWraps ββββββββββββββββ |
| 44 | +// Use cache-busting query params so each import gets a distinct URL. |
| 45 | + |
| 46 | +const outer = 8; |
| 47 | +const inner = 4; |
| 48 | + |
| 49 | +await checkIfCollectableByCounting(async (i) => { |
| 50 | + for (let j = 0; j < inner; j++) { |
| 51 | + const url = `${counterBase}?hm=${i}-${j}`; |
| 52 | + await import(url); |
| 53 | + clearCache(url, { |
| 54 | + parentURL: import.meta.url, |
| 55 | + resolver: 'import', |
| 56 | + caches: 'all', |
| 57 | + }); |
| 58 | + } |
| 59 | + return inner; |
| 60 | +}, ModuleWrap, outer, 50); |
| 61 | + |
| 62 | +// ββ Invariant 2: static-parent cycles cause bounded retention βββββββββββββββ |
| 63 | +// After loading the static parent (which pins one counter instance), each |
| 64 | +// clear+re-import of the base counter URL creates exactly one new ModuleWrap |
| 65 | +// while the old one stays alive (pinned by the parent). |
| 66 | +// The net growth per cycle is +1. After N cycles the live count is |
| 67 | +// baseline + 1(parent) + 1(pinned original counter) + 1(current counter) |
| 68 | +// β a constant overhead, not growing with N. |
| 69 | + |
| 70 | +// Load the static parent; this also loads the counter (count starts at 1 for |
| 71 | +// the global, but we seed it fresh by clearing any earlier runs' state). |
| 72 | +delete globalThis.__module_cache_esm_counter; |
| 73 | + |
| 74 | +const parent = await import(parentURL); |
| 75 | +assert.strictEqual(parent.count, 1); |
| 76 | + |
| 77 | +const wrapCount0 = queryObjects(ModuleWrap, { format: 'count' }); |
| 78 | + |
| 79 | +// Cycle 1: clear counter + re-import β new instance created, old pinned. |
| 80 | +clearCache(counterBase, { |
| 81 | + parentURL: import.meta.url, |
| 82 | + resolver: 'import', |
| 83 | + caches: 'all', |
| 84 | +}); |
| 85 | +const v2 = await import(counterBase); |
| 86 | +assert.strictEqual(v2.count, 2); |
| 87 | + |
| 88 | +const wrapCount1 = queryObjects(ModuleWrap, { format: 'count' }); |
| 89 | +// +1 new ModuleWrap (v2); old one kept alive by parent's static link. |
| 90 | +assert.strictEqual(wrapCount1, wrapCount0 + 1, |
| 91 | + 'Each clear+reimport cycle adds exactly one new ModuleWrap ' + |
| 92 | + 'when a static parent holds the old instance'); |
| 93 | + |
| 94 | +// Cycle 2: clear counter again + re-import. |
| 95 | +clearCache(counterBase, { |
| 96 | + parentURL: import.meta.url, |
| 97 | + resolver: 'import', |
| 98 | + caches: 'all', |
| 99 | +}); |
| 100 | +const v3 = await import(counterBase); |
| 101 | +assert.strictEqual(v3.count, 3); |
| 102 | + |
| 103 | +const wrapCount2 = queryObjects(ModuleWrap, { format: 'count' }); |
| 104 | +// Another +1 (v3); v2 is no longer in loadCache and has no other strong |
| 105 | +// holder, so it MAY have been collected already. v1 (pinned by parent) is |
| 106 | +// still alive. Net growth is bounded by the number of active versions in |
| 107 | +// any live strong reference β typically just the current one plus the |
| 108 | +// parent-pinned original. |
| 109 | +assert.ok( |
| 110 | + wrapCount2 <= wrapCount1 + 1, |
| 111 | + `After a second cycle, live ModuleWrap count should grow by at most 1 ` + |
| 112 | + `(got ${wrapCount2}, was ${wrapCount1})`, |
| 113 | +); |
| 114 | + |
| 115 | +delete globalThis.__module_cache_esm_counter; |
0 commit comments