Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
25 changes: 25 additions & 0 deletions src/runtime/webcore/FileSink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,31 @@
}
WriteResult::Err(err) => {
self.done.set(true);
if self.pending.get().state == streams::PendingState::Pending {
// A backpressured write() left its promise outstanding.
// Throwing here would report the failure to the caller and
// then let the auto-flush/error path reject that promise a
// second time — with nobody holding it when the caller
// discarded write()'s return value, that second delivery
// surfaces as an unhandledRejection. Deliver the error to
// the pending promise instead and hand the caller the same
// promise (exactly like the Pending arm), so the failure is
// reported once, to whichever await is watching. The latch
// and promise grab happen before `writer.end()`: its
// teardown can re-enter `on_error`/`run_pending`
// synchronously, and the slot must already hold the error
// and this caller's promise when that runs.
self.pending
.with_mut(|p| p.result = streams::Writable::Err(err));
// SAFETY: JsCell — `WritablePending::promise` allocates a
// JSPromise (may GC) but does not invoke any FileSink
// host-fn synchronously.
let promise_result = unsafe { self.pending.get_mut() }.promise(global_this);
self.writer.with_mut(|w| w.end());
self.run_pending_later();
// SAFETY: `WritablePending::promise()` never returns null.
return sys::Result::Ok(unsafe { (*promise_result).to_js() });
}

Check failure on line 1161 in src/runtime/webcore/FileSink.rs

View check run for this annotation

Claude / Claude Code Review

Sibling FileSink::end() Err arm still orphans the pending write() promise

The sibling `FileSink::end()` (reached via `sink.close()` → `${name}__doClose` → `js_close`, and via `${controller}__close` on the piped-stream path) has the identical `WriteResult::Err` arm and is left unfixed: it sets `done=true`, calls `writer.end()`, and returns — never latching `pending.result` or scheduling `run_pending_later()`. A backpressured `write()`'s promise is therefore still orphaned forever when `close()` (rather than `end()`) hits synchronous EPIPE. REVIEW.md requires covering t
self.writer.with_mut(|w| w.end());
sys::Result::Err(err)
}
Expand Down
47 changes: 47 additions & 0 deletions test/js/bun/util/filesink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,53 @@
expect(received).toBe(size);
});

// end() called after a backpressured write() whose promise the caller
// discarded, with the reader already gone: end_from_js's flush() sees EPIPE
// synchronously. Throwing it would report the failure to end()'s caller and
// then let the auto-flush/error path reject the orphaned write() promise as an
// unhandledRejection; instead the error is delivered to that pending promise
// and end() returns the same promise, so the failure is reported exactly once.
it.skipIf(!isPosix)(
"end() after a discarded backpressured write() delivers EPIPE once, with no unhandled rejection",
async () => {
const [readFd, writeFd] = createSocketPair();
let readFdOpen = true;
const sink = Bun.file(writeFd).writer();
let unhandled: any = null;
function onUnhandled(e: unknown) {
unhandled = e;
}
process.on("unhandledRejection", onUnhandled);
try {
// Discarded on purpose: the write's promise must not surface on its own.
sink.write(Buffer.alloc(4 * 1024 * 1024, 0x61));
fs.closeSync(readFd);
readFdOpen = false;

let caught: any;
try {
await sink.end();
} catch (e) {
caught = e;
}
expect(caught?.code).toBe("EPIPE");

// Bounded window for a stray second rejection to surface.
for (let i = 0; i < 10; i++) await Bun.sleep(1);
expect(unhandled).toBeNull();

Check warning on line 309 in test/js/bun/util/filesink.test.ts

View check run for this annotation

Claude / Claude Code Review

New test's assertions pass on pre-fix code — only fails via incidental cleanup hang

Both assertions in this test — `expect(caught?.code).toBe("EPIPE")` and `expect(unhandled).toBeNull()` — already pass on pre-fix code (773be9df); the test only fails there because the finally block's defensive `await sink.end()` returns the never-settled write() promise and times out. To make the assertions load-bearing on the actual fix, hold the write() promise and assert it settles rejected with EPIPE (e.g. `const wp = sink.write(...); …; await expect(wp).rejects.toMatchObject({code:'EPIPE'})
} finally {
process.off("unhandledRejection", onUnhandled);
try {
await sink.end();
} catch {}
Comment thread
robobun marked this conversation as resolved.
Resolved
try {
fs.closeSync(writeFd);
} catch {}
if (readFdOpen) fs.closeSync(readFd);
}
},
);

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// The deferred auto-flush microtask runs at the first microtask checkpoint
// after write() backpressures. If its flush() hit EPIPE, it discarded the
// error and then let `run_pending_later()` resolve the pending write() promise
Expand Down
Loading