From afa6c90df2d8f0f78f12b42a598a4e93c10a51e6 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 14 Sep 2026 10:40:25 +0800 Subject: [PATCH] set _mi_process_is_initialized before mi_process_setup_auto_thread_done 439dc27b moved `mi_process_setup_auto_thread_done` into `mi_process_init_once` so it runs after `mi_thread_init` and can associate the current theap with the thread-done key. It now runs before `_mi_process_is_initialized = true`, and with `MI_TLS_RECURSE_GUARD` (always on for `MI_TLS_MODEL_LOCAL` on macOS) `_mi_theap_default()` still returns `_mi_theap_empty` at that point. The new `mi_assert_internal(mi_theap_is_initialized(theap))` fires in debug builds; release builds skip the association, so the first thread's theap is again never passed to `_mi_thread_done`. Repro (macOS): `cmake -DCMAKE_BUILD_TYPE=Debug -DMI_TLS_MODEL=LOCAL` then `./mimalloc-test-api` aborts at process init on v3.5.2 and passes on v3.5.1. Same with `-DMI_TLS_RECURSE_GUARD=ON` on Linux. Set the flag before `mi_process_setup_auto_thread_done`; the theap is fully initialized by then. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_016srh5svTkqfW21zGcU6ht9 --- src/init.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/init.c b/src/init.c index 30445a58c..9b0ded5b5 100644 --- a/src/init.c +++ b/src/init.c @@ -560,8 +560,8 @@ static void mi_process_init_once(void) { // the following can potentially allocate (on freeBSD for pthread keys) _mi_tls_slots_init(); // pthread key create _mi_thread_locals_init(); // pthread key create - mi_process_setup_auto_thread_done(); // after the above mi_thread_init so it can add the current theap - _mi_process_is_initialized = true; + _mi_process_is_initialized = true; // before `mi_process_setup_auto_thread_done` so `_mi_theap_default` returns the current theap with `MI_TLS_RECURSE_GUARD` + mi_process_setup_auto_thread_done(); // after the above mi_thread_init so it can add the current theap #if defined(_WIN32) && defined(MI_WIN_INIT_USE_FLS) // On windows, when building as a static lib the FLS cleanup happens to early for the main thread.