[GH-ISSUE #4271] [Engine] serve: a client that stops reading can block the engine for everyone, and waiting requests are not served in arrival order #1880

Closed
opened 2026-11-04 01:54:59 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @MikeLippold on GitHub (Oct 26, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/4271

Background

I run Strata on a Mac Studio (M5 Max, 64 GB). The setup I wanted was one interactive main agent that I talk to, with a few subagents doing background work on the same engine. I wasn't after more total speed. I wanted the main agent to stay usable while the subagents worked.

Since the Mac has no batching, I expected the requests to take turns, and they do. Each subagent sends a request, runs its tools, then sends the next one. While one subagent runs its tools, another request gets the engine. With PR #3231's conversation cache, switching between conversations is cheap, so this works surprisingly well.

While measuring how long the main agent waits in this setup, we noticed that the order requests were served in didn't match the order they arrived. Looking into why, we also found potential blocking on the response side: the engine's progress depends on each client reading its replies. We wrote a test to confirm it can happen, and it can: a client that stops reading mid-reply leaves the engine idle and every other request waiting.

It takes a specific situation, though. The client has to stop reading without closing its connection, with a long reply still to come. A crash or Ctrl-C closes the connection, and that case is handled fine. The realistic cases are a remote client that drops off the network mid-reply (a laptop going to sleep, Wi-Fi dropping) or a client process that freezes. We haven't seen it happen in normal use.

So on both ends of the engine, the input side (waiting for a turn) and the output side (sending the reply), blocking can lead to unexpected behaviour. On the output side it can also leave the engine idle, which reduces its overall throughput.

The problem

There are two problems, both on the engine's single turn (Service.fifo in serve/server.py).

1. A client can block the engine

The thread that holds the engine's turn is also the one writing tokens to the client's socket, and the generator only advances after each write returns. The turn is released only when generation ends. So a client that stops reading (a hung process, a stalled network, a full send buffer) holds the engine for as long as it stays stuck. The engine sits idle, and every other request waits behind it. There is no write timeout to end it.

Reproduced locally: client A streams a long reply and stops reading after its first token, while client B sends a short request. Generation froze at 2,946 tokens once the socket buffers filled, and the engine sat idle (busy, nothing generated) until A closed its socket 25 s later. B waited the whole time. With a client that reads normally, generation ran straight through to 4,933 tokens.

When a remote client drops off the network, nothing tells the server it's gone, so the engine waits until the OS gives up on the connection. It's the same kind of problem as #1317.

2. Requests are not served in arrival order

Requests waiting for the engine are not served in the order they arrive. A request can be passed by later ones and wait much longer than it should.

In a test with a 105K-token main conversation and three concurrent subagents, the main agent's request was sent at 92.0 s. Two subagent requests sent after it (at 95.9 s and 99.7 s) got their first tokens first, and the main agent waited 16.6 s.

The cause: Service.fifo is a plain threading.Lock, and waiting requests poll it with short timeouts (0.1 s in _inference_fifo, 0.25 s in _turn, QUEUE_BEAT_S in the #1619 keep-alive loop). Python's Lock makes no promise about order, so whichever waiter polls right after a release gets the turn.

Why it's worth solving

  • Predictability. With several agents sharing one engine, it should be possible to say who goes next. Today the answer depends on thread timing. That makes waits hard to reason about and hard to debug, because the same workload can be served in a different order each time.
  • Fairness. A request that has waited longest should not be passed by newer ones. In an interactive setup, the request being passed is often the one a person is waiting on.
  • Isolation. An inference server that may serve several clients, possibly from several machines, shouldn't let one misbehaving client stop it for everyone. The engine's progress shouldn't depend on any client reading its socket.
  • One routing model. Today solo requests are ordered by the lock, and batch requests by StrataEngine._admit_next. A single place that decides order would serve both.

Proposed solution

Split the work that currently shares one thread into three parts, connected by queues:

flowchart LR
  C[Client layer<br/>HTTP, keep-alives, sending,<br/>disconnect → cancel] -->|task| R[Router<br/>admission policy]
  R --> IQ[(Engine input queue)]
  IQ --> E[Engine worker]
  E --> OQ[(Engine output queue)]
  OQ --> R2[Router<br/>results → request]
  R2 --> C
  • Engine: knows only an input queue and an output queue. It takes the next task, runs it and pushes the results. It never touches a socket and never decides what runs next.
  • Router: decides order and admission. FIFO by default; short-prompt-first could remain an option, as STRATA_ADMIT_SHORT_FIRST is today. Solo is one slot. Batch becomes a router policy (admit when a slot frees) rather than a separate path.
  • Client layer: connection threads submit a task, send keep-alives while it waits (#1619), and write results at whatever pace the client reads. A disconnect sets the cancel flag: the router drops a queued task, and the engine stops a running one (#1999).

Order becomes deterministic because there is one queue and one consumer. The engine can't be stalled by a client because it never writes to one. A stuck client costs at most one reply's worth of memory (bounded by max_tokens) until it disconnects.

Open points for whoever takes this on:

  • The other users of the turn (image encodes, load/unload, /v1/vram, slot save/restore) would become tasks in the same queue.
  • Per-request thread-local state (request_trace, embeddings) would need to travel with the task.

This is an architecture change, so I'm raising it here first per CONTRIBUTING.md.

What we do today

We run a small local patch that replaces fifo with a ticket lock that keeps each waiter's place in line, and adds queue #<n> waited <x> s to the done log line. Rerunning the same test, all 39 requests were served in arrival order, the main agent's longest wait dropped from 16.6 s to 12.4 s, and total time was unchanged (126 s vs 128 s).

It's an interim fix with honest limits:

  • To keep a paused keep-alive from reserving the turn, it skips a waiter that isn't actively waiting at the moment of release, so order is still not strictly guaranteed.
  • It doesn't help with a client that stops reading mid-reply.

I'm happy to share it as a PR if it's useful in the meantime.

Environment

  • Run from source: Strata serve/ at 4b9e05a2 (v0.2.6), engine 0.1.50-metal, built with PR #3231 (--conversation-cache-mib 8192)
  • Mac Studio M5 Max 64 GB, macOS 27
  • Qwen3.8-Flash-Next Q2_0, 262144 context, --spec 2 --mtp, solo (no --batch), STRATA_METAL_STREAM=0
  • Both problems reproduced locally.

Related: #1619, #1999, #1317, #2275 (the multi-user direction; that one is about batch throughput, this one about ordering and client isolation).

Originally created by @MikeLippold on GitHub (Oct 26, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/4271 ## Background I run Strata on a Mac Studio (M5 Max, 64 GB). The setup I wanted was one interactive main agent that I talk to, with a few subagents doing background work on the same engine. I wasn't after more total speed. I wanted the main agent to stay usable while the subagents worked. Since the Mac has no batching, I expected the requests to take turns, and they do. Each subagent sends a request, runs its tools, then sends the next one. While one subagent runs its tools, another request gets the engine. With PR #3231's conversation cache, switching between conversations is cheap, so this works surprisingly well. While measuring how long the main agent waits in this setup, we noticed that the order requests were served in didn't match the order they arrived. Looking into why, we also found potential blocking on the response side: the engine's progress depends on each client reading its replies. We wrote a test to confirm it can happen, and it can: a client that stops reading mid-reply leaves the engine idle and every other request waiting. It takes a specific situation, though. The client has to stop reading without closing its connection, with a long reply still to come. A crash or Ctrl-C closes the connection, and that case is handled fine. The realistic cases are a remote client that drops off the network mid-reply (a laptop going to sleep, Wi-Fi dropping) or a client process that freezes. We haven't seen it happen in normal use. So on both ends of the engine, the input side (waiting for a turn) and the output side (sending the reply), blocking can lead to unexpected behaviour. On the output side it can also leave the engine idle, which reduces its overall throughput. ## The problem There are two problems, both on the engine's single turn (`Service.fifo` in `serve/server.py`). ### 1. A client can block the engine The thread that holds the engine's turn is also the one writing tokens to the client's socket, and the generator only advances after each write returns. The turn is released only when generation ends. So a client that stops reading (a hung process, a stalled network, a full send buffer) holds the engine for as long as it stays stuck. The engine sits idle, and every other request waits behind it. There is no write timeout to end it. Reproduced locally: client A streams a long reply and stops reading after its first token, while client B sends a short request. Generation froze at 2,946 tokens once the socket buffers filled, and the engine sat idle (busy, nothing generated) until A closed its socket 25 s later. B waited the whole time. With a client that reads normally, generation ran straight through to 4,933 tokens. When a remote client drops off the network, nothing tells the server it's gone, so the engine waits until the OS gives up on the connection. It's the same kind of problem as #1317. ### 2. Requests are not served in arrival order Requests waiting for the engine are not served in the order they arrive. A request can be passed by later ones and wait much longer than it should. In a test with a 105K-token main conversation and three concurrent subagents, the main agent's request was sent at 92.0 s. Two subagent requests sent after it (at 95.9 s and 99.7 s) got their first tokens first, and the main agent waited 16.6 s. The cause: `Service.fifo` is a plain `threading.Lock`, and waiting requests poll it with short timeouts (0.1 s in `_inference_fifo`, 0.25 s in `_turn`, `QUEUE_BEAT_S` in the #1619 keep-alive loop). Python's `Lock` makes no promise about order, so whichever waiter polls right after a release gets the turn. ## Why it's worth solving - **Predictability.** With several agents sharing one engine, it should be possible to say who goes next. Today the answer depends on thread timing. That makes waits hard to reason about and hard to debug, because the same workload can be served in a different order each time. - **Fairness.** A request that has waited longest should not be passed by newer ones. In an interactive setup, the request being passed is often the one a person is waiting on. - **Isolation.** An inference server that may serve several clients, possibly from several machines, shouldn't let one misbehaving client stop it for everyone. The engine's progress shouldn't depend on any client reading its socket. - **One routing model.** Today solo requests are ordered by the lock, and batch requests by `StrataEngine._admit_next`. A single place that decides order would serve both. ## Proposed solution Split the work that currently shares one thread into three parts, connected by queues: ```mermaid flowchart LR C[Client layer<br/>HTTP, keep-alives, sending,<br/>disconnect → cancel] -->|task| R[Router<br/>admission policy] R --> IQ[(Engine input queue)] IQ --> E[Engine worker] E --> OQ[(Engine output queue)] OQ --> R2[Router<br/>results → request] R2 --> C ``` - **Engine:** knows only an input queue and an output queue. It takes the next task, runs it and pushes the results. It never touches a socket and never decides what runs next. - **Router:** decides order and admission. FIFO by default; short-prompt-first could remain an option, as `STRATA_ADMIT_SHORT_FIRST` is today. Solo is one slot. Batch becomes a router policy (admit when a slot frees) rather than a separate path. - **Client layer:** connection threads submit a task, send keep-alives while it waits (#1619), and write results at whatever pace the client reads. A disconnect sets the cancel flag: the router drops a queued task, and the engine stops a running one (#1999). Order becomes deterministic because there is one queue and one consumer. The engine can't be stalled by a client because it never writes to one. A stuck client costs at most one reply's worth of memory (bounded by `max_tokens`) until it disconnects. Open points for whoever takes this on: - The other users of the turn (image encodes, load/unload, `/v1/vram`, slot save/restore) would become tasks in the same queue. - Per-request thread-local state (`request_trace`, `embeddings`) would need to travel with the task. This is an architecture change, so I'm raising it here first per CONTRIBUTING.md. ## What we do today We run a small local patch that replaces `fifo` with a ticket lock that keeps each waiter's place in line, and adds `queue #<n> waited <x> s` to the `done` log line. Rerunning the same test, all 39 requests were served in arrival order, the main agent's longest wait dropped from 16.6 s to 12.4 s, and total time was unchanged (126 s vs 128 s). It's an interim fix with honest limits: - To keep a paused keep-alive from reserving the turn, it skips a waiter that isn't actively waiting at the moment of release, so order is still not strictly guaranteed. - It doesn't help with a client that stops reading mid-reply. I'm happy to share it as a PR if it's useful in the meantime. ## Environment - Run from source: Strata `serve/` at 4b9e05a2 (v0.2.6), engine 0.1.50-metal, built with PR #3231 (`--conversation-cache-mib 8192`) - Mac Studio M5 Max 64 GB, macOS 27 - Qwen3.8-Flash-Next Q2_0, 262144 context, `--spec 2 --mtp`, solo (no `--batch`), `STRATA_METAL_STREAM=0` - Both problems reproduced locally. Related: #1619, #1999, #1317, #2275 (the multi-user direction; that one is about batch throughput, this one about ordering and client isolation).
Author
Owner

@Niko1221 commented on GitHub (Nov 5, 2026):

Hello MikeLippold,

Thank you for the detailed write-up. In Strata 0.2.11 a streaming client that stops reading no longer holds the engine's turn: a write that makes no progress for 60 seconds ends that request as a disconnect, and the next request is served. A client that is merely idle while the model thinks is not affected. The second part of your report (waiting requests not served in arrival order) we could not show: a plain lock was never out of order on Linux or Windows in our tests, and your report is from macOS. We have left that part open. If you still see a later request overtaking an earlier one on 0.2.11, a log with the start times of both requests would help.

The Strata project

<!-- gh-comment-id:6658423193 --> @Niko1221 commented on GitHub (Nov 5, 2026): Hello MikeLippold, Thank you for the detailed write-up. In Strata 0.2.11 a streaming client that stops reading no longer holds the engine's turn: a write that makes no progress for 60 seconds ends that request as a disconnect, and the next request is served. A client that is merely idle while the model thinks is not affected. The second part of your report (waiting requests not served in arrival order) we could not show: a plain lock was never out of order on Linux or Windows in our tests, and your report is from macOS. We have left that part open. If you still see a later request overtaking an earlier one on 0.2.11, a log with the start times of both requests would help. The Strata project
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#1880
No description provided.