From e0d7788d7cc4f72dba329210ee16544dcebd3da5 Mon Sep 17 00:00:00 2001 From: Cheng Date: Mon, 28 Sep 2026 10:37:30 +0800 Subject: [PATCH] Leak thread local streams on exit in main thread --- mlx/backend/cpu/encoder.cpp | 9 +++++++-- mlx/backend/cuda/device.cpp | 9 +++++++-- mlx/backend/metal/device.cpp | 9 +++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/mlx/backend/cpu/encoder.cpp b/mlx/backend/cpu/encoder.cpp index bb2c06ea0a..0b4654b22f 100644 --- a/mlx/backend/cpu/encoder.cpp +++ b/mlx/backend/cpu/encoder.cpp @@ -22,8 +22,13 @@ CommandEncoder& get_command_encoder(Stream s) { } std::unordered_map& get_command_encoders() { - static thread_local std::unordered_map encoders; - return encoders; + if (is_main_thread()) { + static auto* encoders = new std::unordered_map(); + return *encoders; + } else { + static thread_local std::unordered_map encoders; + return encoders; + } } std::unordered_map& get_global_command_encoders() { diff --git a/mlx/backend/cuda/device.cpp b/mlx/backend/cuda/device.cpp index e7d8f2620d..1f77924daa 100644 --- a/mlx/backend/cuda/device.cpp +++ b/mlx/backend/cuda/device.cpp @@ -612,8 +612,13 @@ CommandEncoder& get_command_encoder(Stream s) { } std::unordered_map& get_command_encoders() { - static thread_local std::unordered_map encoders; - return encoders; + if (is_main_thread()) { + static auto* encoders = new std::unordered_map(); + return *encoders; + } else { + static thread_local std::unordered_map encoders; + return encoders; + } } std::unordered_map& get_global_command_encoders() { diff --git a/mlx/backend/metal/device.cpp b/mlx/backend/metal/device.cpp index 2f25f894e4..d3f435a14b 100644 --- a/mlx/backend/metal/device.cpp +++ b/mlx/backend/metal/device.cpp @@ -931,8 +931,13 @@ CommandEncoder& get_command_encoder(Stream s) { } std::unordered_map& get_command_encoders() { - static thread_local std::unordered_map encoders; - return encoders; + if (is_main_thread()) { + static auto* encoders = new std::unordered_map(); + return *encoders; + } else { + static thread_local std::unordered_map encoders; + return encoders; + } } std::unordered_map& get_global_command_encoders() {