|
| 1 | +// @vitest-environment node |
| 2 | +/** |
| 3 | + * @see https://github.com/mswjs/msw/issues/2735 |
| 4 | + */ |
| 5 | +import * as fs from 'node:fs' |
| 6 | +import * as os from 'node:os' |
| 7 | +import * as path from 'node:path' |
| 8 | +import { setTimeout as delay } from 'node:timers/promises' |
| 9 | +import { writeHeapSnapshot } from 'node:v8' |
| 10 | +import { http } from 'msw' |
| 11 | +import { setupServer } from 'msw/node' |
| 12 | + |
| 13 | +const TOTAL_REQUESTS = 5_000 |
| 14 | +const CONCURRENCY = 20 |
| 15 | +const URL = 'https://localhost/leak' |
| 16 | + |
| 17 | +async function fireBatch(count: number, concurrency: number): Promise<void> { |
| 18 | + let inflight = 0 |
| 19 | + let started = 0 |
| 20 | + let failed = false |
| 21 | + |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + const next = () => { |
| 24 | + if (failed) { |
| 25 | + return |
| 26 | + } |
| 27 | + if (started >= count && inflight === 0) { |
| 28 | + return resolve() |
| 29 | + } |
| 30 | + |
| 31 | + while (inflight < concurrency && started < count) { |
| 32 | + started++ |
| 33 | + inflight++ |
| 34 | + fetch(URL) |
| 35 | + .then((r) => r.text()) |
| 36 | + .catch((error) => { |
| 37 | + failed = true |
| 38 | + reject(error) |
| 39 | + }) |
| 40 | + .finally(() => { |
| 41 | + inflight-- |
| 42 | + next() |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | + next() |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +async function forceGc(): Promise<void> { |
| 51 | + for (let i = 0; i < 6; i++) { |
| 52 | + global.gc?.() |
| 53 | + await delay(30) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function countConstructors(snapPath: string): Map<string, number> { |
| 58 | + const json = JSON.parse(fs.readFileSync(snapPath, 'utf8')) |
| 59 | + const meta = json.snapshot.meta |
| 60 | + const F = meta.node_fields.length |
| 61 | + const fT = meta.node_fields.indexOf('type') |
| 62 | + const fN = meta.node_fields.indexOf('name') |
| 63 | + const objIdx = meta.node_types[0].indexOf('object') |
| 64 | + const counts = new Map<string, number>() |
| 65 | + |
| 66 | + for (let i = 0; i < json.snapshot.node_count; i++) { |
| 67 | + const b = i * F |
| 68 | + if (json.nodes[b + fT] !== objIdx) { |
| 69 | + continue |
| 70 | + } |
| 71 | + const name = json.strings[json.nodes[b + fN]] |
| 72 | + counts.set(name, (counts.get(name) ?? 0) + 1) |
| 73 | + } |
| 74 | + |
| 75 | + return counts |
| 76 | +} |
| 77 | + |
| 78 | +const server = setupServer() |
| 79 | + |
| 80 | +beforeAll(() => { |
| 81 | + server.listen() |
| 82 | +}) |
| 83 | + |
| 84 | +afterAll(() => { |
| 85 | + server.close() |
| 86 | +}) |
| 87 | + |
| 88 | +it('does not retain a per-request Emitter after the response is delivered', async () => { |
| 89 | + server.use( |
| 90 | + http.get('https://localhost/leak', () => { |
| 91 | + return new Response() |
| 92 | + }), |
| 93 | + ) |
| 94 | + |
| 95 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'msw-emitter-leak-')) |
| 96 | + |
| 97 | + // Warm-up so JIT settles and we don't count startup allocation. |
| 98 | + await fireBatch(200, CONCURRENCY) |
| 99 | + await forceGc() |
| 100 | + |
| 101 | + const baseSnap = path.join(tmp, 'baseline.heapsnapshot') |
| 102 | + writeHeapSnapshot(baseSnap) |
| 103 | + const baseCounts = countConstructors(baseSnap) |
| 104 | + |
| 105 | + // The actual measurement burst. |
| 106 | + await fireBatch(TOTAL_REQUESTS, CONCURRENCY) |
| 107 | + await delay(2000) |
| 108 | + await forceGc() |
| 109 | + await delay(1000) |
| 110 | + await forceGc() |
| 111 | + |
| 112 | + const settledSnap = path.join(tmp, 'settled.heapsnapshot') |
| 113 | + writeHeapSnapshot(settledSnap) |
| 114 | + const settledCounts = countConstructors(settledSnap) |
| 115 | + |
| 116 | + const watch = [ |
| 117 | + 'Emitter', |
| 118 | + 'Listener', |
| 119 | + 'LensList', |
| 120 | + 'WeakMap', |
| 121 | + 'WeakSet', |
| 122 | + ] as const |
| 123 | + console.log(`\nConstructor count after ${TOTAL_REQUESTS} requests:\n`) |
| 124 | + console.log( |
| 125 | + ' ' + |
| 126 | + 'name'.padEnd(12) + |
| 127 | + 'baseline'.padStart(10) + |
| 128 | + 'settled'.padStart(10) + |
| 129 | + 'delta'.padStart(10), |
| 130 | + ) |
| 131 | + console.log(' ' + '-'.repeat(42)) |
| 132 | + for (const name of watch) { |
| 133 | + const b = baseCounts.get(name) ?? 0 |
| 134 | + const s = settledCounts.get(name) ?? 0 |
| 135 | + const d = s - b |
| 136 | + const dStr = (d >= 0 ? '+' : '') + d |
| 137 | + console.log( |
| 138 | + ' ' + |
| 139 | + name.padEnd(12) + |
| 140 | + String(b).padStart(10) + |
| 141 | + String(s).padStart(10) + |
| 142 | + dStr.padStart(10), |
| 143 | + ) |
| 144 | + } |
| 145 | + console.log(`\nHeap snapshots written to: ${tmp}\n`) |
| 146 | + |
| 147 | + const emitterDelta = |
| 148 | + (settledCounts.get('Emitter') ?? 0) - (baseCounts.get('Emitter') ?? 0) |
| 149 | + |
| 150 | + // After the fix, this delta is essentially 0. Today, it's ~= TOTAL. |
| 151 | + // Threshold: 5% of request count to allow for noise. |
| 152 | + expect(emitterDelta).toBeLessThan(TOTAL_REQUESTS * 0.05) |
| 153 | +}) |
0 commit comments