Files
splinter/tests/test_swarm_socket.sh
T
lotherk 00bedb4e32 swarm: the socket exists only with --swarm, and only while a member runs
Two things were wrong with .splinter-swarm.sock. Every chat bound it, wanted
or not, so a shared working directory collected a leftover from every session
and the next start that found one sat on "[swarm locked]" because nobody
answered on it. And the master removed the file on its way out even when a
member was still running, which left that member on a path that was gone.

Now the fleet is opt-in. Without --swarm nothing binds or joins the file;
with it, the file lives exactly as long as the fleet does, whatever order the
members leave in.

- main.c: --swarm is a flag; eo.swarm requires it (plus --chat, no --serve,
  no --daemon, no A2A/MCP/delegate).
- swarm.c: join_or_bind() is the one place that decides master or slave, used
  at startup and again on takeover. A slave whose master says "bye" or whose
  connection just ends (SIGKILL) takes the fleet over: bind, or join whoever
  was faster. A leftover file with no listener is removed there and rebound,
  because a refused connect is proof nobody answers.
- master_close() removes the file only when it is the last member. The peer
  check reads the sockets instead of trusting the cached flag: a peer that
  exited is only visible as EOF on read.
- swarm_emergency_unlink() (SIGTERM/SIGHUP) keeps the file while a peer is
  live. A handler may not walk the peer table, so the master keeps a
  sig_atomic_t count of live peers for it.

Tests, all against the real binary and the real signal path:
- test_swarm.c: master leaves first, survivor takes over and the file stays;
  hard-killed master (EOF, no bye), survivor cleans the leftover up; the
  signal cleanup alone leaves the file for a live member; an unowned file is
  still never deleted. Mutation-checked: forcing master_close to always
  unlink, and dropping the peer count from swarm_emergency_unlink, each turn
  the suite red.
- test_swarm_socket.sh (new, in make check): through tmux, --chat without
  --swarm creates no file at all; --chat --swarm creates one that accepts a
  member and is gone after Ctrl-D and after SIGTERM; two members share one
  file that outlives the master and goes with the last one; a SIGTERM-killed
  master does not take the fleet from the survivor. Mutation-checked: making
  the fleet unconditional (the old eo.swarm line) turns it red.

make check: all tests passed. check_asan.sh: all 46 test binaries clean.
2026-11-23 20:19:33 +00:00

172 lines
5.7 KiB
Bash
Executable File

