Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(windows): release thread heaps from a TLS detach callback

Martin Vogel788769e20acf
diff --git a/src/foundation/compat_thread.c b/src/foundation/compat_thread.c
index 1c0b8265a..9dcc3a4ef 100644
--- a/src/foundation/compat_thread.c
+++ b/src/foundation/compat_thread.c
@@ -28,8 +28,23 @@ typedef struct {
void *arg;
} win_thread_arg_t;
-/* CBM_MI_THREAD_DONE=0 disables the release, so one binary can demonstrate the
- * behaviour with and without it. Causality has to be shown, not assumed. */
+/* Release each thread's allocator heap at DLL_THREAD_DETACH.
+ *
+ * Doing this from the thread wrapper instead crashes: the wrapper returns
+ * before the CRT's own thread teardown, and abandoning the heap there raced
+ * with frees still in flight (daemon_ipc_wait_forever_is_interruptible
+ * segfaulted, rc=139, reproducibly and only with the release enabled). A TLS
+ * callback is the mechanism mimalloc itself uses under MSVC and runs after all
+ * other thread cleanup, which is the only point where the heap is genuinely
+ * unreferenced.
+ *
+ * Without any of this, a static MinGW link -- no DllMain, no TLS callback --
+ * never releases a thread heap at all: 607 heaps after 300 requests, 170 MiB
+ * held against a ~300 KiB live set, growing without bound (#581). POSIX gets
+ * this from a pthread TSD destructor, which is why only Windows leaked.
+ *
+ * CBM_MI_THREAD_DONE=0 disables it, so one binary can demonstrate both
+ * behaviours rather than requiring a rebuild to establish causality. */
static bool thread_release_heap_enabled(void) {
static int state = -1;
if (state < 0) {
@@ -42,6 +57,21 @@ static bool thread_release_heap_enabled(void) {
return state == 1;
}
+static void NTAPI cbm_thread_detach_callback(PVOID handle, DWORD reason, PVOID reserved) {
+ (void)handle;
+ (void)reserved;
+ if (reason == DLL_THREAD_DETACH && thread_release_heap_enabled()) {
+ mi_thread_done();
+ }
+}
+
+/* Park the callback in .CRT$XLB, the table the loader walks. The linker only
+ * emits a TLS directory when _tls_used is referenced, so anchor it. */
+extern const IMAGE_TLS_DIRECTORY64 _tls_used;
+static const void *const cbm_tls_anchor __attribute__((used)) = &_tls_used;
+__attribute__((section(".CRT$XLB"), used)) PIMAGE_TLS_CALLBACK cbm_thread_detach_tls_cb =
+ cbm_thread_detach_callback;
+
static DWORD WINAPI win_thread_wrapper(LPVOID lpParam) {
win_thread_arg_t *a = (win_thread_arg_t *)lpParam;
void *(*fn)(void *) = a->fn;
@@ -58,9 +88,6 @@ static DWORD WINAPI win_thread_wrapper(LPVOID lpParam) {
* without bound (#581). The heaps are invisible to mi_heap_visit -- which
* only sees the main and calling heaps -- which is why the growth looked
* like it belonged to no allocator at all. */
- if (thread_release_heap_enabled()) {
- mi_thread_done();
- }
return 0;
}