diff --git a/test/js/bun/spawn/spawn-streaming-stdin.test.ts b/test/js/bun/spawn/spawn-streaming-stdin.test.ts index 329561219289..fa978bb7f5a4 100644 --- a/test/js/bun/spawn/spawn-streaming-stdin.test.ts +++ b/test/js/bun/spawn/spawn-streaming-stdin.test.ts @@ -63,7 +63,13 @@ test("spawn can write to stdin multiple chunks", async () => { remaining -= concurrency; } - const newMaxFD = getMaxFD(); + // Pipe fds are closed on a background thread after their streams finish, so + // the last batch's closes may still be in flight here; wait for them to land. + let newMaxFD = getMaxFD(); + for (let i = 0; i < 100 && newMaxFD !== maxFD; i++) { + await Bun.sleep(10); + newMaxFD = getMaxFD(); + } // assert we didn't leak any file descriptors expect(newMaxFD).toBe(maxFD); diff --git a/test/js/bun/spawn/spawn-streaming-stdout.test.ts b/test/js/bun/spawn/spawn-streaming-stdout.test.ts index 5ae7b09bec85..4a518fb92bbf 100644 --- a/test/js/bun/spawn/spawn-streaming-stdout.test.ts +++ b/test/js/bun/spawn/spawn-streaming-stdout.test.ts @@ -40,11 +40,28 @@ test("spawn can read from stdout multiple chunks", async () => { await Promise.all(promises); i += concurrency; if (maxFD === -1) { + // Pipe fds are closed on a background thread after their streams finish, + // so the first batch's closes may still be in flight here; poll until the + // count settles so the baseline isn't inflated. maxFD = getMaxFD(); + for (let j = 0; j < 100; j++) { + await Bun.sleep(10); + const settled = getMaxFD(); + if (settled === maxFD) break; + maxFD = settled; + } } } - const newMaxFD = getMaxFD(); - expect(newMaxFD).toBe(maxFD); + // Same race for the last batch's closes; wait for the fd count to drop back + // to the baseline before asserting. The settled baseline can still come out + // slightly high (it has no pre-spawn ground truth; #6724 moved it after the + // first batch on purpose), so ending below it is fine; only above is a leak. + let newMaxFD = getMaxFD(); + for (let i = 0; i < 100 && newMaxFD > maxFD; i++) { + await Bun.sleep(10); + newMaxFD = getMaxFD(); + } + expect(newMaxFD).toBeLessThanOrEqual(maxFD); clearInterval(interval); await expectMaxObjectTypeCount(expect, "ReadableStream", 10); await expectMaxObjectTypeCount(expect, "ReadableStreamDefaultReader", 10);