Use group to make it easier to clean up client tasks on exit

This commit is contained in:
2026-01-01 21:41:53 +00:00
parent a3e026ebf1
commit 5a395bdadf

View File

@@ -83,6 +83,9 @@ pub fn start(server: *Server, io: std.Io, gpa: std.mem.Allocator) !void {
), io, .{ .reuse_address = true });
defer tcp_server.deinit(io);
var client_group: std.Io.Group = .init;
defer client_group.cancel(io);
var id: usize = 0;
// Run until SIGINT is handled, then exit gracefully
while (true) : (id +%= 1) {
@@ -90,7 +93,7 @@ pub fn start(server: *Server, io: std.Io, gpa: std.mem.Allocator) !void {
if (server.clients.contains(id)) continue;
const stream = try tcp_server.accept(io);
std.debug.print("accepted connection\n", .{});
_ = io.concurrent(handleConnection, .{ server, gpa, io, id, stream }) catch {
_ = client_group.concurrent(io, handleConnectionInfallible, .{ server, gpa, io, id, stream }) catch {
std.debug.print("could not start concurrent handler for {d}\n", .{id});
stream.close(io);
};
@@ -118,6 +121,16 @@ fn removeClient(server: *Server, io: std.Io, allocator: std.mem.Allocator, id: u
}
}
fn handleConnectionInfallible(
server: *Server,
server_allocator: std.mem.Allocator,
io: std.Io,
id: usize,
stream: std.Io.net.Stream,
) void {
handleConnection(server, server_allocator, io, id, stream) catch {};
}
fn handleConnection(
server: *Server,
server_allocator: std.mem.Allocator,