graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(mcp): exit instead of racing an abandoned teardown thread (#325)
justrach53869eb5b99f
diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.jsonindex 05e52c9a..6851973f 100644--- a/scripts/eval/tier1-manifest.json+++ b/scripts/eval/tier1-manifest.json@@ -10,7 +10,7 @@"src/main.zig","src/repl.zig"],- "test_count_baseline": 529,+ "test_count_baseline": 530,"required_invariants": [{"id": "goal-epochs",diff --git a/src/main.zig b/src/main.zigindex 2a9a71d1..80d8df31 100644--- a/src/main.zig+++ b/src/main.zig@@ -374,7 +374,7 @@ pub fn main(init: std.process.Init) !void {const mcp_home = homeEnv(init.environ_map) orelse "";var registry_storage = try session_start.initRegistryConsent(io, gpa, arena, out, in, flags, mcp_config_path, mcp_home, use_color, json_mode, init.environ_map);boot.mark(io, "MCP registry");- defer registry_storage.deinit();+ defer registry_storage.deinitAtExit(); // #325: an abandoned teardown thread still holds `io`const registry: ?*mcp.Registry = ®istry_storage;// Per-skill/companion opt-outs, animation/theme settings, and the --selftest-spinner headless render live in session_start.zig. The theme/// limyuxi-glam reset `defer`s stay HERE (registered in main()'s own frame) so they fire when main() returns, not when the helper does.diff --git a/src/mcp.zig b/src/mcp.zigindex 73f17435..22304e32 100644--- a/src/mcp.zig+++ b/src/mcp.zig@@ -445,6 +445,15 @@ pub const Registry = struct {/// One shared window bounds the whole teardown: the loop is sequential, so/// a per-server grace cost N graces for N stalled peers (#305).+ /// Registry teardown for the EXIT path. If any transport was abandoned, a+ /// detached thread still holds this Io, so leave immediately rather than let+ /// a later defer tear that Io down underneath it (#325). Kept separate from+ /// `deinit` because tests and mid-session callers must never exit.+ pub fn deinitAtExit(reg: *Registry) void {+ reg.deinit();+ mcp_teardown.exitIfAbandoned();+ }+pub fn deinit(reg: *Registry) void {const budget: mcp_teardown.Budget = .init(reg.io, mcp_teardown.teardown_grace);for (reg.servers) |server| deinitServer(server, reg.io, budget);diff --git a/src/mcp_teardown.zig b/src/mcp_teardown.zigindex ed69d4fa..2e264461 100644--- a/src/mcp_teardown.zig+++ b/src/mcp_teardown.zig@@ -33,6 +33,20 @@ pub const teardown_grace = Io.Duration.fromSeconds(1);const poll_step = Io.Duration.fromMilliseconds(5);+/// Set when runBounded gives up on a task. That thread is detached, still holds+/// the `Io` it was handed, and is parked in the syscall that made it miss the+/// deadline. It almost certainly never returns to touch Io state - but "almost+/// certainly" is a race, not a proof, and the header above says so (#325).+pub var abandoned: std.atomic.Value(bool) = .init(false);++/// Exit NOW if any teardown was abandoned. For the EXIT path only: by this+/// point nothing useful is left to do, and the alternative is letting a later+/// defer tear down an Io that a live thread is still inside a syscall on.+/// Exiting is what makes the header's race unreachable instead of unlikely.+pub fn exitIfAbandoned() void {+ if (abandoned.load(.acquire)) std.process.exit(0);+}+/// One window shared by every transport in a teardown, rather than one window/// each. Registry.deinit tears servers down sequentially, so a per-transport/// grace made N stalled peers cost N graces: exit stayed bounded but grew with@@ -101,7 +115,9 @@ pub fn runBounded(allocator: Allocator, io: Io, grace: Io.Duration, comptime funreturn true;}// Abandoned: `task` is leaked on purpose so the detached thread never- // writes into freed memory.+ // writes into freed memory, and the exit path is told not to tear down the+ // Io that thread is still holding (#325).+ abandoned.store(true, .release);return false;}@@ -162,6 +178,31 @@ test "runBounded abandons teardown that outlives the grace window (#305)" {try std.testing.expect(elapsed < 10 * std.time.ns_per_s);}+test "runBounded flags an abandon so the exit path can skip Io teardown (#325)" {+ const io = std.testing.io;+ const saved = abandoned.load(.acquire);+ defer abandoned.store(saved, .release); // a global: never leak this test's state+ abandoned.store(false, .release);++ // A prompt task must NOT arm the escalation - the common case still runs+ // every remaining defer normally.+ const noop = struct {+ fn run() void {}+ }.run;+ try std.testing.expect(runBounded(std.testing.allocator, io, .fromSeconds(5), noop, .{}));+ try std.testing.expect(!abandoned.load(.acquire));++ // A stalled peer arms it. exitIfAbandoned is deliberately NOT called here:+ // it exits the process, so main's exit path is the only legal caller.+ const stall = struct {+ fn run(inner: Io) void {+ inner.sleep(.fromSeconds(60), .awake) catch {};+ }+ }.run;+ try std.testing.expect(!runBounded(std.heap.page_allocator, io, .fromMilliseconds(50), stall, .{io}));+ try std.testing.expect(abandoned.load(.acquire));+}+test "deinitHttpClient tears down a pooled client within the grace window" {// Mirrors the smolify on-demand transport: an HTTP client that was// connected (handshake done or not) must not hold session exit.@@ -181,12 +222,12 @@ test "one Budget bounds the WHOLE teardown, not each transport (#305)" {}}.run;const start = Io.Timestamp.now(io, .awake);- var abandoned: usize = 0;+ var abandoned_count: usize = 0;for (0..3) |_| {- if (!runBounded(std.heap.page_allocator, io, budget.remaining(io), stall, .{io})) abandoned += 1;+ if (!runBounded(std.heap.page_allocator, io, budget.remaining(io), stall, .{io})) abandoned_count += 1;}const elapsed = Io.Timestamp.now(io, .awake).nanoseconds - start.nanoseconds;- try std.testing.expectEqual(@as(usize, 3), abandoned);+ try std.testing.expectEqual(@as(usize, 3), abandoned_count);// The sharing itself is asserted on STATE, not on the clock: the first// abandon spends the window, so every later transport is handed a zero// grace and waits for nothing. An earlier version compared elapsed against