Repository navigation
ChatSession: cancel the generation task when the consumer goes away - #413
Merged
davidkoski merged 1 commit intoJul 10, 2026
Merged
Conversation
The generation task from generateTask() is unstructured, so cancelling streamMap's task (stream onTermination) never propagated to it. A caller that stops consuming mid-stream (barge-in) and immediately issues the next request deadlocks: 'await genTask.value' waits for the full generation while holding the session's serial cache lock. Cancel the generation task explicitly on consumer termination and on task cancellation; the generate loop already checks Task.isCancelled per token, so it stops promptly.
davidkoski
approved these changes
Jul 10, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Change looks good, nice find! Thank you
1 of 4 tasks
dkgv
pushed a commit
to dkgv/mlx-swift-lm
that referenced
this pull request
Jul 30, 2026
…l-explore#413) The generation task from generateTask() is unstructured, so cancelling streamMap's task (stream onTermination) never propagated to it. A caller that stops consuming mid-stream (barge-in) and immediately issues the next request deadlocks: 'await genTask.value' waits for the full generation while holding the session's serial cache lock. Cancel the generation task explicitly on consumer termination and on task cancellation; the generate loop already checks Task.isCancelled per token, so it stops promptly.
div0-space
pushed a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Nov 29, 2026
Composition with ml-explore#409 (4A grammar-aware MTP), both opt-in: - CBv2MTPConfig carries both constrainedSpeculation and freeSpanSpeculation. - Planning: history eligibility admits constrained rows when grammar verification is on or the constraint offers free spans; drafting admits them when grammar verification is on or the row is in a free span. - A row takes exactly one path per round: a row whose committed state is an opted-in free span keeps the pure same-state preview (masked window, per-token confirmation at finalize); grammar verification (retained raw logits, ordinary sampler per committed position) owns every other constrained row. - The free-span window mask applies only when a free-span row is in the batch, and raw logits are retained only when a grammar-verified row is, so either opt-in alone behaves exactly as its own PR.
div0-space
pushed a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Nov 29, 2026
On the cumulative SDK ml-explore#416 (ml-explore#409 + ml-explore#413 + ml-explore#411 + ml-explore#408 on main 859f641). test-shards.json: gemma-reopened-thought-enabled kept beside gemma-quoted-number-enabled as its own extra run in the same shard (the shard tool refuses one run mixing different env). Every other file merges unchanged: the merge adds exactly ml-explore#407's lines.
div0-space
pushed a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Nov 29, 2026
…ion) into the cumulative SDK Only .github/test-shards.json conflicted. Resolution = ml-explore#414's replanned map (4 native shards + the hosted audio shard, hosted_tests, rest run in shard 4) byte for byte, plus what this branch had added to the old map, each once: - groups native-reasoning-budget (ml-explore#411) and gemma-quoted-number-enabled (ml-explore#408, extra pass with DARKBLOOM_GEMMA_SCHEMA_COERCION=1), unchanged; - a run for each group (shards 1 and 2) and one units run in shard 3 for CBv2FreeSpanTests + QwenFreeSpanHistoryTests (ml-explore#413, was its own shard) and CBv2GrammarNonFiniteTests + CBv2GrammarQwen4Tests (ml-explore#409, was the rest run); - estimated_seconds of those three shards raised accordingly. Every other ml-explore#414 file is main's version; no Swift file differs from the cumulative head eaed116. Checked: test_shards.py load_map/resolve_runs (36 groups each used once, one rest run, no unit listed twice; each new suite selected by exactly one main run); test_shards_test.py 27/27 OK.
div0-space
pushed a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Nov 29, 2026
Cumulative SDK for provider 0.10.13: ml-explore#409 + ml-explore#413 + ml-explore#411 + ml-explore#408 (opt-in)
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
The generation task returned by
generateTask()is unstructured, so cancellingstreamMap's task (via the stream'sonTermination) never propagates to it. A caller that stops consuming mid-stream and immediately issues the next request effectively deadlocks the session:streamMap's task is cancelled, but the generation task keeps generating the FULL reply.respond/streamResponsecall blocks on the session's serial cache lock, which is held untilawait genTask.valuereturns — i.e. until the abandoned generation finishes every remaining token.On a 12B model on device that's many seconds of dead air per interruption (we hit this in a voice-assistant barge-in flow; it looks like a hang).
Fix
Cancel the generation task explicitly at the two places the consumer goes away: when
continuation.yieldreports.terminated, and when the surrounding task itself is cancelled. The generate loop already checksTask.isCancelledper token, so it stops promptly,await genTask.valuereturns, and the cache lock is released. The existing "wait for the task to complete" semantics are preserved — we still await the task so the KV cache is never used concurrently.Testing
xcodebuild build-for-testing+MLXLMTestsviaxcrun xctest(same as CI) pass on this branch.