Repository navigation
fix: Give the worker threads a 64 MB stack - #151
Merged
polvalente merged 2 commits intoOct 20, 2026
Merged
Conversation
MLX compiles a graph with compile_dfs, which recurses once for each level of the graph. std::thread gets the default stack of a secondary thread, 512 KB on macOS, and a compiled chain of 10,000 ops overflows it with SIGBUS. The worker now starts with pthread_create and a 64 MB stack, which holds a chain of 200,000 ops.
polvalente
approved these changes
Oct 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A compiled function with a deep graph stops the VM with SIGBUS on macOS:
MLX compiles a graph with
compile_dfs, which recurses once for each level of the graph. The program compiles on the worker thread, andstd::threadgets the default stack of a secondary thread. On macOS that stack is 512 KB, and a chain of 10,000 ops overflows it. The crash report shows the fault inmlx::core::detail::compile_dfson the worker thread. I found this when an ONNX model with an unrolled LSTM over audio frames crashed for long inputs.With this change, the worker starts with
pthread_createand a 64 MB stack. Stack use is about 200 bytes per level, so a chain of 200,000 ops compiles, and 400,000 still overflows. 64 MB is huge for a stack, but it is address space, not memory. The OS maps a stack page only when the thread first uses it. A worker that never compiles a deep graph uses only the few pages it needs, and a deep compile uses only the pages for the depth that it reaches. On Linux, glibc threads usually get 8 MB, so the limit goes up there too.The test compiles the chain above, 20,000 ops. It stops with SIGBUS when I set the stack back to 512 KB.
A
compile_dfswithout recursion in MLX would remove the limit. This change makes the graphs that people build today safe.