Do not throw from CUDA destructors and avoid implicit default streams in eval/compile - #4514
Merged
Merged
Conversation
Contributor
Author
@zcbenz you are right on the Worker, so I've dropped the Worker changes. For the record, the #4506 trace unwinds from the detached thread's I think we are good now and hopefully the fixes can then unlock the the new release on mlx-swift. @zcbenz thanks for the support, appreciated. |
zcbenz
force-pushed
the
threads_and_strems_4506
branch
from
September 28, 2026 09:13
ed20ef1 to
2bba7fa
Compare
zcbenz
force-pushed
the
threads_and_strems_4506
branch
from
September 28, 2026 09:14
2bba7fa to
0fc5521
Compare
zcbenz
approved these changes
Sep 28, 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.
As described in #4506 a termination crash occurs when a detached worker thread outlives the CUDA runtime and attempts to clean up CUDA resources, triggering a std::terminate.
To resolve this I have made some changes so that the worker class is restructured therefore the detached thread only captures a new, inert State struct containing standard threading primitives. Additionally the CUDA resources remain in the
Worker, which is now managed by theCommandEncoderas aunique_ptrto ensure deterministic destruction before the runtime unloads. The destructor~CudaHandlehas been modified to safely ignore errors during shutdown, andeval_implnow selects streams lazily to prevent accidental worker instantiation.Fixes #4506