From 2133bd7827cd5a7a496adaab1d4a48473160b1bd Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Tue, 6 Oct 2026 12:46:50 -0400 Subject: [PATCH] fix: wait at exit for a free in flight on the last tracked object llama_dart_exit_free untracks its object under the registry lock and frees it outside the lock. Teardown returned at once when nothing was tracked and no creating call was in flight, so an exit that landed while the last tracked object was being freed went on to ggml-metal's static destructor with Metal buffers still allocated and aborted on GGML_ASSERT([rsets->data count] == 0). Frees in flight are now counted under the lock that untracks the object, and teardown takes its normal bounded wait while one is running. A creating call is counted under the lock that begins it instead of a second one, so teardown cannot run between the two and return before the creation starts. --- CMakeLists.txt | 6 +- src/llama_dart_wrapper.cpp | 56 ++++++++++------ src/llama_dart_wrapper.h | 4 +- tests/exit_teardown_test.cpp | 124 +++++++++++++++++++++++++++++++++-- 4 files changed, 161 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f71549e..9a71325a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -249,7 +249,8 @@ if (LLAMADART_BUILD_TESTS) src ) foreach(scenario dispose order repeat in-flight nested settle timeout - idle-wait own-call blocked exit late-static) + idle-wait own-call blocked exit late-static free-in-flight + free-timeout) add_test(NAME llamadart_exit_teardown_${scenario} COMMAND llamadart_exit_teardown_test ${scenario}) endforeach() @@ -276,7 +277,8 @@ if (LLAMADART_BUILD_TESTS) foreach(scenario model-idle model-dispose model-sync model-wrappers model-order model-session model-session-untracked model-freed model-decode-wait model-mtmd-wait model-decode - model-late-load model-load model-load-wait) + model-late-load model-load model-load-wait + model-free-in-flight) add_test(NAME llamadart_exit_teardown_${scenario} COMMAND llamadart_exit_teardown_test ${scenario} "${exit_teardown_model}") diff --git a/src/llama_dart_wrapper.cpp b/src/llama_dart_wrapper.cpp index 87ae1d0e..3326e172 100644 --- a/src/llama_dart_wrapper.cpp +++ b/src/llama_dart_wrapper.cpp @@ -768,6 +768,9 @@ struct llama_dart_exit_registry { uint64_t next_order = 0; int32_t calls = 0; int32_t creating_calls = 0; + // Frees in flight. The object of one is no longer in objects, and exit must + // not go on to destroy the statics that the free still uses. + int32_t freeing_calls = 0; int32_t wait_ms = 2000; std::atomic armed{false}; std::atomic torn_down{false}; @@ -931,11 +934,39 @@ static void llama_dart_exit_release(void *object, if (llama_dart_exit_call_depth++ == 0) { ++registry.calls; } + // Counted under the lock that untracked the object, so that teardown sees + // either the object or its free. + ++registry.freeing_calls; } free_fn(object); + { + std::lock_guard lock(registry.mutex); + --registry.freeing_calls; + } llama_dart_exit_call_end(); } +// Begins a call in flight on this thread. A creating call is counted as one +// under the same lock, so that teardown cannot run in between and return +// before the creation starts. +static void llama_dart_exit_begin_call(bool creating) { + if (!creating && llama_dart_exit_call_depth > 0) { + ++llama_dart_exit_call_depth; + return; + } + auto ®istry = llama_dart_exit_state(); + std::unique_lock lock(registry.mutex); + if (llama_dart_exit_call_depth > 0) { + ++llama_dart_exit_call_depth; + } else if (llama_dart_exit_admit(registry, lock)) { + ++registry.calls; + llama_dart_exit_call_depth = 1; + } + if (creating) { + ++registry.creating_calls; + } +} + struct llama_dart_exit_call { llama_dart_exit_call() { llama_dart_exit_call_begin(); } ~llama_dart_exit_call() { llama_dart_exit_call_end(); } @@ -946,12 +977,7 @@ struct llama_dart_exit_call { // A call in flight that tracks what it creates. Teardown waits for it even // when nothing is tracked yet. struct llama_dart_exit_creating_call { - llama_dart_exit_creating_call() { - llama_dart_exit_call_begin(); - auto ®istry = llama_dart_exit_state(); - std::lock_guard lock(registry.mutex); - ++registry.creating_calls; - } + llama_dart_exit_creating_call() { llama_dart_exit_begin_call(true); } ~llama_dart_exit_creating_call() { { auto ®istry = llama_dart_exit_state(); @@ -1059,17 +1085,7 @@ LLAMADART_API int32_t llama_dart_exit_tracked_count(void) { } LLAMADART_API void llama_dart_exit_call_begin(void) { - if (llama_dart_exit_call_depth > 0) { - ++llama_dart_exit_call_depth; - return; - } - auto ®istry = llama_dart_exit_state(); - std::unique_lock lock(registry.mutex); - if (!llama_dart_exit_admit(registry, lock)) { - return; - } - ++registry.calls; - llama_dart_exit_call_depth = 1; + llama_dart_exit_begin_call(false); } LLAMADART_API void llama_dart_exit_call_end(void) { @@ -1103,9 +1119,9 @@ LLAMADART_API void llama_dart_exit_teardown(void) { const auto can_free = [®istry](const auto &tracked) { return llama_dart_exit_can_free(registry, tracked.second); }; - // With nothing to free and nothing being created, a call in flight is no - // reason to hold up the exit. - if (registry.creating_calls == 0 && + // With nothing to free and nothing being created or freed, a call in + // flight is no reason to hold up the exit. + if (registry.creating_calls == 0 && registry.freeing_calls == 0 && std::none_of(registry.objects.begin(), registry.objects.end(), can_free)) { return; diff --git a/src/llama_dart_wrapper.h b/src/llama_dart_wrapper.h index 2a2b8934..956fbcb7 100644 --- a/src/llama_dart_wrapper.h +++ b/src/llama_dart_wrapper.h @@ -192,8 +192,8 @@ LLAMADART_API void llama_dart_set_log_level(int level); // It leaves TTS, speculative and MTP state alone while the llama or mtmd // context that state was created over is not tracked: a caller that creates // contexts with the upstream functions may be using them in calls teardown -// cannot see. With nothing to free and no creating call in flight, it does not -// wait. +// cannot see. With nothing to free and no creating call or free in flight, it +// does not wait. // // A call in flight is the time a thread spends inside one of: // - the three llama_dart creating functions named above and diff --git a/tests/exit_teardown_test.cpp b/tests/exit_teardown_test.cpp index d9b9a1c4..c4dda51a 100644 --- a/tests/exit_teardown_test.cpp +++ b/tests/exit_teardown_test.cpp @@ -316,8 +316,13 @@ int test_timeout() { return 0; } -// A call that never ends does not hold up an exit that has nothing to free. +// A call that never ends does not hold up an exit that has nothing to free, +// and neither does a free that has finished. int test_idle_wait() { + char *object = name("object"); + assert( + llama_dart_exit_track(object, free_named, LLAMA_DART_EXIT_STAGE_MODEL)); + llama_dart_exit_free(object); std::thread([] { llama_dart_exit_call_begin(); }).join(); llama_dart_exit_set_wait_ms(30000); const auto started = std::chrono::steady_clock::now(); @@ -998,6 +1003,13 @@ void beat_until_teardown() { } } +void start_beat() { + std::thread(beat_until_teardown).detach(); + while (g_last_beat_ms.load() == 0) { + sleep_ms(1); + } +} + // Releases the held evaluation once teardown is waiting for it, and gives up // after ten seconds. void release_when_teardown_waits() { @@ -1057,10 +1069,7 @@ int test_evaluation_wait(const char *path, void (*evaluate)(model_fixture &), while (!g_evaluation_held.load()) { sleep_ms(1); } - std::thread(beat_until_teardown).detach(); - while (g_last_beat_ms.load() == 0) { - sleep_ms(1); - } + start_beat(); std::thread(release_when_teardown_waits).detach(); llama_dart_exit_teardown(); assert(g_release_evaluation.load()); @@ -1214,6 +1223,102 @@ int test_model_load_wait(const char *path) { return 0; } +std::atomic g_free_started{false}; +std::atomic g_free_finished{false}; + +// A free that the exit catches in flight: it finishes only once teardown has +// begun. +void free_when_teardown_waits(void *) { + g_free_started.store(true); + release_when_teardown_waits(); + g_free_finished.store(true); +} + +// Teardown waits for a free in flight although nothing is tracked any more. +// The free has untracked its object, and the exit would go on to destroy what +// the free still uses. +int test_free_in_flight() { + static char object[] = "object"; + assert(llama_dart_exit_track(object, free_when_teardown_waits, + LLAMA_DART_EXIT_STAGE_MODEL)); + llama_dart_exit_set_wait_ms(30000); + start_beat(); + std::thread([] { llama_dart_exit_free(object); }).detach(); + while (!g_free_started.load()) { + sleep_ms(1); + } + assert(llama_dart_exit_tracked_count() == 0); + + llama_dart_exit_teardown(); + assert(g_free_finished.load()); + assert(!g_released_before_teardown.load()); + return 0; +} + +// A free that never finishes holds teardown up for the wait time only. +int test_free_timeout() { + static char object[] = "object"; + assert(llama_dart_exit_track( + object, + [](void *) { + g_free_started.store(true); + sleep_ms(600000); + }, + LLAMA_DART_EXIT_STAGE_MODEL)); + llama_dart_exit_set_wait_ms(1000); + std::thread([] { llama_dart_exit_free(object); }).detach(); + while (!g_free_started.load()) { + sleep_ms(1); + } + + const auto started = std::chrono::steady_clock::now(); + llama_dart_exit_teardown(); + const int64_t waited = elapsed_ms(started); + assert(waited >= 900); + assert(waited < 20000); + return 0; +} + +void free_model_when_teardown_waits(void *object) { + g_free_started.store(true); + release_when_teardown_waits(); + llama_model_free(static_cast(object)); + g_free_finished.store(true); +} + +void expect_free_finished_at_exit() { + if (!g_free_finished.load() || g_released_before_teardown.load()) { + fprintf(stderr, "the exit did not wait for the free in flight\n"); + _Exit(EXIT_FAILURE); + } +} + +// Exits while another thread frees the model, the only object still tracked. +// Metal aborts in its static destructor when the exit does not wait for the +// free; without Metal the check at exit shows it. +int test_model_free_in_flight(const char *path) { + // Registered first, so it runs after teardown and after the Metal device, + // which the load creates, is destroyed. + assert(atexit(expect_free_finished_at_exit) == 0); + static model_fixture fixture; + fixture = load_model(path); + llama_sampler *sampler = greedy_sampler(); + assert(decode_and_sample(fixture, sampler) >= 0); + llama_sampler_free(sampler); + llama_dart_exit_free(fixture.context); + // Tracked again, with a free that the test can hold. + assert(llama_dart_exit_track(fixture.model, free_model_when_teardown_waits, + LLAMA_DART_EXIT_STAGE_MODEL)); + llama_dart_exit_set_wait_ms(30000); + start_beat(); + std::thread([] { llama_dart_exit_free(fixture.model); }).detach(); + while (!g_free_started.load()) { + sleep_ms(1); + } + assert(llama_dart_exit_tracked_count() == 0); + return 0; +} + // Evaluates an image prompt chunk by chunk, as the Dart side does. int32_t eval_chunks_one_by_one(mtmd_context *mtmd, model_fixture &fixture, const mtmd_input_chunks *chunks, @@ -1369,6 +1474,12 @@ int main(int argc, char **argv) { if (scenario == "late-static") { return test_late_static(); } + if (scenario == "free-in-flight") { + return test_free_in_flight(); + } + if (scenario == "free-timeout") { + return test_free_timeout(); + } if (scenario == "graph") { return test_graph(); } @@ -1427,6 +1538,9 @@ int main(int argc, char **argv) { if (scenario == "model-load-wait") { return test_model_load_wait(first); } + if (scenario == "model-free-in-flight") { + return test_model_free_in_flight(first); + } if (second != nullptr) { if (scenario == "model-mtmd") { return test_model_mtmd(first, second);