[PR #7] [MERGED] serve: fix zero-token replies from engine-queue race between requests #2409

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/7
Author: @coolio986
Created: 9/25/2026
Status: ✅ Merged
Merged: 9/26/2026
Merged by: @Niko1221

Base: main ← Head: fix/engine-queue-race


📝 Commits (1)

  • dd03276 serve: fix zero-token replies from engine-queue race between requests

📊 Changes

1 file changed (+56 additions, -18 deletions)

View changed files

📝 serve/server.py (+56 -18)

📄 Description

Summary

Fixes blank/empty responses that appeared when a client (e.g. opencode) has two requests in flight at once — a title-generation request alongside the main tool-calling turn. The second request frequently returned zero tokens: the server logged done: 0 tokens in 0 s (length, cancel=False) and the client showed an empty reply.

Root cause

StrataEngine multiplexes every request over one resident strata --serve subprocess and a single shared line queue (self.lines), with no per-request framing. In Service.run(), a stop token (<|im_end|>) breaks the token loop with the engine generator still suspended and its internal done flag False. That generator's cleanup — write STOP, then drain self.lines until DONE — therefore ran only when run()'s frame was torn down later, after the with self.fifo lock had been released. By then the next request held the lock and had issued its own GEN; the prior request's drain raced it on the shared queue and consumed the new request's DONE. The new request saw an immediate end-of-stream: 0 tokens, finish left at its default "length".

Fix

Close the engine generator inside the with self.fifo block via try/finally, so a request's STOP+drain completes while it still holds the lock and cannot leave stray lines on the shared queue for the next request to misread as its own DONE.

Also included:

  • Clamp a non-positive max_new to the default (1024) in both /v1/chat/completions and /v1/messages. Some clients send max_tokens: -1 ("unlimited"); that value passed straight through and GEN -1 made the engine yield 0 tokens.
  • Distinguish a client disconnect: catch GeneratorExit and set finish="disconnect" instead of leaving it as "length".
  • Diagnostics gated on STRATA_DEBUG (off by default): a per-request _debug_req line and a raw: dump of decoded model output at completion.
  • Progress reporting: add tok/s and cancel state to the done: line; emit the progress line every 1 s instead of 15 s.

Test plan

  • python -c "import ast; ast.parse(open('serve/server.py').read())" passes.
  • Mock engine (--engine mock): 2 concurrent streaming + sequential streaming + non-stream OpenAI + Anthropic requests all return content with correct finish reason; no 0 tokens (length) regressions.
  • Real engine (STRATA_DEBUG=1 ./run-iq3_xxs.sh): full opencode agentic loop (title-gen, tool calls, file write/edit, test run, final answer) completes with every turn producing tokens; the previously-blank tools=11 main turn now streams normally.

🔄 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/7 **Author:** [@coolio986](https://github.com/coolio986) **Created:** 9/25/2026 **Status:** ✅ Merged **Merged:** 9/26/2026 **Merged by:** [@Niko1221](https://github.com/Niko1221) **Base:** `main` ← **Head:** `fix/engine-queue-race` --- ### 📝 Commits (1) - [`dd03276`](https://github.com/Niko1221/Strata/commit/dd03276deaa9775ad834a4a9819db15c2e97137c) serve: fix zero-token replies from engine-queue race between requests ### 📊 Changes **1 file changed** (+56 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+56 -18) </details> ### 📄 Description ## Summary Fixes blank/empty responses that appeared when a client (e.g. opencode) has two requests in flight at once — a title-generation request alongside the main tool-calling turn. The second request frequently returned zero tokens: the server logged `done: 0 tokens in 0 s (length, cancel=False)` and the client showed an empty reply. ### Root cause `StrataEngine` multiplexes every request over one resident `strata --serve` subprocess and a single shared line queue (`self.lines`), with no per-request framing. In `Service.run()`, a stop token (`<|im_end|>`) breaks the token loop with the engine generator still suspended and its internal `done` flag `False`. That generator's cleanup — write `STOP`, then drain `self.lines` until `DONE` — therefore ran only when `run()`'s frame was torn down later, **after** the `with self.fifo` lock had been released. By then the next request held the lock and had issued its own `GEN`; the prior request's drain raced it on the shared queue and consumed the **new** request's `DONE`. The new request saw an immediate end-of-stream: 0 tokens, `finish` left at its default `"length"`. ### Fix Close the engine generator **inside** the `with self.fifo` block via `try/finally`, so a request's `STOP`+drain completes while it still holds the lock and cannot leave stray lines on the shared queue for the next request to misread as its own `DONE`. Also included: - Clamp a non-positive `max_new` to the default (1024) in both `/v1/chat/completions` and `/v1/messages`. Some clients send `max_tokens: -1` ("unlimited"); that value passed straight through and `GEN -1` made the engine yield 0 tokens. - Distinguish a client disconnect: catch `GeneratorExit` and set `finish="disconnect"` instead of leaving it as `"length"`. - Diagnostics gated on `STRATA_DEBUG` (off by default): a per-request `_debug_req` line and a `raw:` dump of decoded model output at completion. - Progress reporting: add tok/s and cancel state to the `done:` line; emit the progress line every 1 s instead of 15 s. ## Test plan - [x] `python -c "import ast; ast.parse(open('serve/server.py').read())"` passes. - [x] Mock engine (`--engine mock`): 2 concurrent streaming + sequential streaming + non-stream OpenAI + Anthropic requests all return content with correct finish reason; no `0 tokens (length)` regressions. - [x] Real engine (`STRATA_DEBUG=1 ./run-iq3_xxs.sh`): full opencode agentic loop (title-gen, tool calls, file write/edit, test run, final answer) completes with every turn producing tokens; the previously-blank `tools=11` main turn now streams normally. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 01:59:54 +07:00
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#2409
No description provided.