Repository navigation
Add cooperative cancellation check to the prompt prefill loop - #423
Merged
davidkoski merged 1 commit intoJul 14, 2026
Merged
Conversation
Member
|
The change itself looks like a great idea! |
On iOS, GPU work submitted while an app is in the background is rejected by the system, and the resulting Metal command-buffer error is thrown as a C++ exception from a completion handler where nothing can catch it, aborting the process. Token generation already checks Task.isCancelled between tokens, but the prompt prefill loop did not - so a multi-second prefill of a long prompt could not be stopped before the app finished transitioning to the background. Check Task cancellation between prefill windows so callers can cancel generation (e.g. on UIApplication.willResignActiveNotification) before further GPU work is enqueued. Ref: ml-explore/mlx-swift-examples#230
davidkoski
force-pushed
the
jot/prefill-cancellation
branch
from
July 14, 2026 21:02
8005bac to
86de8c1
Compare
dkgv
pushed a commit
to dkgv/mlx-swift-lm
that referenced
this pull request
Jul 30, 2026
On iOS, GPU work submitted while an app is in the background is rejected by the system, and the resulting Metal command-buffer error is thrown as a C++ exception from a completion handler where nothing can catch it, aborting the process. Token generation already checks Task.isCancelled between tokens, but the prompt prefill loop did not - so a multi-second prefill of a long prompt could not be stopped before the app finished transitioning to the background. Check Task cancellation between prefill windows so callers can cancel generation (e.g. on UIApplication.willResignActiveNotification) before further GPU work is enqueued. Ref: ml-explore/mlx-swift-examples#230
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.
Problem
Token generation already checks
Task.isCancelledbetween tokens, but the prompt prefill loop inLLMModel.preparedoes not. For long prompts, prefill runs for multiple seconds as an uninterruptible sequence of GPU submissions.On iOS this is fatal: GPU work submitted while an app is in the background is rejected by the system ("Insufficient Permission (to submit GPU work in background)"), and the resulting Metal command-buffer error is thrown as a C++ exception from a completion handler where Swift cannot catch it, aborting the process. Apps cancel generation on
UIApplication.willResignActiveNotification, but if the model is mid-prefill the cancellation has no effect until prefill finishes — by which time the app is already backgrounded and the process is killed. See #230 (mlx-swift-examples) for the original report of this class of crash.Fix
Check
Task.checkCancellation()between prefill windows.prepareis alreadythrows, andTokenIterator/generatealready propagate errors, so cancelling the surrounding task now stops further GPU submissions within oneprefillStepSizewindow (~hundreds of ms with small window sizes) instead of after the full prompt.Verified on device (iPhone, iOS 26): an app that cancels in-flight generation on resign-active previously aborted reliably when backgrounded during prefill of a ~800-token prompt; with this change it survives repeated background/foreground cycles with generation in flight (8/8 in our stress test, previously 0/8).