-
Notifications
You must be signed in to change notification settings - Fork 5k
test(spawn): speed up spawn-noread-leak.test.ts (27s -> 2s on Windows arm64) #35427
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
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
farm/9e61383e/speedup-spawn-noread-leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−22
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,78 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { isASAN } from "harness"; | ||
| import { isASAN, isWindows, tempDir } from "harness"; | ||
| import { join } from "node:path"; | ||
|
|
||
| async function spawn() { | ||
| const proc = Bun.spawn(["cat", import.meta.path], { | ||
| stdio: ["ignore", "ignore", "pipe"], | ||
| // Regression coverage for issue #18265 / PR #20102: a `"pipe"` stdio stream | ||
| // that JS never reads must not retain the PipeReader's read buffer past the | ||
| // child's exit. | ||
| // | ||
| // PipeReader reserves a per-reader read buffer once data arrives (~16 KB on | ||
| // POSIX via the hardcoded `reserve(16 * 1024)` in PosixBufferedReader, ~64 KB | ||
| // on Windows via libuv's pipe alloc_cb suggested_size). On POSIX the first | ||
| // read uses a shared stack buffer and only falls through to the per-reader | ||
| // `reserve()` loop once it has produced data, so the child must write to the | ||
| // piped stream for that allocation to happen. The child writes a full 16 KB so | ||
| // the retained buffer's pages are actually faulted in; with a tiny write only | ||
| // one page is touched and the regressed delta sits under any useful RSS bound. | ||
| // | ||
| // The previous form spawned msys2 `cat` (~9 ms/spawn on Windows arm64) with an | ||
| // empty stderr pipe; native `cmd /c type` / POSIX `cat` keep the spawn cost at | ||
| // ~2.5 ms / ~0.2 ms respectively. | ||
|
|
||
| const MB = 1024 * 1024; | ||
| const BATCH = 50; | ||
|
|
||
| test("unread 'pipe' stdio does not leak the PipeReader buffer", async () => { | ||
| using dir = tempDir("spawn-noread-leak", { | ||
| "payload.bin": Buffer.alloc(16 * 1024, "x").toString(), | ||
| }); | ||
| await proc.exited; | ||
| } | ||
|
|
||
| async function spawn100() { | ||
| return Promise.all(new Array(100).fill(0).map(v => spawn())); | ||
| } | ||
|
|
||
| test("does not leak", async () => { | ||
| const before = process.memoryUsage().rss; | ||
| console.log("before", (before / 1024 / 1024).toFixed(3), "MB"); | ||
| for (let index = 0; index < 30; index++) { | ||
| await spawn100(); | ||
| const payload = join(String(dir), "payload.bin"); | ||
| const cmd = isWindows ? ["cmd", "/c", "type", payload] : ["cat", payload]; | ||
|
|
||
| async function spawnBatch(): Promise<number> { | ||
| const codes = await Promise.all( | ||
| Array.from({ length: BATCH }, async () => { | ||
| const proc = Bun.spawn(cmd, { stdio: ["ignore", "pipe", "ignore"] }); | ||
| return proc.exited; | ||
| }), | ||
| ); | ||
| // Fold all exit codes so a child that failed to launch surfaces as a test | ||
| // failure instead of a silently-different workload. | ||
| return codes.reduce((a, b) => a | b, 0); | ||
| } | ||
|
|
||
| let badExit = 0; | ||
|
|
||
| // Warm up so lazily-created runtime state (thread pools, signal fds, JSC | ||
| // heap growth) is already in the baseline and only per-spawn retention shows | ||
| // up in the delta. | ||
| for (let i = 0; i < 3; i++) { | ||
| badExit |= await spawnBatch(); | ||
| Bun.gc(true); | ||
| } | ||
| const after = process.memoryUsage().rss; | ||
| console.log("after", (after / 1024 / 1024).toFixed(3), "MB"); | ||
| // ASAN's quarantine retains freed allocations so RSS grows much more under | ||
| // bun-asan; widen the multiplier there. | ||
| expect(before + after).toBeLessThan(before * (isASAN ? 6 : 3)); | ||
| }, 0); | ||
| const baseline = process.memoryUsage.rss(); | ||
|
|
||
| const MEASURE_BATCHES = 12; | ||
| for (let i = 0; i < MEASURE_BATCHES; i++) { | ||
| badExit |= await spawnBatch(); | ||
| Bun.gc(true); | ||
| } | ||
| const final = process.memoryUsage.rss(); | ||
| const deltaMB = (final - baseline) / MB; | ||
|
|
||
| console.log( | ||
| `RSS: ${(baseline / MB).toFixed(1)} MB -> ${(final / MB).toFixed(1)} MB ` + | ||
| `(+${deltaMB.toFixed(1)} MB over ${MEASURE_BATCHES * BATCH} spawns)`, | ||
| ); | ||
|
|
||
| expect(badExit).toBe(0); | ||
|
|
||
| // Release builds sit at ~0-1 MB delta across all platforms when nothing | ||
| // leaks; a retained 16 KB buffer over 600 spawns shows as ~10 MB. ASAN | ||
| // quarantine holds the freed buffers so the delta there (~50 MB) is | ||
| // dominated by quarantine growth regardless of whether the retention path is | ||
| // broken; keep that lane as a smoke check with a wider bound (the earlier 6x | ||
| // multiplier on the ratio assertion encoded the same thing). | ||
| const limitMB = isASAN ? 100 : 5; | ||
| expect(deltaMB).toBeLessThan(limitMB); | ||
| }); | ||
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.