Skip to content

Commit

Permalink
fix(ipc) fix closing edge case (#13413)
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari authored Aug 19, 2024
1 parent d55b5cc commit 1367e5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 0 additions & 7 deletions src/bun.js/ipc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ const NamedPipeIPCData = struct {
incoming: bun.ByteList = .{}, // Maybe we should use IPCBuffer here as well
connected: bool = false,
disconnected: bool = false,
has_sended_first_message: bool = false,
connect_req: uv.uv_connect_t = std.mem.zeroes(uv.uv_connect_t),
server: ?*uv.Pipe = null,
onClose: ?CloseHandler = null,
Expand Down Expand Up @@ -809,12 +808,6 @@ fn NewNamedPipeIPCHandler(comptime Context: type) type {

fn onRead(this: *Context, buffer: []const u8) void {
const ipc = this.ipc();
if (!ipc.has_sended_first_message) {
// the server will wait to send the first flush (aka the version) after receiving the first message (which is the client version)
// this works like a handshake to ensure that both ends are listening to the messages
_ = ipc.writer.flush();
ipc.has_sended_first_message = true;
}

log("NewNamedPipeIPCHandler#onRead {d}", .{buffer.len});
ipc.incoming.len += @as(u32, @truncate(buffer.len));
Expand Down
16 changes: 12 additions & 4 deletions src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3845,8 +3845,14 @@ pub const VirtualMachine = struct {
extern fn Bun__setChannelRef(*JSC.JSGlobalObject, bool) void;

export fn Bun__closeChildIPC(global: *JSGlobalObject) void {
const ipc_data = &global.bunVM().ipc.?.initialized.data;
JSC.VirtualMachine.get().enqueueImmediateTask(JSC.ManagedTask.New(IPC.IPCData, closeReal).init(ipc_data));
if (global.bunVM().ipc) |*current_ipc| {
switch (current_ipc.*) {
.initialized => |instance| {
JSC.VirtualMachine.get().enqueueImmediateTask(JSC.ManagedTask.New(IPC.IPCData, closeReal).init(&instance.data));
},
.waiting => {},
}
}
}

fn closeReal(ipc_data: *IPC.IPCData) void {
Expand Down Expand Up @@ -3882,6 +3888,8 @@ pub const VirtualMachine = struct {
.data = undefined,
});

this.ipc = .{ .initialized = instance };

const socket = IPC.Socket.fromFd(context, opts.info, IPCInstance, instance, null) orelse {
instance.destroy();
this.ipc = null;
Expand All @@ -3901,6 +3909,8 @@ pub const VirtualMachine = struct {
.data = .{ .mode = opts.mode },
});

this.ipc = .{ .initialized = instance };

instance.data.configureClient(IPCInstance, instance, opts.info) catch {
instance.destroy();
this.ipc = null;
Expand All @@ -3912,8 +3922,6 @@ pub const VirtualMachine = struct {
},
};

this.ipc = .{ .initialized = instance };

instance.data.writeVersionPacket();

return instance;
Expand Down

0 comments on commit 1367e5e

Please sign in to comment.