feat(runtime): mid-stream cancellation#161
Merged
Merged
Conversation
AsyncStream does NOT auto-cancel the producer Task when the consumer breaks out of `for await`. A user who taps stop or navigates away during a long generation kept burning ANE energy until the loop hit maxDecode. This wires the missing piece: - bind the producer Task to a `genTask` handle - check Task.checkCancellation() at chunk boundaries in both prefill paths (hybrid + decode-loop fallback) and at the top of the decode loop, so cancellation lands within one chunk / one token - catch CancellationError silently — it's the expected user path - continuation.onTermination → genTask.cancel() so a stream deinit or the consumer dropping subscription stops generation immediately Net effect: no API change, no token-output difference, but cancelling a generation now releases the ANE within one decode step (~30 ms) instead of seconds-to-minutes. Extracted from feat/litert-perf-adoptions (commit 8598388 §T4); other items in that branch will land separately.
6d54bab to
e3bf3b2
Compare
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.
Summary
AsyncStream does not auto-cancel the producer Task when the consumer breaks out of
for await. Stopping generation mid-stream (user tap, navigation, deinit) kept burning ANE energy until the loop hitmaxDecode.This PR wires the missing piece:
genTaskhandleTask.checkCancellation()at chunk boundaries in both prefill paths (hybrid + decode-loop fallback) and at the top of the decode loop, so cancellation lands within one chunk / one tokenCancellationErrorsilently — it's the expected user pathcontinuation.onTermination → genTask.cancel()so a stream deinit or consumer drop stops generationNo API change. Cancelling a generation now releases the ANE within one decode step (~30 ms) instead of running to
maxDecode.Extracted from
feat/litert-perf-adoptions(commit 8598388, §T4). Other items (S1/S2/T1/T3/T5) will land separately.Test plan
swift buildon macos-15