-
Notifications
You must be signed in to change notification settings - Fork 5k
Throw instead of aborting when stream text() consumers accumulate more than 2^31-1 bytes #37237
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a1fec8e
streams: make text consumers throw instead of aborting past the strin…
robobun 1bb00c3
test: drain and assert child stderr in stream string-limit tests
robobun 7f36aff
Tighten limit-check comments
robobun d121cfe
Shorten limit-check comments to one line
robobun ee7d707
Release the accumulator on the chunk-append error path too
robobun e9cb9b5
Correct the direct-sink estimate comment
robobun 22a934d
Fold the direct-sink estimate comment onto one line
robobun 9e80871
streams: encode text-consumer strings through simdutf instead of Stri…
robobun 46329b0
Fold the header comment onto one line
robobun bdace63
[autofix.ci] apply automated fixes
autofix-ci[bot] 994ffa7
Add a direct-stream test for a buffer grown past the limit after write()
robobun 08917e2
Assert the resize succeeded before the rejection in the resizable-buf…
robobun 0189c88
ci: retrigger
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,155 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
| import { totalmem } from "node:os"; | ||
|
|
||
| // Consuming a stream as text must reject with a catchable error when the accumulated | ||
| // chunks exceed the maximum string length (2^31-1 bytes), instead of aborting the | ||
| // process in WTF::Vector's capacity check. Each child commits ~2.2GB. | ||
| const enoughMemory = totalmem() >= 8 * 1024 * 1024 * 1024; | ||
|
|
||
| // 3 chunks of n bytes sum to 2^31+1: each chunk fits comfortably, the total does not. | ||
| function consumeToText(streamSource: string): string { | ||
| return ` | ||
| const n = 715827883; | ||
| const rs = ${streamSource}; | ||
| try { | ||
| const text = await Bun.readableStreamToText(rs); | ||
| console.log("resolved", text.length); | ||
| } catch (e) { | ||
| console.log("threw", e.name, e.message); | ||
| } | ||
| `; | ||
| } | ||
|
|
||
| async function run(script: string): Promise<{ stdout: string; stderr: string; exitCode: number }> { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode }; | ||
| } | ||
|
|
||
| const threw = { stdout: "threw RangeError Out of memory\n", stderr: "", exitCode: 0 }; | ||
|
|
||
| describe.skipIf(!enoughMemory)("text consumers reject binary chunks summing past 2^31-1", () => { | ||
| test("queue-backed ReadableStream", async () => { | ||
| const result = await run( | ||
| consumeToText(`new ReadableStream({ | ||
| start(c) { | ||
| for (let i = 0; i < 3; i++) c.enqueue(new Uint8Array(n)); | ||
| c.close(); | ||
| }, | ||
| })`), | ||
| ); | ||
| expect(result).toEqual(threw); | ||
| }); | ||
|
|
||
| test("direct ReadableStream", async () => { | ||
| const result = await run( | ||
| consumeToText(`new ReadableStream({ | ||
| type: "direct", | ||
| pull(c) { | ||
| for (let i = 0; i < 3; i++) c.write(new Uint8Array(n)); | ||
| c.end(); | ||
| }, | ||
| })`), | ||
| ); | ||
| expect(result).toEqual(threw); | ||
| }); | ||
|
|
||
| test("Response(stream).text()", async () => { | ||
| const result = await run(` | ||
| const n = 715827883; | ||
| const rs = new ReadableStream({ | ||
| start(c) { | ||
| for (let i = 0; i < 3; i++) c.enqueue(new Uint8Array(n)); | ||
| c.close(); | ||
| }, | ||
| }); | ||
| try { | ||
| const text = await new Response(rs).text(); | ||
| console.log("resolved", text.length); | ||
| } catch (e) { | ||
| console.log("threw", e.name, e.message); | ||
| } | ||
| `); | ||
| expect(result).toEqual(threw); | ||
| }); | ||
|
|
||
| // WTF::String::utf8() aborts once its conversion needs more than INT32_MAX bytes of | ||
| // scratch (2x the length for 8-bit strings), so a mixed stream with a near-limit string | ||
| // chunk crashed in the encode even when the real UTF-8 total fit. The consumers now size | ||
| // and write through simdutf instead. | ||
| test("mixed chunks with a big ASCII string chunk resolve when the UTF-8 total fits", async () => { | ||
| const result = await run(` | ||
| const big = Buffer.alloc(1200000000, "a").toString(); | ||
| const rs = new ReadableStream({ | ||
| start(c) { | ||
| c.enqueue(new Uint8Array([65])); | ||
| c.enqueue(big); | ||
| c.close(); | ||
| }, | ||
| }); | ||
| try { | ||
| const text = await Bun.readableStreamToText(rs); | ||
| console.log("resolved", text.length); | ||
| } catch (e) { | ||
| console.log("threw", e.name, e.message); | ||
| } | ||
| `); | ||
| expect(result).toEqual({ stdout: "resolved 1200000001\n", stderr: "", exitCode: 0 }); | ||
| }, 60_000); | ||
|
|
||
| test("mixed chunks whose UTF-8 expansion passes the limit reject", async () => { | ||
| const result = await run(` | ||
| // 1.2e9 U+00E9 chars: a Latin1 string whose UTF-8 form is 2.4e9 bytes. | ||
| const big = Buffer.alloc(1200000000, 233).toString("latin1"); | ||
| const rs = new ReadableStream({ | ||
| start(c) { | ||
| c.enqueue(new Uint8Array([65])); | ||
| c.enqueue(big); | ||
| c.close(); | ||
| }, | ||
| }); | ||
| try { | ||
| const text = await Bun.readableStreamToText(rs); | ||
| console.log("resolved", text.length); | ||
| } catch (e) { | ||
| console.log("threw", e.name, e.message); | ||
| } | ||
| `); | ||
| expect(result).toEqual(threw); | ||
| }, 60_000); | ||
|
|
||
| // The direct sink records sizes at write() time and reads the spans at end(), so a | ||
| // resizable ArrayBuffer grown in between bypasses the up-front estimate check; the | ||
| // append itself must reject the oversized span. | ||
| test("direct stream with a buffer grown past the limit after write() rejects", async () => { | ||
| const result = await run(` | ||
| const ab = new ArrayBuffer(8, { maxByteLength: 2400000000 }); | ||
| const rs = new ReadableStream({ | ||
| type: "direct", | ||
| pull(c) { | ||
| c.write(new Uint8Array(ab)); | ||
| ab.resize(2400000000); | ||
| console.log("resized", ab.byteLength); | ||
| c.end(); | ||
| }, | ||
| }); | ||
| try { | ||
| const text = await Bun.readableStreamToText(rs); | ||
| console.log("resolved", text.length); | ||
| } catch (e) { | ||
| console.log("threw", e.name, e.message); | ||
| } | ||
| `); | ||
| expect(result).toEqual({ | ||
| stdout: "resized 2400000000\nthrew RangeError Out of memory\n", | ||
| stderr: "", | ||
| exitCode: 0, | ||
| }); | ||
| }); | ||
| }); |
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.