From 30437ff3b80d1d726e9841e9741c591675b31824 Mon Sep 17 00:00:00 2001 From: David Tai Date: Sat, 24 Oct 2026 10:44:45 -0500 Subject: [PATCH 1/2] Test that a freed compiled closure leaves every thread cache compile(std::function) keys a closure by the address of a heap copy. The deleter of that copy erases only the cache of the thread that called compile(). A trace recorded on another thread stays after the copy is freed. The new test records a trace on a second thread, frees the closure, and checks that the captured array is uniquely referenced again. It fails until the deleter erases through the cache registry. Co-Authored-By: Claude Opus 5.5 (1M context) --- tests/compile_tests.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/compile_tests.cpp b/tests/compile_tests.cpp index 3e0ce06601..2fd000877d 100644 --- a/tests/compile_tests.cpp +++ b/tests/compile_tests.cpp @@ -921,3 +921,37 @@ TEST_CASE("test compatibility erase clears every live thread cache") { first.join(); second.join(); } + +TEST_CASE("test freeing a compiled closure erases every thread cache") { + // compile(std::function) keys a closure by the address of a heap copy of + // it. When the last copy of the compiled function is freed, its traces must + // go from every thread cache, not only from the cache of the thread that + // called compile(). A trace that stays keeps its arrays alive, and a later + // closure that gets the same heap address replays it. + // + // The trace holds the captured array, so the array is uniquely referenced + // again only when no cache holds the trace. The array is built from host + // data and the worker does not evaluate the output, so no device work holds + // a reference to it. + auto captured = array({1.0f, 2.0f, 3.0f}); + auto compiled = compile([captured](const std::vector& inputs) { + return std::vector{inputs[0] + captured}; + }); + + std::barrier phase(2); + std::thread worker([&]() { + { + // Record a trace in this thread's cache only. + auto outputs = compiled({array({1.0f, 1.0f, 1.0f})}); + } + phase.arrive_and_wait(); + // Keep this thread, and so its cache, alive while the closure is freed. + phase.arrive_and_wait(); + }); + + phase.arrive_and_wait(); + compiled = nullptr; + CHECK(captured.is_uniquely_referenced()); + phase.arrive_and_wait(); + worker.join(); +} From ff4bb35b60c3aadafb9e0bd30b6180cef646fb1a Mon Sep 17 00:00:00 2001 From: David Tai Date: Sat, 24 Oct 2026 10:46:18 -0500 Subject: [PATCH 2/2] Erase a freed compiled closure from every thread cache compile(std::function) keys a closure by the address of a heap copy. Each call records its trace in the calling thread's cache, but the deleter of the copy erased only the cache of the thread that called compile(). Traces from other threads stayed after the free: they kept their arrays alive, and a later closure at the same heap address could replay them. The deleter now erases through the cache registry (detail::compile_erase(fun_id)), before the delete. Co-Authored-By: Claude Opus 5.5 (1M context) --- fork.yaml | 14 ++++++++++++++ mlx/compile.cpp | 9 +++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/fork.yaml b/fork.yaml index 969d6e683f..376a8bc9db 100644 --- a/fork.yaml +++ b/fork.yaml @@ -172,6 +172,20 @@ def: - "mlx/compile.cpp" - "mlx/compile_impl.h" - "tests/compile_tests.cpp" + - title: "Freed compiled closures leave every thread cache" + description: | + `compile(std::function)` keys a closure by the address of a heap copy of it. Upstream, + the deleter of that copy erases the key only from the cache of the thread that called + `compile()`. Each call records its trace in the cache of the calling thread, so a trace + from another thread stayed after the copy was freed. The trace kept its arrays alive, + and a later closure at the same address could replay it. The deleter now erases the key + from every live thread cache through the registry above, before the delete. The test + `test freeing a compiled closure erases every thread cache` records a trace on a second + thread, frees the closure, and checks that the captured array is uniquely referenced. + (PR #43) + globs: + - "mlx/compile.cpp" + - "tests/compile_tests.cpp" - title: "Integer floor_divide tests" description: | The fork keeps its regression tests for integer `floor_divide` from PR #32. Upstream diff --git a/mlx/compile.cpp b/mlx/compile.cpp index cd2ffdf9e5..fbb04aa6ed 100644 --- a/mlx/compile.cpp +++ b/mlx/compile.cpp @@ -1302,8 +1302,13 @@ std::function(const std::vector&)> compile( auto pfun = std::shared_ptr< std::function(const std::vector&)>>( new std::function(const std::vector&)>{fun}, - [cache = detail::compile_cache()](auto* p) { - detail::compile_erase(cache, reinterpret_cast(p)); + [](auto* p) { + // The compiled function can be called on any thread, and each call + // records its trace in the cache of the calling thread. Erase the + // key from every live thread cache, and do it before the delete so + // that no new function can get this address while a trace for it + // is still cached. + detail::compile_erase(reinterpret_cast(p)); delete p; }); fun_id = reinterpret_cast(pfun.get());