graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(#56): fail-safe the concurrency-starved SSE/WS fallbacks (Fix-B)
justracha5dbf6ae51c2
diff --git a/src/agent_stream.zig b/src/agent_stream.zigindex 76011d3a..83f50cfc 100644--- a/src/agent_stream.zig+++ b/src/agent_stream.zig@@ -261,14 +261,14 @@ pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []consvar hd_buf: [2]HeadDone = undefined;var hsel: Io.Select(HeadDone) = .init(self.io, &hd_buf);hsel.concurrent(.sent, sendHeadTask, .{ &req, body, &response }) catch {- // No spare concurrency: accept the hang risk (existing behavior).- req.transfer_encoding = .{ .content_length = body.len };- var bw = try req.sendBodyUnflushed(&.{});- try bw.writer.writeAll(body);- try bw.end();- try req.connection.?.flush();- response = try req.receiveHead(&.{});- break :head;+ // #56 Fix-B: Io pool exhausted (deep subagent fan-out saturated the+ // thread pool) — we can't spawn the head-stall watchdog. A bare+ // send+receiveHead here could hang forever on a half-open socket with+ // nothing to trip it. Nothing has streamed yet, so fail safe: poison the+ // connection and return a retryable HungRequest so request() backs off+ // and redials once a pool slot frees, instead of blocking the turn.+ if (req.connection) |conn| conn.closing = true;+ return error.HungRequest;};hsel.concurrent(.stall, headStallTask, .{ self.io, orig_tio != null }) catch {const r = hsel.await() catch |e| {@@ -342,12 +342,21 @@ pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []consvar rd_buf: [2]ReadDone = undefined;var rsel: Io.Select(ReadDone) = .init(self.io, &rd_buf);rsel.concurrent(.line, streamLineTask, .{ reader, &line.writer }) catch {- _ = reader.streamDelimiterEnding(&line.writer, '\n') catch |e| {- if (saw_done and readErrIsClose(e)) break :stream; // #134/#135: terminal event already seen — a post-completion close/reset is clean- if (got_body and readErrIsClose(e)) { noteDropped(self); return error.StreamDropped; } // #133: closed before [DONE]/finish_reason — a drop, not a clean end- return e;- }; // no spare concurrency- break :read;+ // #56 Fix-B: pool exhausted — no idle-stall watchdog for this read.+ // A bare blocking streamDelimiterEnding here is the last forever-hang:+ // on a half-open socket it never returns and nothing can trip it. Fail+ // safe with the SAME outcome as a watchdog deadline — if the terminal+ // event already landed the response is complete (clean end); otherwise+ // flush the partial, poison, and end the turn as StreamStalled (never a+ // hang, never a mislabeled StreamDropped or user Esc).+ if (saw_done) break :stream;+ self.flushStreamTail();+ if (!main_mod.json_mode) if (self.out) |o| {+ o.writeAll("\n⚠ stream stalled — ending turn\n") catch {};+ o.flush() catch {};+ };+ if (req.connection) |conn| conn.closing = true;+ return error.StreamStalled;};rsel.concurrent(.stall, streamStallTask, .{ self.io, orig_tio != null }) catch {const r = rsel.await() catch |e| {diff --git a/src/agent_ws.zig b/src/agent_ws.zigindex b847f525..3e34a4e2 100644--- a/src/agent_ws.zig+++ b/src/agent_ws.zig@@ -156,8 +156,16 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {var rd_buf: [2]ReadDone = undefined;var rsel: Io.Select(ReadDone) = .init(self.io, &rd_buf);rsel.concurrent(.msg, wsReadTask, .{ client, gpa, &fbuf }) catch {- _ = client.readMessage(gpa, &fbuf) catch |e| return e; // no spare concurrency- break :read;+ // #56 Fix-B: pool exhausted — mirror the SSE line-read fail-safe. A bare+ // blocking readMessage can hang forever on a half-open ws with no+ // watchdog; end the turn as StreamStalled (never a hang, never a user+ // Esc), exactly as the .deadline arm below does.+ if (!main_mod.json_mode) if (self.out) |o| {+ o.writeAll("\n⚠ stream stalled — ending turn\n") catch {};+ o.flush() catch {};+ };+ if (self.tracer) |tr| tr.note("ws", "stall");+ return error.StreamStalled;};rsel.concurrent(.stall, streamStallTask, .{ self.io, orig_tio != null }) catch {const r = rsel.await() catch |e| {diff --git a/src/http.zig b/src/http.zigindex 1493d0bd..4d2e8602 100644--- a/src/http.zig+++ b/src/http.zig@@ -301,7 +301,7 @@ pub fn postWatched(gpa: Allocator, io: Io, client: *std.http.Client, provider: Pvar done_buf: [2]Done = undefined;var sel: Io.Select(Done) = .init(io, &done_buf);sel.concurrent(.posted, postTask, .{ gpa, client, provider, body }) catch- return post(gpa, client, provider, body); // no concurrency: accept the hang risk+ return error.HungRequest; // #56 Fix-B: pool exhausted — don't fall back to a bare blocking post() that can hang a subagent turn with no Esc path; return retryable so request() retries + backs offsel.concurrent(.watchdog, postWatchdog, .{io}) catch {const r = sel.await() catch |e| { // posted is the only armsel.cancelDiscard();