From 21287b88a9f8d9bbaa83a816d6abbf4909d4ae6b Mon Sep 17 00:00:00 2001 From: lotherk Date: Wed, 18 Nov 2026 23:16:26 +0000 Subject: [PATCH] agent: free the reply of a terminal failure, and make the tests leak-clean ASAN over the unit tests found a real leak the normal gate cannot see: a transport failure that is not retried (the fourth 500, a 4xx, a dead stream) returned -1 with the last reply's body and calls still allocated. The caller only reads the reply on success, so it leaked on every terminal failure -- a long-running daemon with a flapping provider grows by one reply per failure. test_agent's four-500 case leaked exactly the last "boom"; it is clean now. The same run showed the tests leaking their own fixtures: test_anthropic and test_tasks never freed the registry they filled, test_cli forgot host4 and host5, test_confblock forgot its error buffer, test_mcpclient its registries. Fixed; all 39 unit binaries are now clean under ASAN+UBSAN with leak detection. That gate is `make check-asan`: it rebuilds the tree with the sanitizers, runs the unit binaries with LSAN, and rebuilds normally afterwards. Three MCP-plugin teardown allocations are suppressed in tests/asan.supp: its server thread is detached, shutdown kills the child but cannot join the thread, so the line buffer (bounded at RPC_MAX_LINE) and the tools registered by that thread can outlive the host. Bounded, teardown-only, tracked separately. make check: all tests passed. make check-asan: all 39 test binaries clean. --- Makefile | 9 ++++++++ src/backend/agent.c | 3 +++ tests/asan.supp | 11 +++++++++ tests/check_asan.sh | 51 ++++++++++++++++++++++++++++++++++++++++++ tests/test_anthropic.c | 1 + tests/test_cli.c | 2 ++ tests/test_confblock.c | 1 + tests/test_mcpclient.c | 2 ++ tests/test_secrets.c | 1 + 9 files changed, 81 insertions(+) create mode 100644 tests/asan.supp create mode 100755 tests/check_asan.sh diff --git a/Makefile b/Makefile index 3e19c03..34a0c95 100644 --- a/Makefile +++ b/Makefile @@ -320,6 +320,15 @@ check: $(LIB) $(BIN) $(CLIENT) $(PLUGIN) $(TESTS) tests/test_junction echo "== tests/test_mcpsession.sh"; sh tests/test_mcpsession.sh; \ echo "all tests passed" +# The unit tests under ASAN/UBSAN with leak detection. Rebuilds the tree with +# sanitizers and back; run it when nothing else builds in this tree. +check-asan: + @sh tests/check_asan.sh + +# the test binaries as a list, for check_asan.sh +print-tests: + @echo $(TESTS) + # The installed binary finds its own library through an rpath relative to itself # ($ORIGIN), so 'make install PREFIX=$HOME/.local' produces a program that runs # without a wrapper, without LD_LIBRARY_PATH and without an environment file. diff --git a/src/backend/agent.c b/src/backend/agent.c index 95379e9..3eabc5d 100644 --- a/src/backend/agent.c +++ b/src/backend/agent.c @@ -608,6 +608,9 @@ agent_request(struct sp_agent *a, struct sp_conv *conv, struct sp_reply *reply, } if (a->cfg->note != NULL) a->cfg->note(a->cfg->note_ctx, ""); + /* The caller only reads the reply on success; a terminal failure + * kept the body and the calls of the last attempt alive. */ + sp_reply_free(reply); return -1; } } diff --git a/tests/asan.supp b/tests/asan.supp new file mode 100644 index 0000000..759456c --- /dev/null +++ b/tests/asan.supp @@ -0,0 +1,11 @@ +# LeakSanitizer suppressions for `make check-asan`. +# +# The MCP plugin keeps one connection per server in a static table. Its server +# thread is detached and reconnects on its own; shutdown kills the child but +# does not join the thread, so the thread's line buffer (bounded at +# RPC_MAX_LINE) and the tools it registers can still be allocated while the +# host is being torn down. Bounded, only at teardown, and tracked separately; +# everything else must be leak-free. +leak:read_line +leak:register_tools +leak:connect_server diff --git a/tests/check_asan.sh b/tests/check_asan.sh new file mode 100755 index 0000000..0283540 --- /dev/null +++ b/tests/check_asan.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# The unit tests under AddressSanitizer + UndefinedBehaviorSanitizer. +# +# `make check` proves behaviour; this proves the memory the behaviour runs on. +# It found two real defects the normal run cannot see: the agent never freed +# the reply of a terminal transport failure, and the mock transport leaked a +# queued body. Both are fixed; the remaining suppressions are the MCP plugin's +# teardown allocations (tests/asan.supp explains why). +# +# The whole tree is rebuilt with sanitizers and then rebuilt normally, because +# the Makefile builds in place. Run it when the tree is idle. +set -e +cd "$(dirname "$0")/.." + +ACF="-std=c99 -O1 -g -Wall -Wextra -fPIC -D_POSIX_C_SOURCE=200809L \ +-fsanitize=address,undefined -fno-omit-frame-pointer \ +-Iinclude -Isrc/backend -Isrc/frontend -Ivendor/libmcp \ +-Ivendor/netbsd-curses/libcurses" + +TESTS=$(make -s print-tests) +[ -n "$TESTS" ] || { echo "no test binaries to run" >&2; exit 1; } + +make clean >/dev/null +if ! make -j"$(nproc)" CFLAGS="$ACF" all $TESTS > /tmp/check_asan_build.log 2>&1; then + tail -20 /tmp/check_asan_build.log >&2 + echo "asan build failed" >&2 + exit 1 +fi + +failed=0 +for t in $TESTS; do + if ! ASAN_OPTIONS=detect_leaks=1:abort_on_error=1 \ + UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 \ + LSAN_OPTIONS=suppressions=tests/asan.supp \ + LD_LIBRARY_PATH=. "./$t" > /tmp/check_asan_one.log 2>&1; then + echo "FAIL $t" + tail -20 /tmp/check_asan_one.log | sed 's/^/ /' + failed=1 + fi +done + +# leave the tree as it was found: a normal build +make clean >/dev/null +make -j"$(nproc)" >/dev/null + +if [ "$failed" -eq 0 ]; then + echo "check_asan: all $(echo "$TESTS" | wc -w) test binaries clean" +else + echo "check_asan: FAILURES" >&2 +fi +exit "$failed" diff --git a/tests/test_anthropic.c b/tests/test_anthropic.c index 8f81513..0e202e0 100644 --- a/tests/test_anthropic.c +++ b/tests/test_anthropic.c @@ -292,6 +292,7 @@ main(void) sp_buf_free(&err); sp_buf_free(&delta); sp_conv_free(&conv); + sp_registry_free(®); if (failures == 0) { printf("test_anthropic: ok\n"); return 0; diff --git a/tests/test_cli.c b/tests/test_cli.c index 73a8c7e..4d8c76d 100644 --- a/tests/test_cli.c +++ b/tests/test_cli.c @@ -425,6 +425,8 @@ main(void) (void)system(cmd); } + sp_host_free(host5); + sp_host_free(host4); sp_host_free(host3); sp_host_free(host2); sp_host_free(host); diff --git a/tests/test_confblock.c b/tests/test_confblock.c index 4381f3c..adf8dc0 100644 --- a/tests/test_confblock.c +++ b/tests/test_confblock.c @@ -223,6 +223,7 @@ main(void) CHECK(load("plugin p { v = \"a|b\" }\n", c, &err) != 0); free(c); + sp_buf_free(&err); if (failures == 0) puts("test_confblock: ok"); return failures != 0; diff --git a/tests/test_mcpclient.c b/tests/test_mcpclient.c index 388e6eb..8b03096 100644 --- a/tests/test_mcpclient.c +++ b/tests/test_mcpclient.c @@ -393,6 +393,7 @@ main(void) sp_buf_free(&out2); } sp_host_free(host2); + sp_registry_free(®2); } sp_buf_free(&err2); (void)kill(pid, SIGTERM); @@ -400,6 +401,7 @@ main(void) } } + sp_registry_free(®); if (failures == 0) printf("test_mcpclient: ok\n"); return failures == 0 ? 0 : 1; diff --git a/tests/test_secrets.c b/tests/test_secrets.c index 2b3bf2e..e402aa0 100644 --- a/tests/test_secrets.c +++ b/tests/test_secrets.c @@ -313,6 +313,7 @@ test_tasks(void) sp_host_free(host); sp_buf_free(&out); sp_buf_free(&err); + sp_registry_free(&r); } int