-
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 1 commit
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,75 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { isASAN } from "harness"; | ||
| import { isASAN, isDebug, isWindows } from "harness"; | ||
|
|
||
| async function spawn() { | ||
| const proc = Bun.spawn(["cat", import.meta.path], { | ||
| stdio: ["ignore", "ignore", "pipe"], | ||
| }); | ||
| await proc.exited; | ||
| } | ||
| // 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 16 KB read buffer (libuv's suggested_size on Windows) | ||
| // after the first byte has been buffered. 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 at least one byte to | ||
| // the piped stream for that allocation to happen. stdout is piped and never | ||
| // consumed; `echo x` / `cmd /c echo x` writes two bytes to it and exits. | ||
| // | ||
| // The previous form spawned `cat` with only stderr piped. `cat` writes nothing | ||
| // to stderr, so since the stack-buffer fast path landed the per-reader buffer | ||
| // was never allocated there and the POSIX release lane was not exercising the | ||
| // retention path at all. Switching to a tiny stdout write restores that, and | ||
| // replacing msys2 `cat` (~9 ms/spawn) with native `cmd` (~2.5 ms/spawn) is what | ||
| // brings the Windows arm64 wall time down. | ||
|
|
||
| const MB = 1024 * 1024; | ||
| const BATCH = 50; | ||
|
|
||
| const cmd = isWindows ? ["cmd", "/c", "echo x"] : ["echo", "x"]; | ||
|
|
||
| async function spawn100() { | ||
| return Promise.all(new Array(100).fill(0).map(v => spawn())); | ||
| 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); | ||
| } | ||
|
|
||
| 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(); | ||
| test("unread 'pipe' stdio does not leak the PipeReader buffer", async () => { | ||
| 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 baseline = process.memoryUsage.rss(); | ||
|
|
||
| const MEASURE_BATCHES = 12; | ||
| for (let i = 0; i < MEASURE_BATCHES; 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 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 MB delta across all platforms when nothing leaks; | ||
| // 5 MB corresponds to ~8.5 KB/spawn of touched pages, tighter than the | ||
| // previous `before * 3` ratio (which allowed ~12 KB/spawn). ASAN quarantine | ||
| // retains freed allocations on the same order as the buffer itself so the | ||
| // delta there is ~10 MB regardless of whether the retention path is broken; | ||
| // keep those lanes as a smoke check with a wider bound (the earlier 6x | ||
| // multiplier encoded the same thing). | ||
|
Check warning on line 72 in test/js/bun/spawn/spawn-noread-leak.test.ts
|
||
| const limitMB = isASAN || isDebug ? 30 : 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.