From 3ad38869ae461ace274eef173726ef69d9602bff Mon Sep 17 00:00:00 2001 From: Hermes Date: Thu, 19 Nov 2026 08:43:46 +0000 Subject: [PATCH] http: a client that goes away is not an upstream failure When a streaming client closed the connection, the heartbeat write in wait_readable() failed and the read path reported it as a poll/read error. The router then counted it against the endpoint and put the whole pool into cooldown: one cancelled stream (a splinter session stopped after a malformed tool call) cooled four endpoints, and the next request answered "no endpoint available ... tried: cooling cooling cooling cooling". wait_readable() now returns -2 for "the progress hook could not write to the client", the two buffer readers report it as client_gone, and the request handler maps that to the existing abort path (rc=-2) instead of the failure path. The endpoint stays available: the operator closed the stream, the model did nothing wrong. Reproduced with a stub that answers after 25 s while the client leaves after 1 s: before, the endpoint was cooled and the follow-up request had no endpoint; now the log says aborted ... error=client_gone, offline_s=0 and the follow-up request is served. tests/run.sh: PASS. --- src/http.c | 8 +++++++- src/junction.c | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/http.c b/src/http.c index 87e1ad2..b929e1c 100644 --- a/src/http.c +++ b/src/http.c @@ -85,7 +85,11 @@ static int wait_readable(j_conn *c, double idle_s, double deadline) int rc = poll(&p, 1, (int)(t * 1000.0) + 1); if (rc < 0) { if (errno == EINTR) continue; return -1; } if (rc == 0) { - if (g_progress && g_progress(c->role ? c->role : "upstream", t) != 0) return -1; + /* The progress hook writes a heartbeat to the client. If that write + * fails, the client is gone -- that is not an upstream fault and + * must not cool the endpoint. -2 says so; -1 stays "poll failed". */ + if (g_progress && g_progress(c->role ? c->role : "upstream", t) != 0) + return -2; continue; /* keep waiting until the deadline */ } return 1; @@ -114,6 +118,7 @@ ssize_t j_buf_read_until(j_conn *c, j_buf *b, const char *needle, size_t max, } if (b->len > max) { snprintf(err, errlen, "header_block_too_large"); return -1; } int w = wait_readable(c, idle_s, deadline); + if (w == -2) { snprintf(err, errlen, "client_gone"); return -1; } if (w < 0) { snprintf(err, errlen, "poll: %s", strerror(errno)); return -1; } if (w == 0) { snprintf(err, errlen, "timeout_waiting_headers"); return -2; } if (j_buf_reserve(b, b->len + J_READ_CHUNK + 1)) { snprintf(err, errlen, "oom"); return -1; } @@ -130,6 +135,7 @@ int j_buf_ensure(j_conn *c, j_buf *b, size_t from, size_t n, double idle_s, { while (b->len - from < n) { int w = wait_readable(c, idle_s, deadline); + if (w == -2) { snprintf(err, errlen, "client_gone"); return -1; } if (w < 0) { snprintf(err, errlen, "poll: %s", strerror(errno)); return -1; } if (w == 0) { snprintf(err, errlen, "timeout_waiting_body"); return -2; } if (j_buf_reserve(b, b->len + J_READ_CHUNK + 1)) { snprintf(err, errlen, "oom"); return -1; } diff --git a/src/junction.c b/src/junction.c index cf64144..7ebe738 100644 --- a/src/junction.c +++ b/src/junction.c @@ -4783,6 +4783,13 @@ static void handle_client_inner(j_conn *client) !strncmp(ep->base, "https://", 8), may_cache ? "miss" : "off"); goto done; } + /* + * The heartbeat to the client failed: the operator closed the stream. + * The endpoint did nothing wrong, so an abort must not cool it (one + * cancelled client used to cool the whole pool for the next request). + */ + if (rc == -1 && strcmp(err, "client_gone") == 0) + rc = -2; if (rc == -2) { ep->fail_count++;