From d0ee302e37eeccf6acec49a0fb7c6f35d28c7054 Mon Sep 17 00:00:00 2001 From: Zack Radisic <56137411+zackradisic@users.noreply.github.com> Date: Thu, 22 Feb 2024 17:45:28 -0800 Subject: [PATCH] Actually buffer subproc output --- src/shell/interpreter.zig | 3 ++- src/shell/subproc.zig | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/shell/interpreter.zig b/src/shell/interpreter.zig index a961dda447..2605e02d12 100644 --- a/src/shell/interpreter.zig +++ b/src/shell/interpreter.zig @@ -3625,7 +3625,8 @@ pub const Interpreter = struct { log("cmd ({x}) close buffered stdout", .{@intFromPtr(this)}); if (this.io.stdout == .std and this.io.stdout.std.captured != null and !this.node.redirect.stdout) { var buf = this.io.stdout.std.captured.?; - buf.append(bun.default_allocator, this.exec.subproc.child.stdout.pipe.slice()) catch bun.outOfMemory(); + const the_slice = this.exec.subproc.child.stdout.pipe.slice(); + buf.append(bun.default_allocator, the_slice) catch bun.outOfMemory(); } this.exec.subproc.buffered_closed.close(this, .{ .stdout = &this.exec.subproc.child.stdout }); this.exec.subproc.child.closeIO(.stdout); diff --git a/src/shell/subproc.zig b/src/shell/subproc.zig index fab3d9a6cd..65165eb69f 100644 --- a/src/shell/subproc.zig +++ b/src/shell/subproc.zig @@ -683,6 +683,7 @@ pub const PipeReader = struct { stdio_result: StdioResult, captured_writer: CapturedWriter = .{}, out_type: bun.shell.subproc.ShellSubprocess.OutKind, + buffered: bun.ByteList = .{}, ref_count: u32 = 1, pub usingnamespace bun.NewRefCounted(PipeReader, deinit); @@ -819,6 +820,7 @@ pub const PipeReader = struct { pub fn onReadChunk(ptr: *anyopaque, chunk: []const u8, has_more: bun.io.ReadState) bool { var this: *PipeReader = @ptrCast(@alignCast(ptr)); + this.buffered.append(bun.default_allocator, chunk) catch bun.outOfMemory(); log("PipeReader onReadChunk({x}, ...)", .{@intFromPtr(this)}); if (this.captured_writer.writer.getPoll() == null) { this.captured_writer.writer.handle = .{ .poll = Async.FilePoll.init(this.eventLoop(), if (this.out_type == .stdout) bun.STDOUT_FD else bun.STDERR_FD, .{}, @TypeOf(this.captured_writer.writer), &this.captured_writer.writer) }; @@ -881,7 +883,7 @@ pub const PipeReader = struct { } pub fn slice(this: *PipeReader) []const u8 { - return this.reader.buffer().items[0..]; + return this.buffered.slice(); } pub fn toOwnedSlice(this: *PipeReader) []u8 { @@ -980,6 +982,8 @@ pub const PipeReader = struct { bun.default_allocator.free(this.state.done); } + this.buffered.deinitWithAllocator(bun.default_allocator); + this.reader.deinit(); this.destroy(); }