Skip to content

fix: the CUDA server stops a request when its client has gone or its per-token work fails - #57

Closed
nood-co1 wants to merge 3 commits into
ashhart:mainfrom
blockbrain-ai:contrib/cuda-server-client-gone
Closed

nood-co1 wants to merge 3 commits into
ashhart:mainfrom
blockbrain-ai:contrib/cuda-server-client-gone

Conversation

@nood-co1

@nood-co1 nood-co1 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Issue #26 (client disconnects do not cancel queued or running jobs) was fixed for the Mac server in 0.3.5. This PR does the same for the CUDA server, and keeps failures in the per-token callback out of the engine.

  • A running request, streamed or not, stops at its next round once its client has gone. Nothing more is written to that client.
  • A request whose client left while it waited for the server lock does not start: no prefill, no engine call.
  • The per-token callback never raises into the engine. A failure there stops the engine and is raised after generate returns.
  • docs/api.md describes CUDA concurrency as the recipe and the runbook do, and says what a disconnect does.

Why

In src/tensorfold/cuda/server.py at main (34bae79):

  • Non-streamed requests never stop. They run with emit = lambda delta: True (:519). With the defaults (--max-tokens 4096, cli.py:40; --parallel auto, one request at a time on CUDA, cli.py:329), an abandoned non-streamed request holds the lock for its whole reply, and every request behind it waits.
  • Streams stop only on a failed write. on_tokens (:376-389) stops only when emit fails, and emit runs only on rounds that add visible text (:387). Inside a hidden tool call nothing is written, so nothing stops.
  • Queued requests still run. The lock is taken and generate is called (:393-395) whatever happened to the client while the request waited.
  • Other write errors raise into the engine. emit catches only BrokenPipeError and ConnectionResetError (:482). A timed-out or unreachable client (ETIMEDOUT, EHOSTUNREACH) raises through on_tokens into the decode loop. So does any failure in the callback's own work (StreamDecoder.add, the reasoning split, tool-call hiding).

An exception from on_tokens does more than fail one request:

  • Two ranks (from reading the code; not run on two ranks). Rank 0 leaves decode_tp from its callback (families/qwen3_5/cuda/decode_tp.py:237-238) without broadcasting the empty window that stops rank 1 (:174-198). Rank 1 is still in its decode loop, so it reads rank 0's next request header (families/qwen3_5/cuda/engine.py:160-161) as a window. GLM's callbacks on rank 0 have the same shape (families/glm5_next/cuda/engine.py:298; decode.py:324, :387, :444).
  • Shared rounds (--parallel N). Scheduler.submit runs the callback on the request's thread (cuda/scheduler.py:33-34). The exception leaves submit, and the stream stays in the decoder with nothing to stop it. On the host, with a stand-in decoder on the real Scheduler, a stream whose callback failed at its third round decoded on to its count: 400 of 400 tokens.

The Mac server already checks the socket for this (server/cancellation.py:30-46, used at server/http.py:185-186).

Change

src/tensorfold/cuda/server.py:

  • App.run(..., cancelled=None) takes a callable that is true once the client has gone. do_POST passes the Mac server's check, socket_cancellation(self.connection), for streamed and non-streamed requests.
  • on_tokens checks cancelled() every round, whether or not the round wrote text, and returns True to stop the engine.
  • The callback's body runs under try/except Exception. The first failure is kept and the callback returns True. After a stop or a failure it returns True at once, without decoding or writing more. This matters for the engines that keep calling after a stop (two-rank Flash Next, Nemotron and GLM).
  • After the lock is taken (and before generate, for a concurrent engine), a request whose client has gone raises RequestCancelled. The handler closes the connection without writing more.
  • After generate returns, a kept failure is raised and goes through the handler's existing error path.
  • After generate returns, a request stopped for its client raises RequestCancelled: no final chunk, no [DONE], no JSON reply. The Mac server's handler does the same (server/http.py:353-354, :435-436).
  • emit treats any OSError as a client that has gone. The JSON reply (_json) ignores BrokenPipeError and ConnectionResetError.

src/tensorfold/families/glm5_next/cuda/app.py: GlmApp.run passes cancelled through.

Docs:

  • docs/api.md:45 said "CUDA serializes generation through its server lock". With --parallel N above one, the 27B (one or two ranks) and Flash Next (one rank) share rounds and do not take that lock (cuda/server.py:393, docs/recipes/cuda.md:44-47, RUNBOOK.md:71-73). The paragraph now says so, and adds one sentence on disconnects.
  • docs/recipes/cuda.md:55 names Nemotron among the engines that finish on both ranks (families/nemotron_h/cuda/app.py:231).

