Skip to content

Commit e45d705

Browse files
DeusDataclaude
andcommitted
fix(mcp): free the extraction bitset cache on the in-process auto-index thread (LSan)
cbm_kind_in_set lazily builds a THREAD-LOCAL node-type bitset cache during extraction. That cache must be freed before a worker thread exits, or the calloc'd bitsets are orphaned when the thread's TLS is torn down and LeakSanitizer reports them at process exit. Parallel workers already free it (pass_parallel.c:993) and the main thread frees it at process teardown; the IN-PROCESS sequential auto-index runs extraction on autoindex_thread — a short-lived pthread joined by cbm_mcp_server_free — and was the one gap: it built the cache but never freed it (360 bytes in 9 allocations leaked, all from ks_build). This is a PRE-EXISTING latent leak: the extraction path and autoindex_thread are byte-identical to main. It is newly EXPOSED by DeusData#713's new autoindex_limit_admits_non_git_root_under_limit test, the first LSan-observed path that runs an in-process auto-index and joins the worker thread in-process — so the fix ships with the test that exposes it. Free the calling thread's bitset cache after cbm_pipeline_free(p), mirroring the established per-worker-thread teardown. Zero behavior change to the CLI/main path. macOS LSan lane, mcp suite: 360 B / 9 allocations -> 0. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 7e73b48 commit e45d705

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17383,6 +17383,16 @@ static void register_watcher_if_enabled(cbm_mcp_server_t *srv) {
1738317383
}
1738417384

1738517385
/* Background auto-index thread function */
17386+
/* Extraction builds a THREAD-LOCAL node-type bitset cache (cbm_kind_in_set).
17387+
* Every worker thread that runs extraction must free that cache before it exits,
17388+
* or the calloc'd bitsets are orphaned when the thread's TLS is torn down and
17389+
* LeakSanitizer reports them at process exit. Parallel workers do this in
17390+
* pass_parallel.c; the in-process (sequential) auto-index runs extraction on
17391+
* THIS short-lived thread, so it must free its own cache too. Declared extern
17392+
* (not via internal/cbm/helpers.h) to avoid pulling the extraction layer's
17393+
* header into the MCP TU — the same pattern test_main.c uses for teardown. */
17394+
extern void cbm_kind_in_set_free_cache(void);
17395+
1738617396
static void *autoindex_thread(void *arg) {
1738717397
cbm_mcp_server_t *srv = (cbm_mcp_server_t *)arg;
1738817398

@@ -17422,6 +17432,7 @@ static void *autoindex_thread(void *arg) {
1742217432
cbm_pipeline_unlock();
1742317433

1742417434
cbm_pipeline_free(p);
17435+
cbm_kind_in_set_free_cache(); /* free THIS thread's extraction bitset cache (see above) */
1742517436
cbm_mem_collect(); /* return mimalloc pages to OS after indexing (in-process only) */
1742617437

1742717438
if (rc == 0) {

0 commit comments

Comments
 (0)