From 8994f8fcd236a08d28818e18be97eb3855af3f83 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 18 May 2026 00:16:00 +0000 Subject: [PATCH 1/2] Fix FileSink.start() crash when called without path/fd on an open writer When calling .start() on a FileSink created by Bun.file(path).writer() with an options object that contains neither a 'path' nor an 'fd' property (e.g. .start({}) or .start({ highWaterMark: N })), the options were converted to Start::FileSink with input_path = Fd::INVALID, which was then passed to setup() -> open_for_writing -> dup(Fd::INVALID), tripping a debug assertion in sys::Error::with_fd. Skip the setup() call when the resulting input_path is the invalid-fd placeholder so the existing file setup is preserved. --- src/runtime/webcore/FileSink.rs | 7 ++++++- test/js/bun/util/filesink.test.ts | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/runtime/webcore/FileSink.rs b/src/runtime/webcore/FileSink.rs index 2fb3729b1b1c..aa7cf838c550 100644 --- a/src/runtime/webcore/FileSink.rs +++ b/src/runtime/webcore/FileSink.rs @@ -849,7 +849,12 @@ impl FileSink { pub fn start(&self, stream_start: streams::Start) -> sys::Result<()> { match stream_start { - streams::Start::FileSink(ref file) => { + streams::Start::FileSink(ref file) + if !matches!( + file.input_path, + crate::webcore::PathOrFileDescriptor::Fd(Fd::INVALID) + ) => + { // PORT NOTE: `streams::FileSinkOptions` mirrors `file_sink::Options` // but is a distinct draft type; bridge by-field until streams.rs // aliases to this module's `Options`. diff --git a/test/js/bun/util/filesink.test.ts b/test/js/bun/util/filesink.test.ts index 736e5b085731..3b3f66c9dca4 100644 --- a/test/js/bun/util/filesink.test.ts +++ b/test/js/bun/util/filesink.test.ts @@ -268,3 +268,13 @@ it.skipIf(!isPosix)("does not leak native FileSink when a pending write fails (E // more than that indicates a native leak. expect(fileSinkInternals.liveCount()).toBeLessThanOrEqual(baseline + 1); }); + +it("start() without path/fd on an already-open writer does not crash", async () => { + const path = join(tmpdirSync(), "filesink-restart.txt"); + const writer = Bun.file(path).writer(); + expect(() => writer.start({})).not.toThrow(); + expect(() => writer.start({ highWaterMark: 1024 })).not.toThrow(); + writer.write("hello"); + await writer.end(); + expect(await Bun.file(path).text()).toBe("hello"); +}); From 42d8725d6b414f4674f46f45c3089061a2403a23 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 18 May 2026 00:32:39 +0000 Subject: [PATCH 2/2] Unify streams::FileSinkOptions with file_sink::Options Replace the duplicate FileSinkOptions struct in streams.rs with a type alias to file_sink::Options so FileSink::start() can pass the options straight through to setup() without field-by-field bridging. --- src/runtime/webcore/Blob.rs | 3 +++ src/runtime/webcore/FileSink.rs | 28 ++-------------------------- src/runtime/webcore/streams.rs | 21 +++++---------------- 3 files changed, 10 insertions(+), 42 deletions(-) diff --git a/src/runtime/webcore/Blob.rs b/src/runtime/webcore/Blob.rs index 713eb63edef5..d0500461f2a3 100644 --- a/src/runtime/webcore/Blob.rs +++ b/src/runtime/webcore/Blob.rs @@ -1565,6 +1565,7 @@ impl BlobExt for Blob { let stream_start = streams::Start::FileSink(streams::FileSinkOptions { input_path, chunk_size: 0, + ..Default::default() }); // SAFETY: `init` returns a freshly-allocated +1 *mut FileSink. @@ -1928,6 +1929,7 @@ impl BlobExt for Blob { streams::Start::FileSink(streams::FileSinkOptions { chunk_size: 0, input_path: webcore::PathOrFileDescriptor::Fd(Fd::INVALID), + ..Default::default() }) }; if let streams::Start::FileSink(ref mut opts) = stream_start { @@ -1936,6 +1938,7 @@ impl BlobExt for Blob { stream_start = streams::Start::FileSink(streams::FileSinkOptions { chunk_size: 0, input_path, + ..Default::default() }); } diff --git a/src/runtime/webcore/FileSink.rs b/src/runtime/webcore/FileSink.rs index aa7cf838c550..2c92dca2c921 100644 --- a/src/runtime/webcore/FileSink.rs +++ b/src/runtime/webcore/FileSink.rs @@ -850,33 +850,9 @@ impl FileSink { pub fn start(&self, stream_start: streams::Start) -> sys::Result<()> { match stream_start { streams::Start::FileSink(ref file) - if !matches!( - file.input_path, - crate::webcore::PathOrFileDescriptor::Fd(Fd::INVALID) - ) => + if !matches!(file.input_path, PathOrFileDescriptor::Fd(Fd::INVALID)) => { - // PORT NOTE: `streams::FileSinkOptions` mirrors `file_sink::Options` - // but is a distinct draft type; bridge by-field until streams.rs - // aliases to this module's `Options`. - let opts = Options { - chunk_size: file.chunk_size as webcore::BlobSizeType, - input_path: match &file.input_path { - crate::webcore::PathOrFileDescriptor::Fd(fd) => { - PathOrFileDescriptor::Fd(*fd) - } - crate::webcore::PathOrFileDescriptor::Path(p) => { - // `ZigStringSlice` is non-`Clone` (owns/WTF-refs its - // bytes); borrow the bytes for the duration of - // `setup(&opts)` — `stream_start` (and thus `p`) - // outlives `opts` within this match arm. - PathOrFileDescriptor::Path( - bun_core::zig_string::Slice::from_utf8_never_free(p.slice()), - ) - } - }, - ..Options::default() - }; - match self.setup(&opts) { + match self.setup(file) { sys::Result::Err(err) => { return sys::Result::Err(err); } diff --git a/src/runtime/webcore/streams.rs b/src/runtime/webcore/streams.rs index 84d50daf19ee..d21f45042522 100644 --- a/src/runtime/webcore/streams.rs +++ b/src/runtime/webcore/streams.rs @@ -49,22 +49,8 @@ pub mod result { // Start // ────────────────────────────────────────────────────────────────────────── -/// Options payload for the `Start::FileSink` variant. Mirrors -/// `jsc.WebCore.FileSink.Options` (path-or-fd + chunk size). -// TODO(port): once `crate::webcore::file_sink::Options` is exported, alias to it. -pub struct FileSinkOptions { - pub chunk_size: BlobSizeType, - pub input_path: crate::webcore::PathOrFileDescriptor, -} - -impl Default for FileSinkOptions { - fn default() -> Self { - Self { - chunk_size: 0, - input_path: crate::webcore::PathOrFileDescriptor::Fd(Fd::INVALID), - } - } -} +/// Options payload for the `Start::FileSink` variant. +pub type FileSinkOptions = crate::webcore::file_sink::Options; pub enum Start { Empty, @@ -253,6 +239,7 @@ impl Start { // folded into the owning `ZigStringSlice`. path.to_slice(global_this)?, ), + ..Default::default() })); } else if let Some(fd_value) = value.get_truthy(global_this, b"fd")? { if !fd_value.is_any_int() { @@ -268,6 +255,7 @@ impl Start { return Ok(Start::FileSink(FileSinkOptions { chunk_size, input_path: crate::webcore::PathOrFileDescriptor::Fd(fd), + ..Default::default() })); } else { return Ok(Start::Err(SysError { @@ -281,6 +269,7 @@ impl Start { return Ok(Start::FileSink(FileSinkOptions { input_path: crate::webcore::PathOrFileDescriptor::Fd(Fd::INVALID), chunk_size, + ..Default::default() })); } StartTag::NetworkSink