Behaviour notes:

  • The check is the Mac server's, so clients are treated the same way. A client that shuts its sending side while it waits counts as gone. A pipelined next request on a kept-alive connection does not.
  • A client that has shut its sending side and still reads gets no reply (non-streamed), or a stream with no end chunk and no [DONE]. On main it gets the whole reply.
  • Not covered: a request waiting inside the concurrent Scheduler before admission. It is admitted and prefilled, then stops after its first round. Skipping it needs a hook in the scheduler.

Exactness

No engine file and no arithmetic change. Engines stop through the path a failed streamed write already takes:

  • 27B, one rank: engine.py:147-150, decode.py:190, :252-253.
  • 27B, two ranks: engine.py:171-173, decode_tp.py:175, :237-238.
  • Shared rounds: scheduler.py:33-34.

Tokens sent before a stop are unchanged.

A skipped request makes no engine call. Every engine hands a request to rank 1 only inside generate (27B: families/qwen3_5/cuda/engine.py:160-161; GLM: families/glm5_next/cuda/engine.py:348-349; Flash Next: families/qwen4_exp/cuda/engine.py:263; Nemotron: families/nemotron_h/cuda/app.py:227). A request skipped on rank 0 therefore never reaches rank 1, and the collective sequence stays aligned.

Tests

Host: macOS, Python 3.13.5, PyTorch 2.11.0 CPU, an import-only stand-in for Triton (no kernel launches). No model ran.

tests/test_cuda_server_disconnect.py (new, 17 cases) serves the real Server and handler on 127.0.0.1:0 with a paced fake engine, a character tokenizer and a small template, as tests/test_cuda_request_policy.py does. Every socket, join and wait has a timeout. The cases:

  • A client that leaves mid-reply stops the engine within two rounds: non-streamed, streamed, and streamed inside a hidden tool call.
  • A client that shuts its sending side mid-reply and reads on gets no reply that looks whole.
  • A connected client still gets the whole reply, streamed and non-streamed, and a kept-alive connection serves a second request.
  • A request queued behind the lock whose client left never reaches generate, streamed or not, and the next request is served.
  • App.run and GlmApp.run with cancelled already true make no engine call and release the lock, for serialized and concurrent engines.
  • A stream write failing with OSError(ETIMEDOUT) stops the engine without raising into it.
  • A per-token failure stops the engine and generate returns normally, for an engine that stops and for one that keeps calling to the end. The failure is raised afterwards, and nothing is sent after it.
  • The same failure on the real Scheduler with a stand-in decoder ends the shared-round stream a round or two later.
Run Result
New file on main's sources 14 failed, 3 passed
New file on this branch 17 passed; 25 of 25 repeated runs passed
Each part of the fix removed on its own at least one case fails for every part except the _json guard, which no case reaches
Whole host suite (tests without tests/cuda), main (34bae79) 133 failed, 1,319 passed, 298 skipped, 4 xfailed, 18 errors
Same, this branch merged into main (34bae79) 133 failed, 1,336 passed, 298 skipped, 4 xfailed, 18 errors

The same 151 ids fail or error on both, for host reasons: no transformers tokenizer, no Triton backends, MLX 0.31.1 where GLM-5.3-Flash needs 0.32.2, and two tests that read tools/ and CHANGELOG.md, which the test tree used here did not include. None is a CUDA server test.

tests/cuda/test_qwen27_client_gone.py (new, GPU) drives the 27B decoders with the synthetic weights of tests/cuda/test_qwen27_multi.py through App.run:

  • One stream: a client that leaves at its third check gets a prefix of the exact reply and RequestCancelled. A longer prompt then resumes from its kept prompt end with a fresh prefill's tokens. A request whose callback fails raises, and the next one is exact.
  • Shared rounds, three streams at once: one client leaves, one callback fails, one runs to the end. The survivor and a request after them keep their serial tokens, and the decoder ends empty.

Its harness (helpers, App.run, the real Scheduler) ran on the host with stand-in decoders. The 27B decoders did not.

GPU status

