ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

Server does not abort prefill when client disconnects

Open TipKnuckle opened this issue 6 months ago • 1 comments

Problem

When a client disconnects mid-generation (timeout, user cancel, agent restart), the server detects the broken stream but continues the full prefill to completion before acting on it. On long-context sessions this means several minutes of wasted GPU work before the server is available again.

What happens

The progress callback (server_progress_cb) sets stream_failed = true when a keepalive write fails, but this flag is only checked after ds4_session_sync() returns — there's no path to signal the prefill loop to stop early. The loop in metal_graph_prefill_chunked_range only bails on a Metal GPU error, not on client state.

And it gets worse when the disconnected client reconnects and retries with a slightly different prompt (e.g. one extra token from a tool result), the token mismatch triggers another full prefill from zero immediately after the first one finishes.

Example log of issue

Client disconnected partway through generation. Server finished the full 67580-token prefill (~257 seconds), then started over on the retry:

0602 19:15:59 ds4-server: chat ctx=0..67580:67580 TOOLS prompt done 256.984s
0602 19:15:59 ds4-server: chat ctx=0..67580:67580 TOOLS stream closed during prefill
0602 19:15:59 ds4-server: live kv cache miss live=67580 prompt=67609 common=67527 reason=token-mismatch
0602 19:15:59 ds4-server: chat ctx=0..67609:67609 TOOLS prompt start
0602 19:15:59 ds4-server: chat ctx=0..67609:67609 TOOLS prefill chunk 0/67609 (0.0%) ...

Cause

ds4_session_progress_fn is typedef void (*)(void*, const char*, int, int) — the callback returns void, so there's no way to signal abort back to the prefill loop. stream_failed lives in the server layer and is invisible to ds4.c.

Possible fix

Two approaches:

Option A — cancel flag pointer (smaller diff) Add a volatile bool *cancel_flag to the session (via ds4_session_set_cancel_flag or similar). The server sets it when stream_failed is detected in the progress callback. The chunked prefill loop checks it between 4096-token chunks and returns early if set.

Option B — progress callback returns bool Change ds4_session_progress_fn to return bool (false = cancel). The server callback returns false when stream_failed. Every call site in the prefill loop checks the return value. Cleaner long-term but touches more call sites including ds4_distributed.c.

Option A has a smaller diff and doesn't change the public callback signature.

--

On a Mac Studio M4 Max 128GB, q2-imatrix, cli flags: --ctx 192000 --kv-disk-dir /tmp/ds4-kv --kv-disk-space-mb 16384

TipKnuckle avatar Jun 03 '26 02:06 TipKnuckle

Corroborating this with a longer-context reproduction, plus one amplification path I don't think is described here yet.

Setup: ds4-server on Apple M5 Max 128GB (Metal), single slot, ~202k-token session arriving through an Anthropic-API proxy. Client is Claude Code, whose stream watchdog has a fixed deadline (600s at the time) that SSE comment lines do not reset.

What we measured — 2026-07-14, 85 consecutive minutes on one node:

15:17:52 → 15:34:28  ctx=20480..202634:182154  prefill 944.1s  finish=tool_calls gen=928
15:35:47 → 15:52:14  ctx=20480..202634:182154  prefill 937.5s  finish=error "client stream write failed"
15:52:18 → 16:08:39  ctx=20480..202639:182159  prefill 934.8s  finish=tool_calls gen=838
16:09:16 → 16:25:43  ctx=20480..202645:182165  prefill 934.0s  finish=tool_calls gen=971
16:25:46 → 16:42:08  ctx=20480..202645:182165  prefill 931.9s  finish=error "client stream write failed"

Five full prefills of the same conversation, back to back. The client had already disconnected in every case — its deadline is ~600s and each prefill needs ~935s. The user received zero tokens across six attempts spanning 2.5 hours.

Three things to add to the report:

1. The wasted work is worse than wasted GPU time — the answers were finished. Three of the five runs reached finish=tool_calls and generated 928 / 838 / 971 tokens. The server did the work, produced a complete answer, and dropped it because nobody was on the socket. Only the other two surfaced as client stream write failed, so most of the damage leaves no error in the log at all.

2. The retry doesn't just re-prefill — it can't start. Because the abandoned prefill keeps the single slot for another ~5 minutes after the client leaves, retry N+1 queues behind it. On the proxy side, the retry saw no : prefill progress comments for its entire 630s lifetime: it never got the slot, so it was guaranteed to fail before it began. The cost per disconnect isn't one wasted prefill, it's a self-sustaining queue.

3. With a fixed client deadline shorter than the prefill, this never converges. It isn't "several minutes of wasted GPU work before the server is available again" — it's an unbounded loop. It stopped only when the user gave up. Past roughly 130k tokens (600s × our measured prefill rate) the failure stops being intermittent and becomes certain.

This is reachable straight from the README. The recommended Claude Code wrapper sets CLAUDE_STREAM_IDLE_TIMEOUT_MS=600000, and that watchdog is not reset by the : prefill keepalive comments from #222 — only by a real SSE event. So on the documented setup, any session whose cold prefill exceeds 600s can never produce a token, and every retry re-enters the loop described above. We had to move the fleet to 1200000 to get long-context sessions working at all; with #333 fixed the documented 600000 would at least fail cheaply (slot released in seconds) instead of burning the node.

Collateral: unrelated requests needing 30–76s of actual work took 910s and 1445s wall-clock, queued behind the zombies.

On the fix: Option A (cancel flag checked between chunks) looks sufficient for all of the above, and the granularity is better than it might sound — our logs show 1024-token prefill chunks landing every ~3–5s (220–340 tok/s on M5 Max), so a per-chunk check would release the slot within about 5 seconds:

0714 15:00:01 ds4-server: chat ctx=0..81249:81249 TOOLS prefill chunk 11264/81249 (13.9%) chunk=314.96 t/s avg=343.45 t/s 32.797s
0714 15:00:05 ds4-server: chat ctx=0..81249:81249 TOOLS prefill chunk 12288/81249 (15.1%) chunk=319.31 t/s avg=341.30 t/s 36.004s

This also compounds with #62: once the retry finally runs, the token mismatch means it re-prefills from the same stuck 20480-token prefix, which is why all five runs above are byte-identical in size.

Happy to supply fuller logs or test a patch on the M5 Max box.

kandaman avatar Jul 16 '26 17:07 kandaman