[GH-ISSUE #310] --parallel: per-request timings and cached_tokens can come from another request (shared StrataEngine.last) #117

Closed
opened 2026-10-29 21:21:05 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @pepuscz on GitHub (Oct 20, 2026).
Original GitHub issue: https://github.com/mw00/project-maya/issues/310

Summary

With --parallel N, every request's DONE line is parsed into one shared field, StrataEngine.last. Service.run reads that field after its own stream ends. If another request's DONE arrives in between, the response can carry the other request's figures:

  • timings (prompt_ms, predicted_ms and the per-second rates);
  • usage.prompt_tokens_details.cached_tokens.

The /metrics request history and /v1/status last_timings get the same mix-up.

Where (v1.1.17)

  • StrataEngine._parse_done writes self.last: serve/server.py#L634-L641
  • _generate_multi calls it when this request's DONE arrives: #L921, #L939
  • Service.run reads self.engine.last in its finally (#L2240) and builds timings from it (#L2265-L2268). cached_tokens comes from those timings (#L2285, #L2502).
  • The check against engine_last0 (#L2099) shows that some DONE arrived, not that it was this request's.

Why it matters

Under --parallel, requests decode in lockstep and often finish within a step of each other. In our four-request runs (GLM-5.3-Flash Maya-S24, --parallel 4), the four glm multi: request N done lines landed within about 0.2 s. A client that reports speed from timings (the llama.cpp-style fields), or that checks cached_tokens to confirm prompt reuse, can then show another request's numbers.

We did not catch a wrong figure in practice. We gave each of four concurrent requests a different expected cache reuse, and the reported values were right each time. We found this while reading the code to interpret those runs.

Suggested fix

Keep the DONE figures with the request. For example, _generate_multi could store the parsed dict on the request's thread (self._tls.last) or pass it back through the request's route. Service.run would then use that when the engine runs several requests.

A test with the mock engine at multi = 2 would cover it: two requests finishing back to back with different reused values.

Originally created by @pepuscz on GitHub (Oct 20, 2026). Original GitHub issue: https://github.com/mw00/project-maya/issues/310 ## Summary With `--parallel N`, every request's `DONE` line is parsed into one shared field, `StrataEngine.last`. `Service.run` reads that field after its own stream ends. If another request's `DONE` arrives in between, the response can carry the other request's figures: - `timings` (`prompt_ms`, `predicted_ms` and the per-second rates); - `usage.prompt_tokens_details.cached_tokens`. The `/metrics` request history and `/v1/status` `last_timings` get the same mix-up. ## Where (v1.1.17) - `StrataEngine._parse_done` writes `self.last`: [serve/server.py#L634-L641](https://github.com/mw00/project-maya/blob/v1.1.17/serve/server.py#L634-L641) - `_generate_multi` calls it when this request's `DONE` arrives: [#L921](https://github.com/mw00/project-maya/blob/v1.1.17/serve/server.py#L921), [#L939](https://github.com/mw00/project-maya/blob/v1.1.17/serve/server.py#L939) - `Service.run` reads `self.engine.last` in its `finally` ([#L2240](https://github.com/mw00/project-maya/blob/v1.1.17/serve/server.py#L2240)) and builds `timings` from it ([#L2265-L2268](https://github.com/mw00/project-maya/blob/v1.1.17/serve/server.py#L2265-L2268)). `cached_tokens` comes from those timings ([#L2285](https://github.com/mw00/project-maya/blob/v1.1.17/serve/server.py#L2285), [#L2502](https://github.com/mw00/project-maya/blob/v1.1.17/serve/server.py#L2502)). - The check against `engine_last0` ([#L2099](https://github.com/mw00/project-maya/blob/v1.1.17/serve/server.py#L2099)) shows that *some* `DONE` arrived, not that it was this request's. ## Why it matters Under `--parallel`, requests decode in lockstep and often finish within a step of each other. In our four-request runs (GLM-5.3-Flash Maya-S24, `--parallel 4`), the four `glm multi: request N done` lines landed within about 0.2 s. A client that reports speed from `timings` (the llama.cpp-style fields), or that checks `cached_tokens` to confirm prompt reuse, can then show another request's numbers. We did not catch a wrong figure in practice. We gave each of four concurrent requests a different expected cache reuse, and the reported values were right each time. We found this while reading the code to interpret those runs. ## Suggested fix Keep the `DONE` figures with the request. For example, `_generate_multi` could store the parsed dict on the request's thread (`self._tls.last`) or pass it back through the request's route. `Service.run` would then use that when the engine runs several requests. A test with the mock engine at `multi = 2` would cover it: two requests finishing back to back with different `reused` values.
Author
Owner

@mw00 commented on GitHub (Oct 21, 2026):

Thanks @pepuscz - a careful read. Fixed in v1.1.20 as you suggested: each request keeps its own DONE figures when the engine runs several conversations, so timings and cached_tokens are always the request's own (with a test of three concurrent requests of different lengths).

<!-- gh-comment-id:6310619842 --> @mw00 commented on GitHub (Oct 21, 2026): Thanks @pepuscz - a careful read. Fixed in v1.1.20 as you suggested: each request keeps its own DONE figures when the engine runs several conversations, so `timings` and `cached_tokens` are always the request's own (with a test of three concurrent requests of different lengths).
Sign in to join this conversation.
No labels
pull-request
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
mw00/project-maya#117
No description provided.