From 9a7cc8faed186668e08a2b15df6c29482e9e570f Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 10 Mar 2026 15:46:02 +0100 Subject: [PATCH] refactor: implement transport_error --- src/remote/endpoint.zig | 3 ++- test/remote_child_endpoint.zig | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/remote/endpoint.zig b/src/remote/endpoint.zig index 4bcb450..72d70b9 100644 --- a/src/remote/endpoint.zig +++ b/src/remote/endpoint.zig @@ -165,7 +165,8 @@ const Endpoint = struct { if (self.proxies.get(e.id)) |p| p.send(.{ "exit", e.reason }) catch {}; }, - .link, .transport_error => return tp.exit_error(error.UnexpectedMessage, null), + .transport_error => |te| return tp.exit(te.reason), + .link => return tp.exit_error(error.UnexpectedMessage, null), } } diff --git a/test/remote_child_endpoint.zig b/test/remote_child_endpoint.zig index fdb3e8a..5fbfe22 100644 --- a/test/remote_child_endpoint.zig +++ b/test/remote_child_endpoint.zig @@ -218,6 +218,7 @@ const StdioEndpoint = struct { try self.dispatch_stdin(); try self.fd_stdin.wait_read(); } else if (try m.match(.{ "fd", "stdin", "read_error", tp.any, tp.any })) { + self.send_transport_error("stdin_closed"); return tp.exit("stdin_closed"); } else if (try m.match(.{ "send", tp.extract(&from_id), tp.extract(&to_id_wire), cbor.extract_cbor(&payload) })) { try self.send_wire_by_id(from_id, to_id_wire, payload); @@ -233,7 +234,10 @@ const StdioEndpoint = struct { error.WouldBlock => return, else => return tp.exit_error(e, @errorReturnTrace()), }; - if (n == 0) return tp.exit("stdin_closed"); + if (n == 0) { + self.send_transport_error("stdin_closed"); + return tp.exit("stdin_closed"); + } if (self.accumulator.feed(self.read_buf[0..n])) |frame| { try self.dispatch_frame(frame); } @@ -254,7 +258,8 @@ const StdioEndpoint = struct { else return tp.exit_error(error.UnknownWireId, null); }, - .link, .exit, .transport_error => return tp.exit_error(error.UnexpectedMessage, null), + .transport_error => |te| return tp.exit(te.reason), + .link, .exit => return tp.exit_error(error.UnexpectedMessage, null), } } @@ -279,6 +284,16 @@ const StdioEndpoint = struct { try framing.write_frame(&frame_stream, msg_stream.buffered()); try std.fs.File.stdout().writeAll(frame_stream.buffered()); } + + fn send_transport_error(_: *@This(), reason: []const u8) void { + var msg_buf: [framing.max_frame_size]u8 = undefined; + var msg_stream: std.Io.Writer = .fixed(&msg_buf); + protocol.encode_transport_error(&msg_stream, reason) catch return; + var frame_buf: [framing.max_frame_size + 4]u8 = undefined; + var frame_stream: std.Io.Writer = .fixed(&frame_buf); + framing.write_frame(&frame_stream, msg_stream.buffered()) catch return; + std.fs.File.stdout().writeAll(frame_stream.buffered()) catch {}; + } }; // ---------------------------------------------------------------------------