mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[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
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#1880
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.fifoinserve/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.fifois a plainthreading.Lock, and waiting requests poll it with short timeouts (0.1 s in_inference_fifo, 0.25 s in_turn,QUEUE_BEAT_Sin the #1619 keep-alive loop). Python'sLockmakes no promise about order, so whichever waiter polls right after a release gets the turn.Why it's worth solving
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:
STRATA_ADMIT_SHORT_FIRSTis today. Solo is one slot. Batch becomes a router policy (admit when a slot frees) rather than a separate path.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:
/v1/vram, slot save/restore) would become tasks in the same queue.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
fifowith a ticket lock that keeps each waiter's place in line, and addsqueue #<n> waited <x> sto thedonelog 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:
I'm happy to share it as a PR if it's useful in the meantime.
Environment
serve/at4b9e05a2(v0.2.6), engine 0.1.50-metal, built with PR #3231 (--conversation-cache-mib 8192)--spec 2 --mtp, solo (no--batch),STRATA_METAL_STREAM=0Related: #1619, #1999, #1317, #2275 (the multi-user direction; that one is about batch throughput, this one about ordering and client isolation).
@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