Files
splinter/tests/plugin_lifecycle.c
lotherk c11a6c7989 mcp: join the server threads and give the tools an owner
The MCP plugin's reconnect thread was detached: shutdown killed the child and
returned, so the thread could still be reading into its line buffer (up to
16 MiB) and could call sp_host_add_tool() into a host that was being freed -- a
use-after-free waiting to happen, and the reason three leaks were suppressed in
tests/asan.supp (read_line, register_tools, connect_server).

Each server now keeps its pthread_t, init creates it joinable, and shutdown
waits (bounded, 5 s) for every thread to return before joining it and freeing
its state. teardown() frees the line buffer for the HTTP transport too (it was
freed only for stdio) and closes the stdio pipes even when the child was
already reaped.

The registered tools leaked for a second reason: sp_registry_add copies the
struct, not the strings, and sp_registry_free freed only the arrays -- the
prefixed name, the description, the printed schema and the toolref of every MCP
tool were lost with the registry. struct sp_tool gets a dispose hook; the
registry calls it for each tool, and the MCP plugin frees what it allocated (the
default schema is strdup'd now, so ownership is uniform). Builtins and the
gateway's tools leave it NULL; every positional initializer got the new field.

tests/asan.supp is empty. make check-asan: all 39 test binaries clean.
make check: all tests passed.
2026-11-19 00:05:05 +00:00

65 lines
1.3 KiB
C

/*
* A plugin for tests/test_lifecycle: registers a tool and a ticking widget,
* then fails its init when SP_TEST_INIT_FAIL is set. Its shutdown() writes
* SP_TEST_SHUTDOWN_FILE, so the test can see that it ran.
*/
#include <stdio.h>
#include <stdlib.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int
lc_tool(const char *args, struct sp_buf *out, void *ctx)
{
(void)args;
(void)ctx;
return sp_buf_puts(out, "lifecycle");
}
static int
lc_tick(struct sp_host *h, const char *name, void *ctx)
{
(void)ctx;
return sp_host_widget_text(h, name, "tick");
}
static const struct sp_tool tool = {
"lifecycle_tool", "test", "{\"type\":\"object\"}", SP_AUTH_READ, lc_tool,
NULL,
NULL
};
static const struct sp_widget widget = { "lifecycle", lc_tick, NULL, NULL, 50 };
static int
lc_init(struct sp_host *h)
{
if (sp_host_add_tool(h, &tool) != 0 || sp_host_add_widget(h, &widget) != 0)
return -1;
return getenv("SP_TEST_INIT_FAIL") != NULL ? -1 : 0;
}
static void
lc_shutdown(struct sp_host *h)
{
const char *path = getenv("SP_TEST_SHUTDOWN_FILE");
FILE *f;
(void)h;
if (path != NULL && (f = fopen(path, "w")) != NULL) {
fputs("shut down\n", f);
fclose(f);
}
}
static const struct sp_plugin plugin = {
SP_PLUGIN_ABI, "lifecycle", "1.0.0", lc_init, NULL, lc_shutdown
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &plugin;
}