Repository navigation
Body: downgrade the .body getter's Strong<ReadableStream> once the wrapper's traced slot owns it #36624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Body: downgrade the .body getter's Strong<ReadableStream> once the wrapper's traced slot owns it #36624
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a884453
Body: migrate Locked.readable Strong into the wrapper's m_stream slot…
robobun 6d4d714
[autofix.ci] apply automated fixes
autofix-ci[bot] acd8234
trim comments
robobun ec5bd99
switch to downgrade: keep Locked.readable readable after the Strong i…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // https://github.com/oven-sh/bun/issues/29267 | ||
| // | ||
| // Next.js App Router SSR under `bun --bun next start` leaked one | ||
| // Strong<ReadableStream> per request. Next's dedupe-fetch stores the fetch | ||
| // Response promise in a React.cache()-scoped map that lives inside the | ||
| // request's AsyncLocalStorage store, and Next's cloneResponse reads `.body` | ||
| // on a Response whose body is a buffered ArrayBuffer. Reading `.body` on a | ||
| // Blob/InternalBlob/string-backed Response materializes a ReadableStream and | ||
| // cached a Strong handle to it in `Body::PendingValue.readable`; that Strong | ||
| // was only migrated into the wrapper's traced `m_stream` slot from | ||
| // construct/to_js/clone, not from the `.body` getter. The stream's captured | ||
| // async context reached back to the Response, so the Strong formed an | ||
| // uncollectable cycle and every request's ALS store (tens to hundreds of KB) | ||
| // was retained forever. | ||
|
|
||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isASAN, isDebug } from "harness"; | ||
| import path from "node:path"; | ||
|
|
||
| test("Response.body on a buffered body does not root the stream when the async context references the Response", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), path.join(import.meta.dir, "29267-fixture.ts")], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| const jsonLine = stdout.split("\n").find(l => l.startsWith("{")); | ||
| expect(jsonLine).toBeDefined(); | ||
| const result = JSON.parse(jsonLine!); | ||
|
|
||
| // Before the fix `protectedRS` equals N (one Strong<ReadableStream> per | ||
| // iteration); after the fix it is a small constant. N/2 separates the two | ||
| // with wide margin while tolerating a handful of unrelated transients. | ||
| expect(result.protectedRS).toBeLessThan(result.N / 2); | ||
| // The per-iteration objects should be collected, not accumulate. | ||
| expect(result.response).toBeLessThan(result.N / 2); | ||
| expect(result.readableStream).toBeLessThan(result.N / 2); | ||
|
|
||
| // ~60 KB retained per iteration pre-fix (50 KB ALS payload + 10 KB body). | ||
| // ASAN quarantine and debug-heap padding inflate post-GC heapSize, so the | ||
| // ceiling here is well under the unfixed ~30 MB but above sanitizer noise. | ||
| const heapCeilingMB = isASAN || isDebug ? 14 : 7; | ||
| expect(result.heapMB).toBeLessThan(heapCeilingMB); | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
|
|
||
| expect(stderr).toBe(""); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.