[PR #3686] [Engine] Metal: honor a client cancel during the prompt read (stop at the next chunk) #4750

Open
opened 2026-11-07 15:38:49 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3686
Author: @ipriyaaanshu
Created: 10/22/2026
Status: 🔄 Open

Base: main ← Head: fix/cancel-prompt-read-metal-b


📝 Commits (1)

  • 00a0697 Metal: honor a client cancel during the prompt read

📊 Changes

1 file changed (+12 additions, -1 deletions)

View changed files

📝 src/program/generate.cpp (+12 -1)

📄 Description

Problem

On Metal, a client cancel mid-prefill is honored only after the engine reads the whole prompt: the response is dead, but any next request queues behind the read for the remainder (issues: the repro in the linked issue; also the shape of #962's "cancel is ignored" reports).

Root cause

Prefill::run on STRATA_GPU_METAL reads the whole prompt in one metal_engine::prompt_run call, so the should_stop chunk loop in prefill.cpp only runs on the CUDA-style backends. Window::prompt does chunk the read (STRATA_METAL_PROMPT_CHUNK, default 2048) and aborts cleanly when its on_chunk returns false — but nothing returned false on a cancel.

Fix (4 lines + comments)

  • the stdin reader's STOP handler also sets a file-scope atomic g_prompt_stop_req (the reader thread and the in-shared stop_req live below where the E-9 on_chunk lambda needs the flag);
  • the prompt-read's on_chunk lambda checks it and stops the read at the next chunk with e = "cancelled";
  • it is cleared per request together with stop_req (a STOP that arrived between requests is stale — without this the next request's read aborts instantly).

Validation

Environment: Apple M4 Pro (MPS/Metal), macOS 27.0.1, branch built from upstream/main (local revision 00a06970 on this PR's HEAD), pack Qwen3.8-Flash-Next-GSQ-RCO-Q2_0, served through serve/server.py to :8098 (--prefill 2048 --spec 2 --max-context 65536), prod instance untouched.

  • Defect repro on the release asset (v0.1.45.3, rev 02a8193da): 49k-token prompt, client disconnect at 8 s → engine log 49192 of 49197 read in 213567 ms (230.3 tok/s), 0 generated (cancelled); a tiny request sent right after waited 207 s. Same shape on a local build of this base (cancel ignored).
  • After the fix: fresh ~13.4k prompt (13407-13409 tokens), client disconnect at 8-12 s → 0 of 13409 read ... (cancelled) (with --prefill 2048) and 1024 of 13407 / 3072 of 13408 read (cancelled) with --prompt-cache-every 1024; the tiny request right after every cancel served in 2.5-15 s (2.5-6 s at the 1024 cadence).
  • Durability: 3 cancels in a row (each followed by a normal request) — no fatal, no engine exit (the v0.1.28 "cancelled" poison stays fixed); full reads and 40-token temp-0 decodes right after cancels serve normally.

The leftover work between the cancel and the stop is at most one chunk (--prefill / STRATA_METAL_PROMPT_CHUNK tokens — seconds) instead of the remaining prompt (minutes at 262K).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/3686 **Author:** [@ipriyaaanshu](https://github.com/ipriyaaanshu) **Created:** 10/22/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/cancel-prompt-read-metal-b` --- ### 📝 Commits (1) - [`00a0697`](https://github.com/Niko1221/Strata/commit/00a06970160ec60fab7e09c29fd86c7a056751a4) Metal: honor a client cancel during the prompt read ### 📊 Changes **1 file changed** (+12 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/program/generate.cpp` (+12 -1) </details> ### 📄 Description ## Problem On Metal, a client cancel mid-prefill is honored only after the engine reads the whole prompt: the response is dead, but any next request queues behind the read for the remainder (issues: the repro in the linked issue; also the shape of #962's "cancel is ignored" reports). ## Root cause `Prefill::run` on `STRATA_GPU_METAL` reads the whole prompt in one `metal_engine::prompt_run` call, so the `should_stop` chunk loop in `prefill.cpp` only runs on the CUDA-style backends. `Window::prompt` does chunk the read (`STRATA_METAL_PROMPT_CHUNK`, default 2048) and aborts cleanly when its `on_chunk` returns `false` — but nothing returned false on a cancel. ## Fix (4 lines + comments) - the stdin reader's `STOP` handler also sets a file-scope atomic `g_prompt_stop_req` (the reader thread and the in-shared `stop_req` live below where the E-9 `on_chunk` lambda needs the flag); - the prompt-read's `on_chunk` lambda checks it and stops the read at the next chunk with `e = "cancelled"`; - it is cleared per request together with `stop_req` (a STOP that arrived between requests is stale — without this the next request's read aborts instantly). ## Validation Environment: Apple M4 Pro (MPS/Metal), macOS 27.0.1, branch built from `upstream/main` (local revision `00a06970` on this PR's HEAD), pack Qwen3.8-Flash-Next-GSQ-RCO-Q2_0, served through `serve/server.py` to :8098 (`--prefill 2048 --spec 2 --max-context 65536`), prod instance untouched. - Defect repro on the release asset (v0.1.45.3, rev `02a8193da`): 49k-token prompt, client disconnect at 8 s → engine log `49192 of 49197 read in 213567 ms (230.3 tok/s), 0 generated (cancelled)`; a tiny request sent right after waited **207 s**. Same shape on a local build of this base (cancel ignored). - After the fix: fresh ~13.4k prompt (13407-13409 tokens), client disconnect at 8-12 s → `0 of 13409 read ... (cancelled)` (with `--prefill 2048`) and `1024 of 13407` / `3072 of 13408 read (cancelled)` with `--prompt-cache-every 1024`; the tiny request right after every cancel served in **2.5-15 s** (2.5-6 s at the 1024 cadence). - Durability: 3 cancels in a row (each followed by a normal request) — no fatal, no engine exit (the v0.1.28 "cancelled" poison stays fixed); full reads and 40-token temp-0 decodes right after cancels serve normally. The leftover work between the cancel and the stop is at most one chunk (`--prefill` / `STRATA_METAL_PROMPT_CHUNK` tokens — seconds) instead of the remaining prompt (minutes at 262K). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4750
No description provided.