server: no tool child inherits a listening socket

The `run` tool forks /bin/sh with the server's descriptors open. A command
that left something in the background ("nohup sleep 300 &") kept the LISTEN
socket of the A2A server alive after splinter exited: the port stayed bound
and the next start died with "Address already in use".

Found by starting a background child from a tool, killing the server, and
watching the port: `ss -ltn` still listed it, the inode matched the server's
listen socket.

The listening and the accepted sockets now carry FD_CLOEXEC in a2aserver,
mcpserver, openai_server and swarm. protocol.c already did this.

Test: tests/test_a2a_server.sh starts a background child from the run tool,
kills the server, and checks that the port is free and that a second start
does not report "Address already in use". Without the fix both checks fail;
with it, and under ASan (46 test binaries), they pass.
This commit is contained in:
lotherk committed 2026-11-23 11:12:04 +00:00
1 parent 15fc06c8bf
commit 44f457b68d
5 files changed
+57

No files matched your search

+9
View File
@@ -1953,6 +1953,14 @@ sp_a2a_serve(struct sp_agent *ag, const char *ip, int port, struct sp_buf *err)
sp_buf_printf(err, "a2a: socket: %s", strerror(errno));
return -1;
}
/*
* No child may inherit the listening socket: the `run` tool forks
* /bin/sh with the server's descriptors, and a command that leaves
* something in the background ("nohup sleep 300 &") kept the port
* bound after splinter exited, so the next start died with "Address
* already in use". The same for the accepted connection below.
*/
(void)fcntl(s, F_SETFD, FD_CLOEXEC);
(void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
@@ -1987,6 +1995,7 @@ sp_a2a_serve(struct sp_agent *ag, const char *ip, int port, struct sp_buf *err)
continue;
break;
}
(void)fcntl(c, F_SETFD, FD_CLOEXEC);
{
/*
* One connection at a time, so an unanswered read
+4
View File
@@ -29,6 +29,7 @@
*/
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <signal.h>
@@ -480,6 +481,8 @@ sp_mcp_serve(struct sp_engine **eng, const struct sp_engine_opts *opts,
sp_buf_printf(err, "mcp: socket: %s", strerror(errno));
return -1;
}
/* a tool child must not inherit the socket: see a2aserver.c */
(void)fcntl(s, F_SETFD, FD_CLOEXEC);
(void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
@@ -525,6 +528,7 @@ sp_mcp_serve(struct sp_engine **eng, const struct sp_engine_opts *opts,
continue;
break;
}
(void)fcntl(c, F_SETFD, FD_CLOEXEC);
{
/*
* One connection at a time, so an idle one must not hold
+2
View File
@@ -1257,6 +1257,8 @@ sp_openai_serve(const struct sp_gateway_opts *o, struct sp_buf *err)
sp_buf_printf(err, "gateway: socket: %s", strerror(errno));
return -1;
}
/* a tool child must not inherit the socket: see a2aserver.c */
(void)fcntl(s, F_SETFD, FD_CLOEXEC);
(void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
+3
View File
@@ -94,6 +94,8 @@ try_become_master(struct swarm *s)
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) return -1;
/* no tool child inherits the socket the master answers on */
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
memset(&a, 0, sizeof(a));
a.sun_family = AF_UNIX;
snprintf(a.sun_path, sizeof(a.sun_path), "%.107s", s->path);
@@ -194,6 +196,7 @@ master_accept(struct swarm *s)
if (s->listen_fd < 0) return;
fd = accept(s->listen_fd, NULL, NULL);
if (fd < 0) return;
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
if (!same_user(fd)) {
close(fd);
return;
+39
View File
@@ -13,14 +13,17 @@ P=$(free_port)
P2=$((P + 1))
P3=$((P + 2))
P4=$((P + 3))
P5=$((P + 4))
S1=
S2=
S3=
S4=
W=
cleanup() {
[ -n "$S1" ] && kill "$S1" 2>/dev/null
[ -n "$S2" ] && kill "$S2" 2>/dev/null
[ -n "$S3" ] && kill "$S3" 2>/dev/null
[ -n "$S4" ] && kill "$S4" 2>/dev/null
[ -n "$W" ] && kill "$W" 2>/dev/null
rm -rf "$D"
}
@@ -275,5 +278,41 @@ if ! grep -q 'second client answered' "$D/slow.txt"; then
fails=$((fails + 1))
fi
# ---- a background process started by a tool must not hold the port ----
# The `run` tool forks /bin/sh; the child inherited the server's descriptors.
# A command that leaves something behind ("sleep 300 &") kept the LISTEN socket
# open after splinter exited, so the port stayed bound and the next start died
# with "Address already in use". The listen socket is created with
# SOCK_CLOEXEC and the accepted one gets FD_CLOEXEC, so no exec inherits them.
# The child writes its own pid down: killing by name ("pkill -f sleep") would
# reach a sleep that belongs to something else on this machine.
cat > "$D/bg.txt" <<MDL
tool run {"command":"nohup sleep 120 >/dev/null 2>&1 & echo \$! > $D/bgpid; echo started"}
answer fertig
MDL
HOME="$D/home" ./splinter --no-config --autonomous --script "$D/bg.txt" \
--a2a-server-listen 127.0.0.1 --a2a-listen-port "$P5" \
>"$D/out5" 2>"$D/err5" &
S4=$!
check wait_port "$P5"
post "$P5" "$(send_json)" > /dev/null
sleep 1
kill "$S4" 2>/dev/null
wait "$S4" 2>/dev/null
S4=
sleep 1
check_that "a tool's background child does not keep the A2A port bound" \
sh -c "! ss -ltn 2>/dev/null | grep -q ':$P5 '"
HOME="$D/home" ./splinter --no-config --script "$D/model.txt" \
--a2a-server-listen 127.0.0.1 --a2a-listen-port "$P5" \
>"$D/out6" 2>"$D/err6" &
S4=$!
sleep 1
check_that "the port can be taken again after a tool left a child behind" \
sh -c "! grep -q 'Address already in use' '$D/err6'"
kill "$S4" 2>/dev/null
S4=
[ -f "$D/bgpid" ] && kill "$(cat "$D/bgpid")" 2>/dev/null
if [ "$fails" -eq 0 ]; then echo "test_a2a_server: ok"; exit 0; fi
exit 1