Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Free the MCP shape cache at session teardown

justrachbcfea6b160ac
diff --git a/src/mcp_shapes.zig b/src/mcp_shapes.zig
index de927f5e..1589fa83 100644
--- a/src/mcp_shapes.zig
+++ b/src/mcp_shapes.zig
@@ -27,6 +27,14 @@ pub fn reset(gpa: Allocator, io: Io) void {
clearUnlocked(gpa);
}
+/// Session teardown (#1196): free the cache with the allocator that filled it,
+/// before main()'s leak check. A no-op when nothing was stored.
+pub fn shutdown(io: Io) void {
+ store.mu.lockUncancelable(io);
+ defer store.mu.unlock(io);
+ clearUnlocked(store.gpa orelse return);
+}
+
fn clearUnlocked(gpa: Allocator) void {
var it = store.map.iterator();
while (it.next()) |e| {
@@ -443,6 +451,22 @@ pub fn lookup(io: Io, name: []const u8) ?[]const u8 {
return store.map.get(name);
}
+test "shutdown frees a filled cache and is a no-op when empty (#1196)" {
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ shutdown(io);
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ store.mu.lockUncancelable(io);
+ putUnlocked(gpa, arena_state.allocator(), "linear_issues", "{\"type\":\"object\"}");
+ store.mu.unlock(io);
+ try std.testing.expectEqual(@as(usize, 1), store.map.count());
+ shutdown(io); // testing.allocator fails the test on anything left behind
+ try std.testing.expectEqual(@as(usize, 0), store.map.count());
+ try std.testing.expect(store.gpa == null);
+ shutdown(io);
+}
+
test "infer strips values and keeps keys plus broad types" {
const gpa = std.testing.allocator;
var arena_state = std.heap.ArenaAllocator.init(gpa);
diff --git a/src/session_globals.zig b/src/session_globals.zig
new file mode 100644
index 00000000..82e66aab
--- /dev/null
+++ b/src/session_globals.zig
@@ -0,0 +1,19 @@
+//! Process-global state a session releases before main()'s exit-time leak
+//! check. The one-shot path and finalizeSession both end a run, so both call
+//! `release` rather than each keeping its own list that can fall out of step.
+const std = @import("std");
+const Io = std.Io;
+const Allocator = std.mem.Allocator;
+
+const presence = @import("presence.zig");
+
+pub fn release(gpa: Allocator, io: Io) void {
+ // #469: our presence record leaves the registry with us; a crashed session
+ // skips this and gets reaped by the next reader's liveness probe instead.
+ presence.retire(io);
+ @import("presence_accord.zig").stop(io);
+ presence.deinit(gpa);
+ @import("router_catalog.zig").shutdown(io);
+ // #1196: filled by the first successful MCP tool result.
+ @import("mcp_shapes.zig").shutdown(io);
+}
diff --git a/src/session_run.zig b/src/session_run.zig
index 5aed4645..f963a230 100644
--- a/src/session_run.zig
+++ b/src/session_run.zig
@@ -246,10 +246,7 @@ pub fn runOneshotPrompt(gpa: Allocator, io: Io, arena: Allocator, root: *agent_m
// before finalizeSession, so retire + free its gpa-owned globals here or
// they reach the exit-time leak check — and our record would linger in
// the registry until a peer's liveness probe reaps it.
- presence.retire(io);
- @import("presence_accord.zig").stop(io);
- presence.deinit(gpa);
- @import("router_catalog.zig").shutdown(io);
+ @import("session_globals.zig").release(gpa, io);
// #396: the run is over. The frontend hands the terminal back and latches
// its reader shut before main()'s teardown — a terminal-ownership move the
// sink owns now, in EVERY mode, exactly as the old unconditional call did.
@@ -474,12 +471,7 @@ pub fn finalizeSession(gpa: Allocator, io: Io, arena: Allocator, out: *Io.Writer
// Task-outcome telemetry: a working goal that never completed counts as
// abandoned. Before presence.retire so the event can't outlive the flush.
@import("task_outcome.zig").noteSessionEnd(root);
- // #469: our presence record leaves the registry with us; a crashed session
- // skips this and gets reaped by the next reader's liveness probe instead.
- presence.retire(io);
- @import("presence_accord.zig").stop(io);
- presence.deinit(gpa); // its gpa-owned globals must not reach the exit-time leak check
- @import("router_catalog.zig").shutdown(io);
+ @import("session_globals.zig").release(gpa, io); // retire presence, free gpa-owned globals
@import("workspace_switch.zig").deinitDisplay(gpa);
if (!json_mode and root.messages.items.len > 0) {
const sink = engine_sink.writerSink(out);