#!/bin/sh
#
# splinter -- the fleet socket exists only while a fleet does.
#
# What it guards: every chat used to bind .splinter-swarm.sock, whether the
# operator wanted a fleet or not. The file then sat in a shared working
# directory after the session ended, and a later start that found a stale
# path stayed on "[swarm locked]" because nobody answered on it. Checked here
# against the real binary, through tmux because --chat needs a terminal:
#
# 1. --chat without --swarm creates no socket file, at all.
# 2. --chat --swarm creates one, it answers a second member, and it is gone
# again when the session ends -- by Ctrl-D and by SIGTERM, the path that
# never reaches sp_engine_close.
# 3. two --swarm sessions share one file; it goes when the last one leaves,
# not when the master does.
set -u
cd "$(dirname "$0")/.." || exit 1
ROOT=$(pwd)
. "$(dirname "$0")/sh/port.sh"
if ! command -v tmux >/dev/null 2>&1; then
echo "test_swarm_socket: skipped (no tmux)"
exit 0
fi
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-swsock-XXXXXX") || exit 1
SPLINTER_SESSION_DIR="$D/home/.splinter"
export SPLINTER_SESSION_DIR
# Our own tmux server, never the user's: $TMUX wins over $TMUX_TMPDIR, so a run
# started inside the user's tmux would drive -- and kill -- that server.
unset TMUX TMUX_PANE
export TMUX_TMPDIR="$D"
own_server() {
if [ -n "${TMUX:-}" ] || [ "${TMUX_TMPDIR:-}" != "$D" ]; then
echo "test_swarm_socket: refusing to touch a tmux server this test does not own" >&2
exit 1
fi
}
killserver() { own_server; tmux kill-server 2>/dev/null; }
F=
fails=0
cleanup() {
killserver
[ -n "$F" ] && kill "$F" 2>/dev/null
rm -rf "$D"
}
trap cleanup EXIT
check() { d=$1; shift; if "$@"; then :; else echo "FAIL $d"; fails=$((fails + 1)); fi; }
has_sock() { [ -S "$1/.splinter-swarm.sock" ]; }
no_sock() { ! has_sock "$1"; }
mkdir -p "$D/home"
P=$(free_port)
python3 -I tests/fake_openai_echo.py "$P" >"$D/fake.out" 2>&1 &
F=$!
wait_listen "$P" "$F" || { echo "FAIL fake did not start"; cat "$D/fake.out"; exit 1; }
start_chat() { # start_chat <session> <dir> [--swarm]
S=$1
dir=$2
shift 2
mkdir -p "$dir"
# exec: the pane pid is splinter itself, not a shell in front of it -- the
# SIGTERM case below has to hit the process that owns the socket
tmux new-session -d -s "$S" -x 100 -y 30 \
"cd $dir && HOME=$D/home SPLINTER_LOCALE=en exec $ROOT/splinter \
--no-config --endpoint http://127.0.0.1:$P/v1/chat/completions \
--model m --chat $*"
}
wait_sock() { # wait_sock <dir>: up to 5s for the path to appear
i=0
while [ $i -lt 20 ] && ! has_sock "$1"; do
sleep 0.25
i=$((i + 1))
done
has_sock "$1"
}
wait_gone() { # wait_gone <dir>: up to 5s for the path to disappear
i=0
while [ $i -lt 20 ] && has_sock "$1"; do
sleep 0.25
i=$((i + 1))
done
no_sock "$1"
}
end_chat() { # end_chat <session>: Ctrl-D, and the session is gone when it exits
tmux send-keys -t "$1" C-u C-d 2>/dev/null
i=0
while [ $i -lt 40 ] && tmux has-session -t "$1" 2>/dev/null; do
sleep 0.25
i=$((i + 1))
done
tmux kill-session -t "$1" 2>/dev/null
}
joined() { # joined <dir>: a second member can connect and say hello
python3 - "$1/.splinter-swarm.sock" <<'PY'
import socket, sys
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.settimeout(5)
s.connect(sys.argv[1])
s.sendall(b'{"t":"hello","pid":1,"busy":0}\n')
s.recv(4096) # the seat list, or at least a close: both mean a live master
s.close()
PY
}
# 1. without --swarm: no socket, not while it runs and not after it ends
A="$D/plain"
start_chat plain "$A"
sleep 2
check "a plain --chat created .splinter-swarm.sock" no_sock "$A"
end_chat plain
check "a plain --chat left .splinter-swarm.sock behind" no_sock "$A"
# 2. with --swarm: the file exists while the session runs ...
B="$D/fleet"
start_chat fleet "$B" --swarm
if check "--swarm did not create the socket" wait_sock "$B"; then
# ... and it answers: a second member joins and gets an answer
check "the socket did not accept a member" joined "$B"
fi
# ... and it is gone when the session ends, here by Ctrl-D
end_chat fleet
check "Ctrl-D left .splinter-swarm.sock behind" wait_gone "$B"
# ... and by SIGTERM, which never reaches sp_engine_close()
start_chat fleet2 "$B" --swarm
check "--swarm did not create the socket again" wait_sock "$B"
pid=$(tmux display-message -p -t fleet2 '#{pane_pid}')
kill -TERM "$pid" 2>/dev/null
check "SIGTERM left .splinter-swarm.sock behind" wait_gone "$B"
tmux kill-session -t fleet2 2>/dev/null
# 3. two members, one file: it goes when the last one leaves
C="$D/shared"
start_chat m1 "$C" --swarm
check "the first member created no socket" wait_sock "$C"
start_chat m2 "$C" --swarm
sleep 2
check "the second member removed the live socket" has_sock "$C"
end_chat m1
sleep 1
check "the master's exit removed a socket another member still needs" \
has_sock "$C"
end_chat m2
check "the last member left .splinter-swarm.sock behind" wait_gone "$C"
# 4. the master is killed while a member runs: the fleet has to survive it.
# The signal handler removes the file (it cannot know about the peer), so
# the survivor's takeover is what has to bring the fleet back.
K="$D/killed"
start_chat k1 "$K" --swarm
check "the master created no socket" wait_sock "$K"
start_chat k2 "$K" --swarm
sleep 2
check "the second member removed the live socket" has_sock "$K"
pid=$(tmux display-message -p -t k1 '#{pane_pid}')
kill -TERM "$pid" 2>/dev/null
sleep 2
check "a killed master took the fleet away from the survivor" wait_sock "$K"
check "the survivor's fleet does not answer" joined "$K"
end_chat k2
check "the survivor left .splinter-swarm.sock behind" wait_gone "$K"
kill "$F" 2>/dev/null
if [ "$fails" -eq 0 ]; then
echo "test_swarm_socket: ok"
exit 0
fi
echo "test_swarm_socket: $fails failure(s)"
exit 1