Repository navigation
webcore: store ReadableStreamSource onClose in WriteBarrier slot instead of Strong #32582
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
Jarred-Sumner
merged 8 commits into
main
from
farm/4514e660/readablestream-source-onclose-strong-cycle
Jun 23, 2026
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
df13e6c
webcore: store ReadableStreamSource onClose in WriteBarrier slot inst…
robobun c3bf12e
FileReader: add !started guard to Windows on_start ref, matching POSIX
robobun 831c135
[autofix.ci] apply automated fixes
autofix-ci[bot] 4f754d7
doc: drop stale accessor count from source_context_codegen comment
robobun cead354
NewSource: add hasPendingActivity so the JS wrapper is rooted while I…
robobun e1fbb50
NewSource: use SeqCst for ref_count atomics per repo convention
robobun 2b39b48
ci: retrigger
robobun b894a15
NewSource: replace hasPendingActivity with JsRef upgrade/downgrade on…
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { heapStats } from "bun:jsc"; | ||
| import { expect, test } from "bun:test"; | ||
|
|
||
| // The JS onClose callback is installed on the native source wrapper when the | ||
| // lazy stream is first pulled. Storing it in a JSC::Strong rooted a cycle | ||
| // (source wrapper -> m_ctx NewSource -> Strong(onClose) -> bound fn -> | ||
| // NativeReadableStreamSource -> $stream -> source wrapper) that only broke | ||
| // when EOF ran the JS-side callClose or the cancel algorithm ran #cancel. A | ||
| // stream that is read partially and then dropped (releaseLock without cancel) | ||
| // never hits either path, so the source wrapper leaked forever. Storing | ||
| // onClose in the GC-traced onCloseCallback WriteBarrier slot (as onDrain | ||
| // already did) turns the cycle into an ordinary intra-heap cycle that | ||
| // mark-sweep collects. | ||
|
|
||
| test("native ReadableStream source is collectable after partial read + releaseLock", async () => { | ||
| // Payload must exceed the native pull buffer (ByteBlobLoader caps | ||
| // chunk_size at 2MB) so the first pull does not return *AndDone and | ||
| // callClose is never queued. | ||
| const payload = Buffer.alloc(8 * 1024 * 1024, "x"); | ||
| async function once() { | ||
| const stream = new Blob([payload]).stream(); | ||
| const reader = stream.getReader(); | ||
| await reader.read(); | ||
| reader.releaseLock(); | ||
| } | ||
|
|
||
| for (let i = 0; i < 5; i++) await once(); | ||
| Bun.gc(true); | ||
| const before = heapStats().objectTypeCounts.BlobInternalReadableStreamSource ?? 0; | ||
|
|
||
| for (let i = 0; i < 30; i++) await once(); | ||
| Bun.gc(true); | ||
| await 1; | ||
| Bun.gc(true); | ||
| const after = heapStats().objectTypeCounts.BlobInternalReadableStreamSource ?? 0; | ||
|
|
||
| // Pre-fix this grew by exactly 30 (one pinned wrapper per iteration). | ||
| expect(after - before).toBeLessThan(8); | ||
| }); | ||
|
|
||
| test("fetch body native source is collectable after partial read + releaseLock", async () => { | ||
| const payload = Buffer.alloc(4 * 1024 * 1024, "x"); | ||
| using server = Bun.serve({ | ||
| port: 0, | ||
| fetch() { | ||
| return new Response(payload); | ||
| }, | ||
| }); | ||
| const url = `http://127.0.0.1:${server.port}/`; | ||
|
|
||
| async function once() { | ||
| const res = await fetch(url); | ||
| const reader = res.body!.getReader(); | ||
| await reader.read(); | ||
| reader.releaseLock(); | ||
| } | ||
|
|
||
| for (let i = 0; i < 5; i++) await once(); | ||
| Bun.gc(true); | ||
| const countSources = () => { | ||
| const c = heapStats().objectTypeCounts; | ||
| return (c.BlobInternalReadableStreamSource ?? 0) + (c.BytesInternalReadableStreamSource ?? 0); | ||
| }; | ||
| const before = countSources(); | ||
|
|
||
| for (let i = 0; i < 30; i++) await once(); | ||
| Bun.gc(true); | ||
| await 1; | ||
| Bun.gc(true); | ||
| const after = countSources(); | ||
|
|
||
| // Pre-fix this grew by ~30. | ||
| expect(after - before).toBeLessThan(8); | ||
| }); | ||
|
|
||
| // Regression guard: the cycle-breaking paths that already worked must keep | ||
| // working after switching storage. | ||
| test("native ReadableStream source is collectable after full consumption", async () => { | ||
| const payload = Buffer.alloc(8 * 1024 * 1024, "x"); | ||
| async function once() { | ||
| const stream = new Blob([payload]).stream(); | ||
| for await (const _ of stream) { | ||
| } | ||
| } | ||
|
|
||
| for (let i = 0; i < 5; i++) await once(); | ||
| Bun.gc(true); | ||
| const before = heapStats().objectTypeCounts.BlobInternalReadableStreamSource ?? 0; | ||
|
|
||
| for (let i = 0; i < 30; i++) await once(); | ||
| Bun.gc(true); | ||
| await 1; | ||
| Bun.gc(true); | ||
| const after = heapStats().objectTypeCounts.BlobInternalReadableStreamSource ?? 0; | ||
|
|
||
| expect(after - before).toBeLessThan(8); | ||
| }); | ||
|
|
||
| test("native source onClose callback still fires after switching to cached slot", async () => { | ||
| // proc.stdout is a FileInternalReadableStreamSource; closing stdin makes | ||
| // cat exit, which EOFs stdout. FileReader.on_reader_done -> parent.on_close | ||
| // -> on_js_close must still find and invoke the JS onClose callback now | ||
| // that it lives in a WriteBarrier slot rather than a Strong. | ||
| await using proc = Bun.spawn({ | ||
| cmd: ["cat"], | ||
| stdin: "pipe", | ||
| stdout: "pipe", | ||
| env: { PATH: process.env.PATH }, | ||
| }); | ||
| const reader = proc.stdout.getReader(); | ||
| const pending = reader.read(); | ||
| proc.stdin.end(); | ||
| const { done } = await pending; | ||
| // If on_js_close did not fire, the pending pull promise would never resolve | ||
| // with done:true and this test would hang. | ||
| expect(done).toBe(true); | ||
| reader.releaseLock(); | ||
| await proc.exited; | ||
| }); |
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.