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.
This commit is contained in:
lotherk committed 2026-11-18 23:16:26 +00:00
1 parent 4cbd57d829
commit 21287b88a9
9 files changed
+81

No files matched your search

+9
View File
@@ -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.
+3
View File
@@ -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;
}
}
+11
View File
@@ -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
+51
View File
@@ -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"
+1
View File
@@ -292,6 +292,7 @@ main(void)
sp_buf_free(&err);
sp_buf_free(&delta);
sp_conv_free(&conv);
sp_registry_free(&reg);
if (failures == 0) {
printf("test_anthropic: ok\n");
return 0;
+2
View File
@@ -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);
+1
View File
@@ -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;
+2
View File
@@ -393,6 +393,7 @@ main(void)
sp_buf_free(&out2);
}
sp_host_free(host2);
sp_registry_free(&reg2);
}
sp_buf_free(&err2);
(void)kill(pid, SIGTERM);
@@ -400,6 +401,7 @@ main(void)
}
}
sp_registry_free(&reg);
if (failures == 0)
printf("test_mcpclient: ok\n");
return failures == 0 ? 0 : 1;
+1
View File
@@ -313,6 +313,7 @@ test_tasks(void)
sp_host_free(host);
sp_buf_free(&out);
sp_buf_free(&err);
sp_registry_free(&r);
}
int