From 6d6a109a086361d300da675db8445ede74be58f6 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Mon, 13 Feb 2023 19:59:45 -0600 Subject: [PATCH] fix: gracefully exit fix stack overflow bug in State.deinit allow for code in another thread to signal shutdown to gdbstub --- src/Server.zig | 8 +++++++- src/State.zig | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Server.zig b/src/Server.zig index ac39a23..2901869 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -4,6 +4,7 @@ const Packet = @import("Packet.zig"); const Socket = network.Socket; const Allocator = std.mem.Allocator; +const Atomic = std.atomic.Atomic; const Emulator = @import("lib.zig").Emulator; const Self = @This(); @@ -94,16 +95,21 @@ const Action = union(enum) { nack, }; -pub fn run(self: *Self, allocator: Allocator) !void { +pub fn run(self: *Self, allocator: Allocator, quit: *Atomic(bool)) !void { var buf: [Packet.max_len]u8 = undefined; while (true) { const len = try self.client.receive(&buf); if (len == 0) break; + if (quit.load(.Monotonic)) break; const action = try self.parse(allocator, buf[0..len]); try self.send(allocator, action); } + + // Just in case its the gdbstub that exited first, + // attempt to signal to the GUI that it should also exit + quit.store(true, .Monotonic); } fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action { diff --git a/src/State.zig b/src/State.zig index 5c4e0f7..93c0e5d 100644 --- a/src/State.zig +++ b/src/State.zig @@ -25,7 +25,7 @@ const SwBkpt = struct { } pub fn deinit(self: *@This()) void { - self.deinit(); + self.list.deinit(); self.* = undefined; }