One DGX Spark (GB10, sm_121); container: Python 3.12.3, PyTorch 2.13.0a0 (NVIDIA 26.07), CUDA 13.3, Triton 3.7.1. TORCH_CUDA_ARCH_LIST=12.1 was set: without it the 0.3.5 extensions do not build in this container (the failure #56 reports, fixed on main in 0.3.6.1). Branch head 18d4f4d.

Run Result
tests/cuda/test_qwen27_client_gone.py 2 passed
tests/cuda 290 passed, 7 skipped; main (beddbb7): 288 passed, the same 7 skipped

Still pending:

  1. A served 27B on one GPU (default --parallel auto): a non-streamed request with max_tokens 4096 whose client closes after 1 s. The next request should start within a round, and the log should show no traceback.
  2. The same on two ranks, then one more request whose token_sha equals a solo run's.

Evidence

Surface: macOS, a stand-in engine at one token every 10 ms, the real CUDA App and handler, and the Qwen3.8-27B checkpoint's tokenizer and chat template; three trials per case. No model ran.

Case main (beddbb7) this branch
Streamed, client leaves after 0.2 s stopped at 14, 16, 15 of 300 tokens stopped at 15, 17, 15 of 300
Non-streamed, client leaves after 0.2 s ran 300 of 300 (3 of 3) stopped at 17, 17, 15 of 300
Streamed, queued; client leaves while it waits generate called (3 of 3); stopped after 1 of 100 tokens generate not called (0 of 3)
Non-streamed, queued the same way generate called (3 of 3); ran 100 of 100 generate not called (0 of 3)
Non-streamed, client shuts its sending side after 0.2 s and reads on 300 of 300; a 200 reply (3 of 3) stopped at 15, 18, 17 of 300; no reply, connection closed (3 of 3)
Streamed, the same 300 of 300; end chunk and [DONE] (3 of 3) stopped at 17, 17, 17 of 300; no end chunk, no [DONE] (3 of 3)

The two-rank misalignment after a callback exception is from reading the code. It was not run on two ranks.

Overlaps


Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).

…per-token work fails

The CUDA server stopped a request only when a streamed write failed with a broken pipe or a reset. A
non-streamed request always ran to its reply limit, a stream stopped only at a round that wrote text (not
inside a hidden tool call), and a request queued behind the server lock was prefilled and decoded after its
client had left. A write that timed out, or any other failure in the per-token callback, raised into the
engine's decode loop; with two ranks that skips the stop that keeps rank 1 in step.

- App.run takes cancelled=; the handler passes the Mac server's socket check (socket_cancellation) for
  streamed and non-streamed requests. The callback checks it every round, with or without new text, and
  asks the engine to stop.
- A request whose client left while it waited for the lock raises RequestCancelled before generate: no
  prefill, no engine call, nothing written.
- A request stopped for its client raises RequestCancelled once generate returns, as on the Mac server: no
  final chunk, no [DONE], no JSON reply. A client that has only shut its sending side does not get the
  stopped prefix as a whole reply with finish_reason "length".
- The callback never raises into the engine. A failure is kept, the engine is asked to stop, and the
  failure is raised after generate returns. After a stop or a failure the callback returns True at once, for
  the engines that finish on both ranks and keep calling.
- A streamed write treats any OSError (timed out, host unreachable) as a client that has gone; the JSON
  reply ignores a broken pipe or a reset.

Engines stop through the path a failed streamed write already takes. No engine or arithmetic change;
tokens sent before a stop are unchanged. Not covered: a request waiting inside the concurrent scheduler
before admission is prefilled, then stops after its first round.

Test: tests/test_cuda_server_disconnect.py (17 cases; 14 fail without the fix).
docs/api.md said the CUDA server serializes generation through its lock; with --parallel N the 27B (one or
two ranks) and Flash Next (one rank) share rounds and skip it, as the CUDA recipe and the runbook say. It now
says so, and what a disconnect does on CUDA. The recipe's note on engines that finish on both ranks names
Nemotron too, which decodes to the end on two ranks whatever the callback returns.
Synthetic 27B weights, one stream and shared rounds: a request whose client leaves mid-reply sends a prefix
of the exact reply and ends in RequestCancelled; a failing per-token callback raises after generate; the
requests beside it and after it, and a resume from the stopped request's prompt end, keep their exact
tokens. Not yet run on a GPU.
@ashhart

ashhart commented Sep 28, 2026

Copy link
Copy Markdown
Owner

Thank you, @nood-co1. Before this, a CUDA request whose client had gone kept its turn on the GPU when
nothing was being written to the socket, and an error in the per-token path could land inside the engine.
Your fix reuses the Mac server's socket check, so both servers now handle a vanished client the same way,
and your tests cover the one-stream and shared-round paths.

We rebased it onto next-0.3.6.2 with your three commits unchanged, plus one small style commit that puts a
docstring and a comment on one line each (a house rule for this tree). On a DGX Spark with Qwen3.8-27B,
drafted replies still equal serial ones and the four decode cells are within 1% of the base. The clearest
case is a non-streamed request whose client leaves: before, the abandoned 1,500-token reply ran to the end
and the next request waited 37.2 s; with your change it stops within a round and the next request waited
1.0 s. It ships in 0.3.6.2: 24afe5ee, 740811ab, ce09822b. Thanks for the careful work, and we'd be glad to see more from you.

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