diff --git a/src/install/lifecycle_script_runner.zig b/src/install/lifecycle_script_runner.zig index 5778eb3c0065..ad6b3e90ab7a 100644 --- a/src/install/lifecycle_script_runner.zig +++ b/src/install/lifecycle_script_runner.zig @@ -186,7 +186,10 @@ pub const LifecycleScriptSubprocess = struct { combined_script, null, }; - if (Environment.isWindows) { + const buffer_output = this.manager.options.log_level != .silent and + !this.manager.options.log_level.isVerbose() and + !this.foreground; + if (Environment.isWindows and buffer_output) { this.stdout.source = .{ .pipe = bun.new(uv.Pipe, std.mem.zeroes(uv.Pipe)) }; this.stderr.source = .{ .pipe = bun.new(uv.Pipe, std.mem.zeroes(uv.Pipe)) }; } @@ -196,20 +199,16 @@ pub const LifecycleScriptSubprocess = struct { else .ignore, - .stdout = if (this.manager.options.log_level == .silent) - .ignore - else if (this.manager.options.log_level.isVerbose() or this.foreground) - .inherit + .stdout = if (!buffer_output) + if (this.manager.options.log_level == .silent) .ignore else .inherit else if (Environment.isPosix) .buffer else .{ .buffer = this.stdout.source.?.pipe, }, - .stderr = if (this.manager.options.log_level == .silent) - .ignore - else if (this.manager.options.log_level.isVerbose() or this.foreground) - .inherit + .stderr = if (!buffer_output) + if (this.manager.options.log_level == .silent) .ignore else .inherit else if (Environment.isPosix) .buffer else diff --git a/src/io/PipeReader.zig b/src/io/PipeReader.zig index 96048a13f551..2568a21a822d 100644 --- a/src/io/PipeReader.zig +++ b/src/io/PipeReader.zig @@ -980,11 +980,11 @@ pub const WindowsBufferedReader = struct { MaxBuf.removeFromPipereader(&this.maxbuf); this.buffer().deinit(); const source = this.source orelse return; - this.source = null; if (!source.isClosed()) { // closeImpl will take care of freeing the source this.closeImpl(false); } + this.source = null; } pub fn setRawMode(this: *WindowsBufferedReader, value: bool) bun.sys.Maybe(void) { @@ -1053,12 +1053,13 @@ pub const WindowsBufferedReader = struct { // Get parent before completing (fs.data may be null if detached) const parent_ptr = fs.data; - // ALWAYS complete the read first (cleans up fs_t, updates state) + // ALWAYS complete the read first (cleans up fs_t, updates state). + // NOTE: when detached (parent_ptr == null), complete() finalizes `file` + // (async close or immediate destroy) — must not be dereferenced past + // this point on the detached branch. file.complete(was_canceled); - // If detached, file should be closing itself now if (parent_ptr == null) { - bun.assert(file.state == .closing); // complete should have started close return; } @@ -1197,6 +1198,8 @@ pub const WindowsBufferedReader = struct { switch (source) { .sync_file, .file => |file| { // Detach - file will close itself after operation completes + // (or just free the struct if the caller owns the fd). + file.close_fd = this.flags.close_handle; file.detach(); }, .pipe => |pipe| { diff --git a/src/io/source.zig b/src/io/source.zig index 85af95ec627a..f2cc85c836f6 100644 --- a/src/io/source.zig +++ b/src/io/source.zig @@ -53,6 +53,10 @@ pub const Source = union(enum) { /// When true, file will close itself when the current operation completes. close_after_operation: bool = false, + /// When false, finalize() frees this struct without calling uv_fs_close + /// — the caller owns the fd. Mirrors BufferedReader.flags.close_handle. + close_fd: bool = true, + /// Get the File struct from an fs_t pointer using field offset. pub fn fromFS(fs: *uv.fs_t) *File { return @fieldParentPtr("fs", fs); @@ -84,9 +88,9 @@ pub const Source = union(enum) { } } - /// Detach from parent and schedule automatic cleanup. - /// If an operation is in progress, it will complete and then close the file. - /// If idle, closes the file immediately. + /// Detach from parent and schedule automatic cleanup. After this call, + /// `this` may be freed (immediately when idle and `close_fd == false`, + /// or via `onCloseComplete` once the async close finishes). pub fn detach(this: *File) void { this.fs.data = null; this.close_after_operation = true; @@ -94,12 +98,15 @@ pub const Source = union(enum) { if (this.state == .deinitialized) { this.close_after_operation = false; - this.startClose(); + this.finalize(); } } - /// Mark the operation as complete and clean up. - /// Must be called first in the callback before processing data. + /// Mark the operation as complete and clean up. Called first in the + /// callback. When detached with an op in flight (`close_after_operation`), + /// this frees `this` (immediately when `close_fd == false`, or via the + /// async close callback) — callers must not dereference `this` afterward + /// on that branch. pub fn complete(this: *File, was_canceled: bool) void { bun.assert(this.state == .operating or this.state == .canceling); if (was_canceled) { @@ -111,14 +118,21 @@ pub const Source = union(enum) { if (this.close_after_operation) { this.close_after_operation = false; - this.startClose(); + this.finalize(); } } - fn startClose(this: *File) void { + /// Either start an async close (when `close_fd`) or destroy immediately. + /// `this` must not be dereferenced after this returns. + fn finalize(this: *File) void { bun.assert(this.state == .deinitialized); - this.state = .closing; - _ = uv.uv_fs_close(uv.Loop.get(), &this.fs, this.file, onCloseComplete); + if (this.close_fd) { + this.state = .closing; + _ = uv.uv_fs_close(uv.Loop.get(), &this.fs, this.file, onCloseComplete); + } else { + // Caller owns the fd — free this struct without closing. + bun.default_allocator.destroy(this); + } } fn onCloseComplete(fs: *uv.fs_t) callconv(.c) void { @@ -285,8 +299,12 @@ pub const Source = union(enum) { log("openFile (fd = {f})", .{fd}); const file = bun.handleOom(bun.default_allocator.create(Source.File)); - file.* = std.mem.zeroes(Source.File); - file.file = fd.uv(); + file.* = .{ + .fs = std.mem.zeroes(uv.fs_t), + .iov = std.mem.zeroes(uv.uv_buf_t), + .file = fd.uv(), + // state/close_after_operation/close_fd take their declared defaults + }; return file; } diff --git a/src/runtime/webcore/FileReader.zig b/src/runtime/webcore/FileReader.zig index 0d26e3f3d2e2..9d53946923d9 100644 --- a/src/runtime/webcore/FileReader.zig +++ b/src/runtime/webcore/FileReader.zig @@ -242,6 +242,11 @@ pub fn onStart(this: *FileReader) streams.Start { this.waiting_for_onReaderDone = true; _ = this.parent().incrementCount(); } + } else if (comptime Environment.isWindows) { + if (this.reader.source != null and !this.reader.isDone()) { + this.waiting_for_onReaderDone = true; + _ = this.parent().incrementCount(); + } } if (comptime Environment.isPosix) { @@ -622,6 +627,11 @@ pub fn onReaderError(this: *FileReader, err: bun.sys.Error) void { this.pending.result = .{ .err = .{ .Error = err } }; this.pending.run(); + + if (this.waiting_for_onReaderDone) { + this.waiting_for_onReaderDone = false; + _ = this.parent().decrementCount(); + } } pub fn setRawMode(this: *FileReader, flag: bool) bun.sys.Maybe(void) { diff --git a/src/runtime/webcore/ReadableStream.zig b/src/runtime/webcore/ReadableStream.zig index c084d9ebae17..cf29349dd973 100644 --- a/src/runtime/webcore/ReadableStream.zig +++ b/src/runtime/webcore/ReadableStream.zig @@ -429,7 +429,6 @@ pub fn NewSource( pending_err: ?Syscall.Error = null, close_handler: ?*const fn (?*anyopaque) void = null, close_ctx: ?*anyopaque = null, - close_jsvalue: jsc.Strong.Optional = .empty, cancel_handler: ?*const fn (?*anyopaque) void = null, cancel_ctx: ?*anyopaque = null, globalThis: *JSGlobalObject = undefined, @@ -517,7 +516,6 @@ pub fn NewSource( this.ref_count -= 1; if (this.ref_count == 0) { - this.close_jsvalue.deinit(); deinit_fn(&this.context); return 0; } @@ -694,7 +692,7 @@ pub fn NewSource( this.globalThis = globalObject; if (value.isUndefined()) { - this.close_jsvalue.deinit(); + if (this.this_jsvalue != .zero) js.onCloseCallbackSetCached(this.this_jsvalue, globalObject, .js_undefined); return; } @@ -702,7 +700,7 @@ pub fn NewSource( return globalObject.throwInvalidArgumentType("ReadableStreamSource", "onclose", "function"); } const cb = value.withAsyncContextIfNeeded(globalObject); - this.close_jsvalue.set(globalObject, cb); + if (this.this_jsvalue != .zero) js.onCloseCallbackSetCached(this.this_jsvalue, globalObject, cb); } pub fn setOnDrainFromJS(this: *ReadableStreamSourceType, globalObject: *jsc.JSGlobalObject, value: jsc.JSValue) bun.JSError!void { @@ -726,7 +724,10 @@ pub fn NewSource( jsc.markBinding(@src()); - return this.close_jsvalue.get() orelse .js_undefined; + if (this.this_jsvalue != .zero) { + if (js.onCloseCallbackGetCached(this.this_jsvalue)) |val| return val; + } + return .js_undefined; } pub fn getOnDrainFromJS(this: *ReadableStreamSourceType, globalObject: *jsc.JSGlobalObject) jsc.JSValue { @@ -754,11 +755,12 @@ pub fn NewSource( fn onClose(ptr: ?*anyopaque) void { jsc.markBinding(@src()); var this = bun.cast(*ReadableStreamSourceType, ptr.?); - if (this.close_jsvalue.trySwap()) |cb| { - this.globalThis.queueMicrotask(cb, &.{}); + if (this.this_jsvalue != .zero) { + if (js.onCloseCallbackGetCached(this.this_jsvalue)) |cb| { + if (!cb.isUndefined()) this.globalThis.queueMicrotask(cb, &.{}); + js.onCloseCallbackSetCached(this.this_jsvalue, this.globalThis, .js_undefined); + } } - - this.close_jsvalue.deinit(); } pub fn finalize(this: *ReadableStreamSourceType) void {