refactor: add remote exit test
This commit is contained in:
parent
981ab22c5c
commit
bbcc800964
3 changed files with 295 additions and 3 deletions
|
|
@ -10,8 +10,17 @@
|
|||
/// so the parent can establish a proxy and reply by ID.
|
||||
/// - If from_id != 0: reply mode — sends wire send to from_id with ["done"].
|
||||
///
|
||||
/// "die_test" (wire ID 4): receives {from_id, payload}.
|
||||
/// - If from_id == 0: trigger mode — echoes payload back to "test_receiver"
|
||||
/// so the parent can establish a proxy for this actor.
|
||||
/// - If from_id != 0: die mode — exits with "die_test", triggering the
|
||||
/// cross-process link/exit propagation path.
|
||||
///
|
||||
/// StdioEndpoint dispatches inbound wire messages to actors, passing
|
||||
/// {from_id, payload} so actors can reply by ID when needed.
|
||||
///
|
||||
/// StdioEndpoint also supports the wire "link" message: it monitors the
|
||||
/// referenced local actor and sends ["exit", wire_id, reason] when it exits.
|
||||
const std = @import("std");
|
||||
const tp = @import("thespian");
|
||||
const cbor = @import("cbor");
|
||||
|
|
@ -138,6 +147,61 @@ const EchoIdActor = struct {
|
|||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// DieTestActor (wire ID 4)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const DieTestActor = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
receiver: tp.Receiver(*@This()),
|
||||
|
||||
const Args = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
parent: tp.pid,
|
||||
};
|
||||
|
||||
fn start(args: Args) tp.result {
|
||||
return init(args) catch |e| return tp.exit_error(e, @errorReturnTrace());
|
||||
}
|
||||
|
||||
fn init(args: Args) !void {
|
||||
defer args.parent.deinit();
|
||||
const self = try args.allocator.create(@This());
|
||||
self.* = .{
|
||||
.allocator = args.allocator,
|
||||
.receiver = .init(receive_fn, deinit, self),
|
||||
};
|
||||
errdefer self.deinit();
|
||||
try args.parent.send(.{"die_test_ready"});
|
||||
tp.receive(&self.receiver);
|
||||
}
|
||||
|
||||
fn deinit(self: *@This()) void {
|
||||
self.allocator.destroy(self);
|
||||
}
|
||||
|
||||
fn receive_fn(self: *@This(), from: tp.pid_ref, m: tp.message) tp.result {
|
||||
return self.receive(from, m) catch |e| return tp.exit_error(e, @errorReturnTrace());
|
||||
}
|
||||
|
||||
fn receive(self: *@This(), from: tp.pid_ref, m: tp.message) !void {
|
||||
var from_id: u64 = 0;
|
||||
var payload: []const u8 = "";
|
||||
if (try m.match(.{ tp.extract(&from_id), cbor.extract_cbor(&payload) })) {
|
||||
if (from_id == 0) {
|
||||
// Trigger mode: echo payload back so the parent can establish a proxy.
|
||||
try from.send(.{ "send", @as(u64, 4), "test_receiver", protocol.RawCbor{ .bytes = payload } });
|
||||
} else {
|
||||
// Die mode: any message from a non-zero sender causes a clean exit.
|
||||
return tp.exit("die_test");
|
||||
}
|
||||
} else {
|
||||
return tp.unexpected(m);
|
||||
}
|
||||
_ = self;
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// StdioEndpoint
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -149,6 +213,8 @@ const StdioEndpoint = struct {
|
|||
read_buf: [4096]u8,
|
||||
/// Maps wire ID → local actor pid for routing inbound send-by-ID.
|
||||
wire_ids: std.AutoHashMap(u64, tp.pid),
|
||||
/// Maps actor instance_id → wire ID for exit notification via wire "link".
|
||||
link_notify: std.AutoHashMap(usize, u64),
|
||||
/// Number of child actors that have reported ready; arm stdin when all ready.
|
||||
ready_count: u8,
|
||||
receiver: tp.Receiver(*@This()),
|
||||
|
|
@ -168,11 +234,16 @@ const StdioEndpoint = struct {
|
|||
.accumulator = .{},
|
||||
.read_buf = undefined,
|
||||
.wire_ids = std.AutoHashMap(u64, tp.pid).init(args.allocator),
|
||||
.link_notify = std.AutoHashMap(usize, u64).init(args.allocator),
|
||||
.ready_count = 0,
|
||||
.receiver = .init(receive_fn, deinit, self),
|
||||
};
|
||||
errdefer self.deinit();
|
||||
|
||||
// Trap exit signals so linked actor exits arrive as messages,
|
||||
// allowing us to send wire "exit" notifications to the parent.
|
||||
_ = tp.set_trap(true);
|
||||
|
||||
_ = try tp.spawn_link(args.allocator, EchoActor.Args{
|
||||
.allocator = args.allocator,
|
||||
.parent = tp.self_pid().clone(),
|
||||
|
|
@ -183,6 +254,11 @@ const StdioEndpoint = struct {
|
|||
.parent = tp.self_pid().clone(),
|
||||
}, EchoIdActor.start, "echo_id");
|
||||
|
||||
_ = try tp.spawn_link(args.allocator, DieTestActor.Args{
|
||||
.allocator = args.allocator,
|
||||
.parent = tp.self_pid().clone(),
|
||||
}, DieTestActor.start, "die_test");
|
||||
|
||||
tp.receive(&self.receiver);
|
||||
}
|
||||
|
||||
|
|
@ -190,6 +266,7 @@ const StdioEndpoint = struct {
|
|||
var it = self.wire_ids.valueIterator();
|
||||
while (it.next()) |p| p.deinit();
|
||||
self.wire_ids.deinit();
|
||||
self.link_notify.deinit();
|
||||
self.fd_stdin.deinit();
|
||||
self.allocator.destroy(self);
|
||||
}
|
||||
|
|
@ -203,17 +280,34 @@ const StdioEndpoint = struct {
|
|||
var to_id_wire: u64 = 0;
|
||||
var to_name: []const u8 = "";
|
||||
var payload: []const u8 = "";
|
||||
var reason: []const u8 = "";
|
||||
|
||||
if (try m.match(.{"echo_ready"})) {
|
||||
tp.env.get().proc_set("echo", from);
|
||||
try self.wire_ids.put(2, from.clone());
|
||||
self.ready_count += 1;
|
||||
if (self.ready_count == 2) try self.fd_stdin.wait_read();
|
||||
if (self.ready_count == 3) try self.fd_stdin.wait_read();
|
||||
} else if (try m.match(.{"echo_id_ready"})) {
|
||||
tp.env.get().proc_set("echo_id", from);
|
||||
try self.wire_ids.put(3, from.clone());
|
||||
self.ready_count += 1;
|
||||
if (self.ready_count == 2) try self.fd_stdin.wait_read();
|
||||
if (self.ready_count == 3) try self.fd_stdin.wait_read();
|
||||
} else if (try m.match(.{"die_test_ready"})) {
|
||||
tp.env.get().proc_set("die_test", from);
|
||||
try self.wire_ids.put(4, from.clone());
|
||||
self.ready_count += 1;
|
||||
if (self.ready_count == 3) try self.fd_stdin.wait_read();
|
||||
} else if (try m.match(.{ "exit", tp.extract(&reason) })) {
|
||||
// A linked actor has exited. If it was set up via a wire "link",
|
||||
// send ["exit", wire_id, reason] to the parent. Otherwise the exit
|
||||
// is from a sibling actor dying unexpectedly — propagate cleanly.
|
||||
const actor_id = from.instance_id();
|
||||
if (self.link_notify.fetchRemove(actor_id)) |entry| {
|
||||
self.send_wire_exit(entry.value, reason) catch {};
|
||||
} else {
|
||||
self.send_transport_error(reason);
|
||||
return tp.exit(reason);
|
||||
}
|
||||
} else if (try m.match(.{ "fd", "stdin", "read_ready" })) {
|
||||
try self.dispatch_stdin();
|
||||
try self.fd_stdin.wait_read();
|
||||
|
|
@ -265,7 +359,17 @@ const StdioEndpoint = struct {
|
|||
if (self.wire_ids.get(e.id)) |actor|
|
||||
actor.send(.{ "exit", e.reason }) catch {};
|
||||
},
|
||||
.link => return tp.exit_error(error.LinkNotSupported, null),
|
||||
.link => |lnk| {
|
||||
// Remote side is requesting a link to one of our local actors.
|
||||
// lnk.remote_id is the wire ID of our local actor to monitor.
|
||||
// When that actor exits, we send ["exit", wire_id, reason].
|
||||
if (self.wire_ids.get(lnk.remote_id)) |actor| {
|
||||
try actor.link();
|
||||
try self.link_notify.put(actor.instance_id(), lnk.remote_id);
|
||||
} else {
|
||||
return tp.exit_error(error.UnknownWireId, null);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -291,6 +395,17 @@ const StdioEndpoint = struct {
|
|||
try std.fs.File.stdout().writeAll(frame_stream.buffered());
|
||||
}
|
||||
|
||||
fn send_wire_exit(_: *@This(), wire_id: u64, reason: []const u8) !void {
|
||||
var msg_buf: [framing.max_frame_size]u8 = undefined;
|
||||
var msg_stream: std.Io.Writer = .fixed(&msg_buf);
|
||||
try protocol.encode_exit(&msg_stream, wire_id, reason);
|
||||
|
||||
var frame_buf: [framing.max_frame_size + 4]u8 = undefined;
|
||||
var frame_stream: std.Io.Writer = .fixed(&frame_buf);
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue