Node: failed passthrough fetch requests retain InterceptorHttpNetworkFrame until server.close() (2.15.0)
Environment
- MSW 2.15.0 (current npm latest when tested), @mswjs/interceptors 0.41.9; fresh public npm installation, no patches or overrides.
- Node.js v22.18.0, macOS arm64.
- No framework, application code, custom handlers, or request/response storage.
Reproduction repository
https://github.com/r34son/msw-node-memory-repro
Reproduction steps
git clone https://github.com/r34son/msw-node-memory-repro.git
cd msw-node-memory-repro
npm ci
npm test # fails: retained heap grows >8 MiB
npm run control # passes: same requests without MSW
node --expose-gc repro.mjs interceptors fetch-reset # also passesThe entire MSW setup is:
const interception = setupServer();
interception.listen({ onUnhandledRequest: 'bypass' });A local HTTP server reads the request and destroys its socket without returning a response. The script awaits each fetch rejection, warms up with 500 requests, then performs 1500 further requests. No errors/requests/responses are stored. GC is forced between batches, and the final snapshot is taken after an additional 2-second idle period and GC.
Current behavior
Retained heap growth after 1500 measured requests, two independent runs:
| Mode | Run 1 | Run 2 |
|---|---|---|
| No interception | +0.43 MB | +0.43 MB |
| BatchInterceptor only | +0.55 MB | +0.55 MB |
| setupServer() | +11.25 MB | +11.25 MB |
A follow-up heap snapshot run counts the actual constructor InterceptorHttpNetworkFrame:
| Object | After 500 warmup requests | After 1500 more | After server.close() |
|---|---|---|---|
| InterceptorHttpNetworkFrame | 500 | 2000 | 0 |
| HTTPParser | 2 | 2 | 2 |
| MockHttpSocket | 0 | 0 | 0 |
This appears separate from the HTTPParser retention in mswjs/interceptors#779: parser and socket counts remain flat here. External memory and ArrayBuffers are also stable.
Suspected lifecycle gap
In the shipped src/core/experimental/sources/interceptor-source.ts, #handleRequest adds a frame to #frames, while #handleResponse deletes it and removes its listeners. A failed real fetch has no response event. disable() clears the map, consistent with the frames disappearing after server.close().
Issue #2735 mentioned this response-less path in its lifecycle notes, but that issue is closed and the leak still reproduces on 2.15.0. This report isolates the failed-request case without the successful-response emitter leak or an application-level body map. Please redirect/merge if this should instead be tracked there.
Expected behavior
Once the caller has observed a terminal network error and the request is no longer in flight, its frame and listeners should become eligible for collection without closing the entire interception server. A long-lived setupServer should not retain one frame per failed request.
Source: mswjs/msw