Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
77066e2
console.write: handle every failed write when given several arguments
dylan-conway Sep 21, 2026
fbf7c3a
console.write: only a Promise is pending; intrinsics for the array an…
dylan-conway Sep 21, 2026
7072113
console.write: sum the pending Promises without Promise.all
dylan-conway Sep 21, 2026
5c4dae0
console.write: write every argument natively
dylan-conway Sep 21, 2026
7288f3a
console-write test: check the pipe is broken, and cover mixed argumen…
dylan-conway Sep 21, 2026
10773d4
console.write: crediting the counts never replaces a stored error
dylan-conway Sep 21, 2026
33f6390
console.write: a pending operation a later argument settles; flush af…
dylan-conway Sep 21, 2026
28530c9
Sink: the flush result's conversion to JS is not generic
dylan-conway Sep 21, 2026
6afac97
FileSink: a write that flushes earlier chunks reports its own bytes
dylan-conway Sep 21, 2026
63035f1
console.write: a flush that leaves the sink backed up gives the calle…
dylan-conway Sep 21, 2026
e096f7c
FileSink: a write to a sink that failed reports the error it failed with
dylan-conway Sep 21, 2026
9716f7d
FileSink: a sink that was closed stays "done"
dylan-conway Sep 21, 2026
958f02e
FileSink: a write to a finished sink is "done" again
dylan-conway Sep 21, 2026
a2aaf97
filesink test: the un-awaited writes case is POSIX behaviour
dylan-conway Sep 21, 2026
8fcb219
Merge branch 'main' of github.com:oven-sh/bun into claude/filesink-wi…
dylan-conway Sep 21, 2026
9b14b3d
Terminal: the writer no longer reports more than the chunk
dylan-conway Sep 21, 2026
5c98ced
console.write: a Promise that never reaches the caller is not an unha…
dylan-conway Sep 21, 2026
54188c4
console.write: tell the sink's operations apart by their Promise
dylan-conway Sep 21, 2026
80f235d
console.write: carry only this call's bytes; shorter comments
dylan-conway Sep 21, 2026
ec2c027
console-write and filesink tests: the new spawning tests run concurre…
dylan-conway Sep 21, 2026
c45bae8
console.write: back to the loop in JS; mark the Promises it drops as …
dylan-conway Sep 21, 2026
a64b952
console-write test: the later write failing is how it always was
dylan-conway Sep 21, 2026
4e0d147
fs.promises.writeFile test: short and long chunks from an async iterator
dylan-conway Sep 21, 2026
92fb862
console.write: only a Promise that is already rejected is marked handled
dylan-conway Sep 21, 2026
44600fe
console.write: a write that settled before the call was over counts
dylan-conway Sep 22, 2026
ca1353b
console-write test: find the C library the way the harness does
dylan-conway Sep 22, 2026
9975cb5
console.write: what is not a Promise adds up as it did in 1.4.2
dylan-conway Sep 23, 2026
8ab6db2
console.write: no private slot; a failed write's Promise is told apar…
dylan-conway Sep 23, 2026
4133870
Merge branch 'main' of github.com:oven-sh/bun into claude/filesink-wi…
dylan-conway Sep 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/js/builtins/ConsoleObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,32 @@ export function write(this: Console & { $writer: ConsoleWriter | undefined }, in
$putByIdDirectPrivate(this, "writer", writer);
}

// A backed-up writer (FileSink) returns a Promise instead of a count: one Promise, the same for every write made
// while it is backed up, of the bytes those writes added. The caller gets a Promise of the total then, because
// awaiting it waits for the drain and is where a write error (EPIPE from a reader that hung up) arrives.
// The sink's write() returns a count, or a Promise of one in two cases. A backed-up sink returns its one
// outstanding Promise, the same for every write made while it is backed up, of the bytes those writes added. A
// write that fails on the spot (the pipe is already broken) returns a rejected Promise of its own. The caller
// gets a Promise over every distinct one: awaiting it waits for the drain and is where a write error (EPIPE)
// arrives, with none left over for an unhandled rejection. (A finished sink returns `true`: nothing written.)
var wrote = 0;
var pending: Promise<number> | undefined;
var pending: Promise<number>[] | undefined;
const count = $argumentCount();
var i = 0;
do {
const result = writer.write(arguments[i]);
if (typeof result === "number") wrote += result;
else pending = result;
else if ($isPromise<number>(result)) {
if (pending === undefined) pending = [result];
// The shared Promise comes back from consecutive writes, so the last one is the only possible repeat.
else if (pending[pending.length - 1] !== result) $arrayPush(pending, result);
}
} while (++i < count);
Comment thread
dylan-conway marked this conversation as resolved.
Resolved

writer.flush(true);
if (pending === undefined) return wrote;
if (wrote === 0) return pending;
return pending.then(n => wrote + n);
if (pending.length === 1 && wrote === 0) return pending[0];
return Promise.all(pending).$then(counts => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
for (var j = 0; j < counts.length; j++) wrote += counts[j];
return wrote;
});
}

// This is the `console.Console` constructor. It is mostly copied from Node.
Expand Down
42 changes: 42 additions & 0 deletions test/js/bun/console/console-write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ console.error(JSON.stringify({ one, two }));

// The Promise a backed-up console.write() returns is where its write error arrives: a script that
// awaits it can handle a reader that hung up, instead of dying with an unhandled rejection.
//
// Not on Windows: a write to a pipe whose reader is alive is accepted whole there and returns its byte
// count, so whether this script sees a Promise at all depends on whether the reader is already gone.
test.skipIf(isWindows).each([
["one argument", ""],
["several arguments", ', "tail"'],
Expand Down Expand Up @@ -134,3 +137,42 @@ try {
expect(stderr).toBe("caught EPIPE\n");
expect(exitCode).toBe(0);
});

// Every write to a pipe that is already broken fails on the spot, with a rejected Promise of its own.
// console.write() kept only the last one, so a script that awaited it and caught the error still died of the
// unhandled rejection of an earlier argument's write.
//
// The child blocks in a synchronous read of stdin until the parent has closed its end of stdout, and the writes
// are large enough to go to the pipe at once instead of being buffered.
test("an awaited console.write with several arguments handles every failed write", async () => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
process.on("unhandledRejection", e => {
console.error("unhandledRejection " + e?.code);
});
require("node:fs").readSync(0, Buffer.alloc(1));
try {
await console.write(Buffer.alloc(1024 * 1024, "a").toString(), Buffer.alloc(1024 * 1024, "b").toString());
console.error("resolved");
} catch (e) {
console.error("caught " + e.code);
}
`,
],
env: bunEnv,
stdin: "pipe",
stdout: "pipe",
stderr: "pipe",
});

await proc.stdout.cancel();
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
proc.stdin.write("x");
await proc.stdin.end();

const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(stderr).toBe("caught EPIPE\n");
expect(exitCode).toBe(0);
});
3 changes: 3 additions & 0 deletions test/js/bun/util/filesink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,9 @@ it("start() with invalid options throws instead of silently ignoring them", asyn
//
// The child blocks in a synchronous read of stdin between its two writes, so no event-loop turn can tell the sink
// about the hang-up first: the second write() is the one that finds out, with the first still pending.
//
// Not on Windows: a write to a pipe whose reader is alive is accepted whole there and returns its byte count, so
// there is no pending promise for a later write to fail beside.
it.skipIf(isWindows)("a write() that fails while another is pending rejects the pending promise once", async () => {
await using proc = Bun.spawn({
cmd: [
Expand Down
Loading