fix(request): release cloned request bodies via buffer-once replay - #397
Open
SynthLuvr wants to merge 1 commit into
Open
fix(request): release cloned request bodies via buffer-once replay#397SynthLuvr wants to merge 1 commit into
SynthLuvr wants to merge 1 commit into
Conversation
…onojs#347) clone() no longer tee()s the socket-backed body stream, whose unread branch queues the entire body until it is read or GC'd - pinned for as long as the request graph is reachable (logger/tracing contexts, error objects, profilers). Instead the body is read once into a shared Buffer (via the existing readBodyDirect fast path) and the original request + clones replay from it. When the response completes, the listener releases the shared buffer and cancels replay streams that were never read. - Hook the release of the synchronous cacheable fast path to 'close' rather than 'finish' so the buffer is also released when the client disconnects early, and only attach the listener when a shared body exists - the path stays free of close listeners otherwise - Export bodySharedBufferKey so tests can watch the shared buffer with a FinalizationRegistry, next to the existing memory leak tests
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.
Fixes #347
Problem
clone()tee()s the socket-backed body stream. The unread branch of a tee queues every chunk until it is read, so when middleware doesc.req.raw.clone().json()and the original body is never read, the entire raw body stays resident for as long as the request graph is reachable — logger/tracing contexts, error objects, and profilers routinely retain the request past the response, which shows up as the linear memory growth reported in the issue.Approach
clone()now reads the body exactly once into a Buffer (via the existingreadBodyDirectfast path) and hands the original request and every clone a lightweight replay stream backed by that shared buffer. No tee is created, so nothing queues an unread copy.'close'rather than'finish'so the buffer is also released when the client disconnects before the response is flushed — and it is only attached when a shared body exists, so responses without cloned bodies keep the path free of close listeners.Reading an unread clone after the response has completed rejects with
TypeError: Body is unusable, matching what a nativeRequestdoes when its body stream has been canceled.Tests
test/request.test.ts: clones replay the body while leaving the original readable; unread clones are canceled on release; clones being read keep deliveringtest/listener.test.ts: the original body stays readable after a clone has consumed it (integration); the synchronous cacheable fast path releases the shared body on closetest/server.test.ts: GC-based memory-leak regression next to the existing ones — the shared buffer is collected after the response completes even while the request is still retainedVerified locally:
format,lint,typecheck,build+ publint, and the full vitest suite.