Skip to content

ChatSession: cancel the generation task when the consumer goes away - #413

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
fdagostino:pr/upstream-cancel-gentask
Jul 10, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
fdagostino:pr/upstream-cancel-gentask

Conversation

@fdagostino

Copy link
Copy Markdown
Contributor

Problem

The generation task returned by generateTask() is unstructured, so cancelling streamMap's task (via the stream's onTermination) never propagates to it. A caller that stops consuming mid-stream and immediately issues the next request effectively deadlocks the session:

  1. Consumer cancels the stream (e.g. voice barge-in: the user talks over the reply).
  2. streamMap's task is cancelled, but the generation task keeps generating the FULL reply.
  3. The next respond/streamResponse call blocks on the session's serial cache lock, which is held until await genTask.value returns — 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.yield reports .terminated, and when the surrounding task itself is cancelled. The generate loop already checks Task.isCancelled per token, so it stops promptly, await genTask.value returns, 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 + MLXLMTests via xcrun xctest (same as CI) pass on this branch.
  • Reproduced the stall and verified the fix on-device (iPad, Gemma 4 12B): cancel mid-generation + immediate next request now responds promptly instead of waiting out the abandoned generation.

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 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.

Change looks good, nice find! Thank you

@davidkoski
davidkoski merged commit 2c1dd13 into ml-explore:main Jul 10, 2026
2 checks passed
@GoodOlClint GoodOlClint mentioned this pull request Jul 30, 2026
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
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