Skip to content

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

Python

MCP HTTP: retry the cancel notice when local ports run out (#1794)

justrach0b494a5bd7c8
diff --git a/src/additional_tests.zig b/src/additional_tests.zig
index d2332709..609e6533 100644
--- a/src/additional_tests.zig
+++ b/src/additional_tests.zig
@@ -28,6 +28,7 @@ test {
_ = @import("engine_events_tests.zig");
_ = @import("run_task_meta.zig");
_ = @import("acp_run_task_tests.zig");
+ _ = @import("mcp_cancel_notice.zig");
// Claude Agent provider (ADR 0309).
_ = @import("claude_agent_tests.zig");
_ = @import("claude_agent_login.zig");
diff --git a/src/mcp_cancel_notice.zig b/src/mcp_cancel_notice.zig
new file mode 100644
index 00000000..57024b41
--- /dev/null
+++ b/src/mcp_cancel_notice.zig
@@ -0,0 +1,82 @@
+//! `notifications/cancelled` for a legacy Streamable HTTP session: graff's
+//! word to the server that it stopped waiting on a request.
+//!
+//! The hung request still holds its connection, so the notice needs a new
+//! one. On a busy machine every local port can be in TIME_WAIT for a moment
+//! and the connect fails with `AddressUnavailable`; the notice used to be
+//! dropped then, silently, and the server kept working on a request nobody
+//! wanted. Ports free up within moments, so that one error is retried for a
+//! bounded time. Any other failure still gives up at once: it is best effort.
+
+const std = @import("std");
+const mcp_http = @import("mcp_http.zig");
+const mcp_notify = @import("mcp_notify.zig");
+
+/// Tries after the first, 100 ms apart: two seconds at most.
+const retries = 20;
+
+/// Tell a legacy session to stop request `id`. Bounded, and the turn's
+/// cancel flag (still set) must not abort this send.
+pub fn notify(http: *mcp_http.HttpTransport, protocol_version: []const u8, id: i64) void {
+ var buf: [256]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&buf);
+ const line = mcp_notify.cancelledLine(fba.allocator(), id) catch return;
+ const Send = struct {
+ http: *mcp_http.HttpTransport,
+ line: []const u8,
+ protocol_version: []const u8,
+
+ fn send(s: @This()) anyerror!void {
+ const reply = try mcp_http.postWith(s.http, s.line, .{ .protocol_version = s.protocol_version, .method = "notifications/cancelled", .modern = false }, null, .{ .watch_cancel = false, .timeout_s = 3 });
+ if (reply) |bytes| s.http.client.allocator.free(bytes);
+ }
+
+ fn pause(s: @This()) bool {
+ s.http.client.io.sleep(.fromMilliseconds(100), .awake) catch return false;
+ return true;
+ }
+ };
+ _ = retrying(Send, .{ .http = http, .line = line, .protocol_version = protocol_version }, Send.send, Send.pause);
+}
+
+/// Calls `send` until it succeeds, fails with anything but
+/// `AddressUnavailable`, `pause` says stop, or the tries run out. Returns
+/// how many times it sent.
+fn retrying(comptime Ctx: type, ctx: Ctx, comptime send: fn (Ctx) anyerror!void, comptime pause: fn (Ctx) bool) usize {
+ var sent: usize = 0;
+ while (true) {
+ sent += 1;
+ send(ctx) catch |err| {
+ if (err != error.AddressUnavailable or sent > retries or !pause(ctx)) return sent;
+ continue;
+ };
+ return sent;
+ }
+}
+
+const Fake = struct {
+ fail: []const anyerror,
+ sent: *usize,
+
+ fn send(f: Fake) anyerror!void {
+ defer f.sent.* += 1;
+ if (f.sent.* < f.fail.len) return f.fail[f.sent.*];
+ }
+
+ fn pause(_: Fake) bool {
+ return true;
+ }
+};
+
+test "cancel notice: out of ports is retried until it goes; other errors and the bound stop it" {
+ var n: usize = 0;
+ // Two connects out of ports, then the notice goes.
+ try std.testing.expectEqual(@as(usize, 3), retrying(Fake, .{ .fail = &.{ error.AddressUnavailable, error.AddressUnavailable }, .sent = &n }, Fake.send, Fake.pause));
+ // A refused or timed-out server is not waited for.
+ n = 0;
+ try std.testing.expectEqual(@as(usize, 1), retrying(Fake, .{ .fail = &.{error.ConnectionRefused}, .sent = &n }, Fake.send, Fake.pause));
+ // Never a free port: bounded.
+ n = 0;
+ const always: [retries + 5]anyerror = @splat(error.AddressUnavailable);
+ try std.testing.expectEqual(@as(usize, retries + 1), retrying(Fake, .{ .fail = &always, .sent = &n }, Fake.send, Fake.pause));
+}
diff --git a/src/mcp_http.zig b/src/mcp_http.zig
index 416867f1..cca975f5 100644
--- a/src/mcp_http.zig
+++ b/src/mcp_http.zig
@@ -353,16 +353,6 @@ pub fn postWith(http: *HttpTransport, body: []const u8, meta: RequestMeta, expec
}
}
-/// Tell a legacy session to stop request `id`. Best effort: bounded, and the
-/// turn's cancel flag (still set) must not abort this send.
-pub fn notifyCancelled(http: *HttpTransport, protocol_version: []const u8, id: i64) void {
- var buf: [256]u8 = undefined;
- var fba = std.heap.FixedBufferAllocator.init(&buf);
- const line = mcp_notify.cancelledLine(fba.allocator(), id) catch return;
- const reply = postWith(http, line, .{ .protocol_version = protocol_version, .method = "notifications/cancelled", .modern = false }, null, .{ .watch_cancel = false, .timeout_s = 3 }) catch return;
- if (reply) |bytes| http.client.allocator.free(bytes);
-}
-
/// A probe reply: the raw status and (if any) body, on ANY status — unlike
/// `post`, a probe never errors on a non-2xx status, since the whole point
/// is to hand the caller the error body so `mcp_protocol.classifyProbe` can
diff --git a/src/mcp_rpc.zig b/src/mcp_rpc.zig
index 6903af9c..b13b525c 100644
--- a/src/mcp_rpc.zig
+++ b/src/mcp_rpc.zig
@@ -221,7 +221,7 @@ pub fn request(server: *Server, response_alloc: Allocator, params: []const u8, m
}, id) catch |err| {
// A modern request is cancelled by dropping it; a legacy
// session is told which request to stop.
- if (err == error.McpCancelled and !modern) mcp_http.notifyCancelled(http, server.protocol_version, id);
+ if (err == error.McpCancelled and !modern) @import("mcp_cancel_notice.zig").notify(http, server.protocol_version, id);
return err;
}) orelse return error.BadMcpResponse;
defer http.client.allocator.free(response_body);