From 44f457b68dca89792641de3064cbac1718acdca6 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 11:12:04 +0000 Subject: [PATCH] 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. --- src/backend/a2aserver.c | 9 +++++++++ src/backend/mcpserver.c | 4 ++++ src/backend/openai_server.c | 2 ++ src/backend/swarm.c | 3 +++ tests/test_a2a_server.sh | 39 +++++++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+) diff --git a/src/backend/a2aserver.c b/src/backend/a2aserver.c index 0653d5e..f0f320e 100644 --- a/src/backend/a2aserver.c +++ b/src/backend/a2aserver.c @@ -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 diff --git a/src/backend/mcpserver.c b/src/backend/mcpserver.c index 2e0764f..c483075 100644 --- a/src/backend/mcpserver.c +++ b/src/backend/mcpserver.c @@ -29,6 +29,7 @@ */ #include #include +#include #include #include #include @@ -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 diff --git a/src/backend/openai_server.c b/src/backend/openai_server.c index 0f46420..4449c7d 100644 --- a/src/backend/openai_server.c +++ b/src/backend/openai_server.c @@ -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; diff --git a/src/backend/swarm.c b/src/backend/swarm.c index 06e6171..4e76420 100644 --- a/src/backend/swarm.c +++ b/src/backend/swarm.c @@ -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; diff --git a/tests/test_a2a_server.sh b/tests/test_a2a_server.sh index 92d884c..75f9fe6 100755 --- a/tests/test_a2a_server.sh +++ b/tests/test_a2a_server.sh @@ -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" </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