Files
splinter/tests/test_attach_busy.sh
lotherk a00534cf6c serve: a second client is told why, not left hanging
`--serve` holds one engine, so one client talks to it at a time - that part is
deliberate. What was not: the second client connected into the listen backlog
and then waited for a greeting that could not come until the first one left.
splinterctl printed nothing and hung; --attach gave up 10 s later with "no
greeting from the backend", which names the wrong cause.

The accept is non-blocking now and a client that arrives while the engine is
taken is answered at once: an error event with the reason, then a shutdown of
the write side. That reads on the other end as "another client is attached:
one conversation runs one turn at a time". Descriptors are drained before the
close, or the RST would eat the message the client has not read yet.

Two smaller ones on the same path:
- --attach reconnects on a broken connection; it now carries the server's error
  text through instead of replacing it with "no greeting".
- a client that reads nothing is not waited for forever: a greeting timeout
  turns a silent server into a message instead of a hang.
2026-11-22 10:26:06 +00:00

136 lines
4.8 KiB
Bash
Executable File

#!/bin/sh
#
# splinter -- --serve answers a second client instead of leaving it hanging.
#
# What it guards, as found: the protocol server serves one client at a time by
# design (docs/PROTOCOL.md), but a second `splinterctl` connected, sat in the
# accept() backlog and printed nothing at all - not even an error - until an
# outer `timeout` killed it (rc=124). A silent hang looks like a broken engine.
# Two answers are wanted and this test asks for both:
#
# * the server tells a second client at once that one conversation runs one
# turn at a time, and exits non-zero;
# * the first client keeps its connection and is not disturbed by that;
# * a server that never greets at all (an older build) cannot trap a client
# for longer than the greeting timeout.
#
# The first is the fix, the second is the regression it could cause, the third
# is the safety net for the version of the server the user already runs.
set -e
cd "$(dirname "$0")/.."
. "$(dirname "$0")/sh/port.sh"
SPLINTER=./splinter
CTL=./splinterctl
D=$(mktemp -d)
trap 'kill $SRV 2>/dev/null; rm -rf "$D"' EXIT INT TERM
fail() { echo "FAIL: $*" >&2; exit 1; }
pass() { echo "ok $*"; }
# The server's own default socket lives in $HOME; --serve takes no path.
export HOME="$D/home"
mkdir -p "$HOME/.splinter"
$SPLINTER --no-config --autonomous --serve --endpoint http://127.0.0.1:1/v1 \
--model m >"$D/serve.log" 2>&1 &
SRV=$!
SOCK="$HOME/.splinter/splinter.sock"
i=0
while [ "$i" -lt 100 ]; do
[ -S "$SOCK" ] && break
i=$((i + 1))
sleep 0.1
done
[ -S "$SOCK" ] || { cat "$D/serve.log" >&2; fail "the server did not create $SOCK"; }
# The first client holds the engine for as long as its socket stays open. A
# raw connection is enough: what the server sees is one attached client.
python3 - "$SOCK" <<'EOF' >"$D/c1.log" 2>&1 &
import socket, sys, time
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(sys.argv[1])
s.settimeout(0.5)
end = time.time() + 60
while time.time() < end:
try:
chunk = s.recv(4096)
if not chunk:
break
sys.stdout.write(chunk.decode("utf-8", "replace")); sys.stdout.flush()
except socket.timeout:
pass
EOF
C1=$!
sleep 0.7
grep -q '"ready"' "$D/c1.log" 2>/dev/null ||
fail "the first client never got a greeting: $(cat "$D/c1.log")"
pass "the first client is attached"
# The second client must be sent away with a reason, and quickly.
START=$(date +%s)
set +e
timeout 20 $CTL --socket "$SOCK" --cmd /status >"$D/c2.out" 2>"$D/c2.err"
RC=$?
set -e
TOOK=$(($(date +%s) - START))
[ "$RC" -ne 0 ] || fail "the second client exited 0 with the engine taken"
[ "$RC" -ne 124 ] || fail "the second client hung (rc=124) instead of being refused"
[ "$TOOK" -le 10 ] || fail "the second client took ${TOOK}s to hear 'no'"
grep -qi 'another client is attached' "$D/c2.err" ||
fail "no reason on stderr, got: $(cat "$D/c2.err")"
pass "the second client is refused at once with a reason (${TOOK}s, rc=$RC)"
# Being refused must not close the first client's connection.
kill -0 "$C1" 2>/dev/null || fail "the first client died when the second was refused"
[ -S "$SOCK" ] || fail "the server went away"
pass "the first client and the server survive the refusal"
# A real front end sends its greeting requests at once. The server used to
# close with that input unread, the kernel answered with RST, and the RST ate
# the explanation still sitting in the client's buffer: --attach printed
# "no greeting from the backend" instead of "another client is attached".
set +e
timeout 15 script -qec "$SPLINTER --no-config --attach --socket $SOCK" \
/dev/null >"$D/c4.log" 2>&1
set -e
grep -qi 'another client is attached' "$D/c4.log" ||
fail "--attach lost the reason, got: $(head -3 "$D/c4.log")"
pass "--attach as a second client reports why it cannot attach"
# The safety net: a server that accepts but never speaks must not trap us.
python3 - "$D/mute.sock" <<'EOF' &
import socket, sys, time
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.bind(sys.argv[1]); s.listen(4)
held = []
end = time.time() + 30
while time.time() < end:
try:
s.settimeout(1.0)
c, _ = s.accept()
held.append(c) # keep it open and say nothing
except Exception:
pass
EOF
MUTE=$!
sleep 0.5
START=$(date +%s)
set +e
timeout 30 $CTL --socket "$D/mute.sock" --cmd /status >"$D/c3.out" 2>"$D/c3.err"
RC=$?
set -e
TOOK=$(($(date +%s) - START))
kill $MUTE 2>/dev/null || true
[ "$RC" -ne 124 ] || fail "a mute server trapped the client past 30 s"
[ "$RC" -ne 0 ] || fail "a mute server made the client exit 0"
[ "$TOOK" -le 12 ] || fail "the greeting timeout let the client wait ${TOOK}s"
grep -qi 'no answer' "$D/c3.err" || fail "a mute server gave no message: $(cat "$D/c3.err")"
pass "a mute server ends the client in ${TOOK}s with a message"
echo "test_attach_busy: ok"