Skip to content

Serialize MTP runs per session (fix cancel race), release v0.8.70 - #127

Merged
nyo16 merged 2 commits into
masterfrom
fix-mtp-cancel-race
Nov 14, 2026
Merged

nyo16 merged 2 commits into
masterfrom
fix-mtp-cancel-race

Conversation

@nyo16

@nyo16 nyo16 commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Fixes the :mtp_cancel race and bumps the version to v0.8.70. llama.cpp stays at 9daa0aa22.

Problem

Cancelling an MTP stream only sets a flag; nothing confirms the loop has stopped. Generator.stop/1 returns while the cancelled generate_mtp_tokens loop can still be inside a target or draft decode. A generate/3 / stream/3 started right then on the same %MTP{} session decoded into the same two contexts at the same time. At 9daa0aa22 that aborted the VM in 4 of 7 runs with GGML_ASSERT(logits != nullptr) (common/sampling.cpp, from the MTP drafter).

Fix

LlamaSpeculative gains a run_mutex, which generate_mtp_tokens holds for its whole run. A second call on the session waits for the first to finish. After a cancel the wait is at most one draft-and-verify step: the loop checks the flag every step, and a cancelled prompt decode stops early through the abort callback. The cancel protocol for normal (non-MTP) streaming is unchanged.

Behaviour change: a call made while another stream on the same session is still running now waits for it, instead of corrupting it.

Tests and docs

  • The known-broken LlamaCppEx.MTPCancelTest (:mtp_cancel) becomes a regular :mtp test in MTPModelTest. It cancels three streams in a row and asserts the greedy output afterwards is unchanged, so it also catches a corrupted cache that doesn't crash.
  • The :mtp_cancel tag is removed from test_helper.exs and from the release guide.
  • The MTP moduledoc warning against reusing a session is replaced by a description of the waiting behaviour.

Verification (M1 Max, Metal, Qwen3.6-27B)

Run Result
Old mtp_cancel test, before the fix 4/7 aborted
Old test with the fix 10/10 passed
New test with the lock removed (negative control) 5/5 aborted, GGML_ASSERT(logits != nullptr)
:mtp module with the fix 5/5 runs, 9/9 tests each
Default suite, vm_exit_test.exs, :mtp_sidecar (Qwen3.8-27B), format pass

Cancelling an MTP stream is unacknowledged: the consumer returns while the
cancelled loop can still be inside a target or draft decode. The next
generate/stream on the same session then decoded into the same two
contexts, aborting the VM (GGML_ASSERT(logits != nullptr) in 4 of 7 runs at
9daa0aa22).

generate_mtp_tokens now holds a per-session mutex for its whole run, so a
second call waits, at most one draft-and-verify iteration after a cancel.

The known-broken :mtp_cancel test moves into :mtp and also compares the
greedy output after three cancelled streams. Without the lock it aborts
5/5; with it, 10/10 of the old test and 5/5 of the :mtp module pass.
@nyo16
nyo16 merged commit 227ebb3 into master Nov 14, 2026
9 checks passed
@nyo16
nyo16 deleted the fix-mtp-cancel-race branch November 14, 2026 11:50
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.

1 participant