From 0b643ca6d92f0a3d6ff2df5ff350617a68a23918 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 01:11:56 +0000 Subject: [PATCH] servers: a silent client no longer holds the gateway or A2A server Both HTTP servers answer one connection at a time. A client that sent half a header and then went quiet held the whole gateway: a second client's complete request was never answered (measured: recv timed out after 10s, with the lazy socket open). The A2A server had the same shape with a five minute read timeout, so one idle socket blocked every other request for five minutes. Both now bound the read (gateway 60s, A2A 120s) and take the value from SPLINTER_GATEWAY_READ_TIMEOUT / SPLINTER_A2A_READ_TIMEOUT so a test does not wait a minute. Tests: tests/slow_client.py and tests/slow_a2a_client.py open a socket, send half a request, wait past the timeout, then send a complete one and require a 200. Without the fix both print "the server is still held" and the suite is red; with it, test_gateway_openai: ok and test_a2a_server: ok. Full suite: all tests passed. check_asan: all 45 test binaries clean. --- README.md | 8 +++++++ docs/GATEWAY.md | 4 +++- src/backend/a2aserver.c | 12 +++++++++- src/backend/openai_server.c | 15 ++++++++++++ tests/slow_a2a_client.py | 44 ++++++++++++++++++++++++++++++++++++ tests/slow_client.py | 42 ++++++++++++++++++++++++++++++++++ tests/test_a2a_server.sh | 20 ++++++++++++++++ tests/test_gateway_openai.sh | 22 ++++++++++++++++++ 8 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 tests/slow_a2a_client.py create mode 100644 tests/slow_client.py diff --git a/README.md b/README.md index 9a4304b..377a98b 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,10 @@ between a local `read` and a caller's `read`. Beyond loopback a `SPLINTER_GATEWAY_TOKEN` is required, and local mutating tools need `--autonomous`. `docs/GATEWAY.md` has the details and examples. +The gateway answers one request at a time, so a client that connects and sends +half a request used to hold it for everyone; an unanswered read now ends after +60 seconds (`SPLINTER_GATEWAY_READ_TIMEOUT`). + ## Plugins and themes A plugin is a `.so` against `libsplinter.so`, exporting @@ -343,6 +347,10 @@ parts are fetched from loopback or a host in `SPLINTER_A2A_URL_ALLOW`. server answers one request at a time, so there is no progress to watch. Beyond loopback the bearer token is required; there is no TLS, so put it behind ssh or a tunnel. +One request at a time means a client that opens a socket and sends nothing +holds everyone else: the server gives up on an unanswered read after 120 +seconds (`SPLINTER_A2A_READ_TIMEOUT`). + ## Configuration `splinter` alone starts the chat: endpoint, model, plugins, theme and session diff --git a/docs/GATEWAY.md b/docs/GATEWAY.md index fb80e09..240e112 100644 --- a/docs/GATEWAY.md +++ b/docs/GATEWAY.md @@ -109,7 +109,9 @@ A caller tool comes back instead: - There is no TLS. Expose it through ssh, WireGuard or a TLS proxy when the network is not yours. - One request is served at a time; the endpoint is a gateway, not a load - balanced service. + balanced service. That makes a silent client everyone's problem: an + unanswered read ends after 60 seconds + (`SPLINTER_GATEWAY_READ_TIMEOUT`), and the next connection is then served. ## Where the tools come from diff --git a/src/backend/a2aserver.c b/src/backend/a2aserver.c index ba1bd47..8daf6e8 100644 --- a/src/backend/a2aserver.c +++ b/src/backend/a2aserver.c @@ -1975,8 +1975,18 @@ sp_a2a_serve(struct sp_agent *ag, const char *ip, int port, struct sp_buf *err) break; } { - struct timeval tv = { 300, 0 }; + /* + * One connection at a time, so an unanswered read + * holds every other client: 5 minutes was a client + * that opened a socket and went quiet. The duration + * covers a whole request, body included, and a test + * can shorten it. + */ + const char *tmo = getenv("SPLINTER_A2A_READ_TIMEOUT"); + struct timeval tv = { 120, 0 }; + if (tmo != NULL && atoi(tmo) > 0) + tv.tv_sec = atoi(tmo); (void)setsockopt(c, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); } diff --git a/src/backend/openai_server.c b/src/backend/openai_server.c index 3de7eab..0f46420 100644 --- a/src/backend/openai_server.c +++ b/src/backend/openai_server.c @@ -1305,9 +1305,24 @@ sp_openai_serve(const struct sp_gateway_opts *o, struct sp_buf *err) (void)fcntl(fd, F_SETFD, FD_CLOEXEC); { int one_tcp = 1; + /* + * One connection at a time: a client that sends half + * a header and then goes quiet used to hold the whole + * gateway, so no other request was answered at all. + * This bounds the read of the request; the turn that + * follows is written to the client, not read from it. + * SPLINTER_GATEWAY_READ_TIMEOUT lets a test prove it + * without waiting a minute. + */ + const char *tmo = getenv("SPLINTER_GATEWAY_READ_TIMEOUT"); + struct timeval tv = { 60, 0 }; + if (tmo != NULL && atoi(tmo) > 0) + tv.tv_sec = atoi(tmo); (void)setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one_tcp, sizeof(one_tcp)); + (void)setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, + sizeof(tv)); } serve_one(fd, o, token); close(fd); diff --git a/tests/slow_a2a_client.py b/tests/slow_a2a_client.py new file mode 100644 index 0000000..5d57dce --- /dev/null +++ b/tests/slow_a2a_client.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +"""splinter -- prove the A2A server gives up on a client that goes quiet. + +Same shape as slow_client.py, for the A2A server: it serves one connection at +a time, so a client that opens a socket and sends nothing must not hold the +next request. Before the read timeout was bounded, that client held the server +for five minutes. + +argv: port, path. Exit 0 when the second client got a reply. +""" +import socket +import sys +import time + +port = int(sys.argv[1]) +path = sys.argv[2] if len(sys.argv) > 2 else "/a2a" + +lazy = socket.create_connection(("127.0.0.1", port), timeout=10) +lazy.sendall(b"POST " + path.encode() + b" HTTP/1.1\r\nHost: x\r\nContent-Le") +# the server's read timeout is 2s here; wait past it +time.sleep(3.0) + +body = (b'{"jsonrpc":"2.0","id":"1","method":"SendMessage","params":{"message":' + b'{"messageId":"m","role":"ROLE_USER","parts":[{"text":"hallo"}]}}}') +good = socket.create_connection(("127.0.0.1", port), timeout=15) +good.sendall( + b"POST " + path.encode() + b" HTTP/1.1\r\nHost: x\r\n" + b"Connection: close\r\nContent-Type: application/json\r\n" + b"Content-Length: %d\r\n\r\n" % len(body) + body) +good.settimeout(15) +try: + data = good.recv(400) +except socket.timeout: + print("second client got nothing: the server is still held") + sys.exit(1) +finally: + lazy.close() + good.close() + +if data.startswith(b"HTTP/1.1 200"): + print("second client answered") + sys.exit(0) +print("second client got %r" % data[:80]) +sys.exit(1) diff --git a/tests/slow_client.py b/tests/slow_client.py new file mode 100644 index 0000000..530e979 --- /dev/null +++ b/tests/slow_client.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +"""splinter -- prove the gateway gives up on a client that goes quiet. + +The accept loop serves one connection at a time. Without a read timeout a +client that sends half a header and then nothing held the whole gateway: the +second connection below was accepted by the kernel and never answered. + +argv: port. Exit 0 when the second client got a reply. +""" +import socket +import sys +import time + +port = int(sys.argv[1]) + +lazy = socket.create_connection(("127.0.0.1", port), timeout=10) +lazy.sendall(b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Le") +# the server's read timeout is 2s here; wait past it so the lazy client is +# dropped before the second one is queued +time.sleep(3.0) + +good = socket.create_connection(("127.0.0.1", port), timeout=10) +body = b'{"model":"x","messages":[{"role":"user","content":"hi"}]}' +good.sendall( + b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nConnection: close\r\n" + b"Content-Type: application/json\r\nContent-Length: %d\r\n\r\n" % len(body) + + body) +good.settimeout(10) +try: + data = good.recv(400) +except socket.timeout: + print("second client got nothing: the gateway is still held") + sys.exit(1) +finally: + lazy.close() + good.close() + +if data.startswith(b"HTTP/1.1 200"): + print("second client answered") + sys.exit(0) +print("second client got %r" % data[:80]) +sys.exit(1) diff --git a/tests/test_a2a_server.sh b/tests/test_a2a_server.sh index beb9f98..cf77bc8 100755 --- a/tests/test_a2a_server.sh +++ b/tests/test_a2a_server.sh @@ -12,12 +12,15 @@ SPLINTER_SESSION_DIR="$D/home/.splinter"; export SPLINTER_SESSION_DIR P=$(free_port) P2=$((P + 1)) P3=$((P + 2)) +P4=$((P + 3)) S1= S2= +S3= W= cleanup() { [ -n "$S1" ] && kill "$S1" 2>/dev/null [ -n "$S2" ] && kill "$S2" 2>/dev/null + [ -n "$S3" ] && kill "$S3" 2>/dev/null [ -n "$W" ] && kill "$W" 2>/dev/null rm -rf "$D" } @@ -244,5 +247,22 @@ resp5=$(post "$P2" "$(send_json)" -H 'Authorization: Bearer tok') echo "$resp5" | grep -q 'hallo von splinter' check test $? -eq 0 +# ---- a client that goes quiet must not hold the server ---- +# The accept loop serves one connection at a time. With a five minute read +# timeout, one socket that opened and said nothing blocked every other client +# for five minutes. +HOME="$D/home" SPLINTER_A2A_READ_TIMEOUT=2 ./splinter --no-config \ + --script "$D/model.txt" --a2a-server-listen 127.0.0.1 \ + --a2a-listen-port "$P4" >"$D/out4" 2>"$D/err4" & +S3=$! +check wait_port "$P4" +python3 -I tests/slow_a2a_client.py "$P4" /a2a > "$D/slow.txt" 2>&1 +check test $? -eq 0 +if ! grep -q 'second client answered' "$D/slow.txt"; then + echo "FAIL a quiet client held the A2A server" + cat "$D/slow.txt" + fails=$((fails + 1)) +fi + if [ "$fails" -eq 0 ]; then echo "test_a2a_server: ok"; exit 0; fi exit 1 diff --git a/tests/test_gateway_openai.sh b/tests/test_gateway_openai.sh index 29691a7..70db2e1 100755 --- a/tests/test_gateway_openai.sh +++ b/tests/test_gateway_openai.sh @@ -16,16 +16,19 @@ P1=$((P0 + 70)) P2=$((P0 + 71)) P3=$((P0 + 72)) P4=$((P0 + 73)) +P5=$((P0 + 74)) F= G1= G2= G3= +G4= fails=0 cleanup() { [ -n "$F" ] && kill "$F" 2>/dev/null [ -n "$G1" ] && kill "$G1" 2>/dev/null [ -n "$G2" ] && kill "$G2" 2>/dev/null [ -n "$G3" ] && kill "$G3" 2>/dev/null + [ -n "$G4" ] && kill "$G4" 2>/dev/null rm -rf "$D" } trap cleanup EXIT @@ -184,6 +187,25 @@ check test "$(curl -s -o /dev/null -w '%{http_code}' -X POST \ "http://127.0.0.1:$P1/v1/chat/completions" -H 'Content-Type: application/json' \ -d '{"model":"x","messages":[{"role":"user","content":"hi"}],"tools":[{"type":"function","function":{"name":"gw_read","parameters":{"type":"object"}}}]}')" = 400 +# a client that sends half a header and then goes quiet must not hold the +# gateway. Before the read timeout it did: the accept loop is one connection +# at a time, so a second client waited forever and got no answer at all. +HOME=$D/h5 SPLINTER_LOCALE=en SPLINTER_GATEWAY_READ_TIMEOUT=2 ./splinter \ + --no-config --endpoint "http://127.0.0.1:$PF/v1" --model up \ + --openai-listen 127.0.0.1 --openai-listen-port "$P5" \ + > "$D/g5.log" 2>&1 & +G4=$! +if ! wait_port "$P5" 40; then + echo "FAIL the timeout gateway did not start" + cat "$D/g5.log" + fails=$((fails + 1)) +else + mkdir -p "$D/h5" + python3 -I tests/slow_client.py "$P5" > "$D/slow.txt" 2>&1 + check test $? -eq 0 + checkgrep 'second client answered' "$D/slow.txt" +fi + # beyond loopback there is no gateway without a token HOME=$D/h4 SPLINTER_LOCALE=en timeout 10 ./splinter --no-config \ --endpoint "http://127.0.0.1:$PF/v1" --model up \