From 2196ffcc09aa46fc961dd293f94eaac4ee52a81f Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Mon, 12 Feb 2024 00:50:47 -0800 Subject: [PATCH] Fix test failure --- src/bun.js/webcore/streams.zig | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/bun.js/webcore/streams.zig b/src/bun.js/webcore/streams.zig index a3e6c30851..98426ea6b1 100644 --- a/src/bun.js/webcore/streams.zig +++ b/src/bun.js/webcore/streams.zig @@ -2876,7 +2876,13 @@ pub const FileSink = struct { pub fn onWrite(this: *FileSink, amount: usize, done: bool) void { log("onWrite({d}, {any})", .{ amount, done }); + + // Only keep the event loop ref'd while there's a pending write in progress. + // If there's no pending write, no need to keep the event loop ref'd. + this.writer.updateRef(this.eventLoop(), false); + this.written += amount; + if (this.pending.state == .pending) this.pending.consumed += @truncate(amount); @@ -2954,7 +2960,11 @@ pub const FileSink = struct { _ = bun.sys.close(fd); return .{ .err = err }; }, - .result => {}, + .result => { + // Only keep the event loop ref'd while there's a pending write in progress. + // If there's no pending write, no need to keep the event loop ref'd. + this.writer.updateRef(this.eventLoop(), false); + }, } return .{ .result = {} }; @@ -3159,6 +3169,9 @@ pub const FileSink = struct { return .{ .err = err }; }, .pending => |pending_written| { + // Pending writes keep the event loop ref'd + this.writer.updateRef(this.eventLoop(), true); + this.pending.consumed += @truncate(pending_written); this.pending.result = .{ .owned = @truncate(pending_written) }; return .{ .pending = &this.pending };