Skip to content

Add cooperative cancellation check to the prompt prefill loop - #423

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
sunshinfight:jot/prefill-cancellation
Jul 14, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
sunshinfight:jot/prefill-cancellation

Conversation

@sunshinfight

Copy link
Copy Markdown
Contributor

Problem

Token generation already checks Task.isCancelled between tokens, but the prompt prefill loop in LLMModel.prepare does 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. prepare is already throws, and TokenIterator/generate already propagate errors, so cancelling the surrounding task now stops further GPU submissions within one prefillStepSize window (~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).

@davidkoski

davidkoski commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

If you have a chance can you rebase this with main? It is showing a conflict. If not I will try to get to it later today -- it looks simple. Done!

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
davidkoski force-pushed the jot/prefill-cancellation branch from 8005bac to 86de8c1 Compare July 14, 2026 21:02

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, thank you!

@davidkoski
davidkoski merged commit 2b03485 into ml-explore:main Jul 14, 2026
2 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants