Repository navigation
Conversation
MLX sometimes dies at thread or process teardown with "terminate called without an active exception" or SIGSEGV, on Linux/Windows. The general pattern: a destructor reachable from __call_tls_dtors() must not acquire the GIL, nor throw, nor drop a Python reference. Acquiring the GIL is fatal because take_gil() answers a request from a non-finalizing thread, while another thread finalizes, by calling PyThread_exit_thread(); the forced unwind then crosses the noexcept destructor frame. Throwing is equally fatal: CUDA destructors ran CHECK_*_ERROR on teardown calls that legitimately fail -- every Windows run with two threads died. Adds python/tests/test_teardown.py. The failure kills the process, so the cases run in subprocesses and repeat; both fail without this change.
zcbenz
reviewed
Aug 10, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
Our current recommended pattern is to let users call clear_streams at the end of threads and the process, so they can do cleanup before cuda and python shutdown, which is something that we could not do on the library side. Have you tried that? We should have a multi-threading doc for all these APIs though.
Lines 89 to 101 in e78d894
Contributor
Author
|
I can close this PR, these are just crashes that I was noticing while working on other improvements. |
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.
Proposed changes
MLX sometimes dies at thread or process teardown with "terminate called without an active exception" or SIGSEGV, on Linux/Windows. The general pattern: a destructor reachable from __call_tls_dtors() must not acquire the GIL, nor throw, nor drop a Python reference.
Acquiring the GIL is fatal because take_gil() answers a request from a non-finalizing thread, while another thread finalizes, by calling PyThread_exit_thread(); the forced unwind then crosses the noexcept destructor frame.
Throwing is equally fatal: CUDA destructors ran CHECK_*_ERROR on teardown calls that legitimately fail -- every Windows run with two threads died.
Adds python/tests/test_teardown.py. The failure kills the process, so the cases run in subprocesses and repeat; both fail without this change.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes