[PR #5495] [Engine] serve: a picture's encode whose client left no longer holds every request #5468

Open
opened 2026-11-07 21:43:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5495
Author: @Keoian
Created: 11/7/2026
Status: 🔄 Open

Base: main ← Head: serve-vision-cancel-encode


📝 Commits (1)

  • 566a962 [Engine] serve: a picture's encode whose client left no longer holds every request

📊 Changes

3 files changed (+463 additions, -36 deletions)

View changed files

📝 docs/DETAILS.md (+5 -0)
📝 serve/server.py (+212 -36)
➕ serve/test_vision_cancel.py (+246 -0)

📄 Description

Problem and result

If the client leaves while a picture is being read (the web page's Stop, a closed connection), the server stops answering for the rest of that picture's encode. Nothing crashes and nothing is logged.

  • The picture reader (strata-vision, llama.cpp mtmd) cannot stop in the middle of a picture.
  • The encode holds the engine's single turn, so every request waits behind it, text included.
  • #1999 added a cancel check before each picture. The maintainer's reply there names what is left: "a one-picture request that is abandoned mid-encode keeps the encoder busy until that picture is done". On a CPU reader that can be more than a minute.

With this PR, a picture's encode whose client left goes on only while that is useful:

  • Another request waits for the turn (text, another picture, /unload?wait). The reader is killed within 0.1 s of seeing that, started again at once (0.9-1.2 s on the CPU below), and the turn is handed on. The half-written output file is deleted.
  • A waiting request sends the same picture (the follow-up question about it, a retry, Regenerate). The encode goes on and that request uses it, so the picture is not read twice.
  • Nobody waits. The encode goes on and is cached, as before, so a follow-up typed later finds the picture read.
  • A request whose client stays is never stopped.

Measured on 2x Xeon E5-2696 v3 and 2x Tesla P100 16 GB (layer split, power-capped at 150 W). Model: Qwen3.8-Flash-Next (a Q4_K_XL / IQ4_NL pack). The picture reader ran on the CPU ("vision": {"gpu": false, "threads": 16, "max_tokens": 3600}, Q8_0 mmproj). Each case used fresh pictures (a random 6-character code in a white box on a noisy background, so nothing was cached), sent over HTTP and cancelled the way a browser does: the socket was shut down. Times run from sending a request to the end of its answer.

case before after
text sent 0.5 s after a 2560x1440 picture (3600 image tokens, ~80 s encode) was cancelled 5 s in 73.2 s 1.9 s
Stop, resend the same picture, Stop again, then text 69.3 s 1.6 s
text after a cancelled request with two 1600x900 pictures 13.7 s 1.4 s
the same, "stream": false 13.6 s 1.3 s, 1.8 s
the same, /v1/messages 13.4 s 1.3 s
follow-up about the cancelled 1440p picture, sent 5 s after the cancel (while it is still read) 83.4 s 83.0 s, 83.8 s; the code is read right
text queued behind a picture and cancelled while queued gives its place up, the picture's request is unaffected same

The cost: if another request ended a cancelled picture's encode, a later question about that picture reads it again from the start. In the first row, the follow-up sent after the text took 94.1 s instead of 15.0 s (when it was cached), and it read the code right.

Log lines name each outcome:

[strata] images: a picture's client left and another request waits for the turn: its encode was stopped 0.5 s after the client left (the encoder starts again with the next request)
[strata] images: the image encoder was started again in 0.9 s
[strata] images: a picture whose client left was read to the end (73.8 s after it left): a waiting request sends the same picture

#1999 (closed). It added the cancel event to prepare() and named this case as the remaining limit. This is a focused fix with no new setting and no changed default, so there was no separate scope discussion.

Approach

  • Service.fifo becomes a Turn: a threading.Lock that also counts the requests blocked on it. Everything that used the lock (acquire with or without a timeout, acquire(blocking=False), release, with) works the same. A refused non-blocking try does not count as waiting. unload_waiting counts as waiting.
  • While a request waits for the turn, it registers its pictures by the hash of their bytes as sent (Vision.wanting). A waiting request with the same picture keeps the cancelled encode going.
  • Vision._readline takes an optional stop. It checks it every 0.1 s and, when it is true, kills the encoder the same way #1317's timeout does, then raises ClientGone. stop is passed only when the request has a cancel event, so the other callers are unchanged.
  • A resident (not elastic) encoder that was stopped starts again in the same turn, so the next request finds it running and the engine is not running while it starts. If that start fails, the next picture's _vision_up starts it, and a failure there is a 503 as at load. An elastic encoder starts with its next picture, as before.
  • docs/DETAILS.md (Images): one paragraph on what a Stop does while a picture is read.

Compatibility: the change only matters once a client has left during an encode. Requests whose client stays take the same turns in the same order. The extra work is a 0.1 s poll while an encode runs and one more SHA-256 of each picture's bytes as sent (one for the encode, one more for a request that has to wait for the turn).

Validation

Passed:

  • python -m serve.test_vision_cancel, on this branch (146df3c4 plus this commit), Ubuntu, Python 3.14: 7 tests OK. They use a fake reader: a request that stays is never stopped; a cancelled encode is stopped as soon as another request waits and the reader is started again before the turn is handed on, with no half-written file left; it is kept while nobody waits; it is kept for a waiting request with the same picture; a reader that did not start again starts with the next picture; unload_waiting counts as waiting; Turn counts its waiters.
  • python -m pytest serve, same machine. With this commit: 1467 passed, 49 skipped, 1 failed (below). On 146df3c4 alone: 1460 passed, 49 skipped, the same 1 failed. The 7 more are this PR's tests. The full run was made before a test-only fix: test_unload_with_wait_counts_as_waiting passed, but its thread ended in an AttributeError on the mock engine (one pytest warning more than on 146df3c4). Since the fix, that file passes 3 of 3 with no warning.
  • The live runs in the table: 2x P100 rig, configuration as above. One run of each case before and one after, each with fresh pictures, plus a second run after for the follow-up case and the "stream": false case (the two numbers in those rows). The server code was this commit's serve/server.py changes on my P100 working branch (upstream 9f7d977d plus P100 engine commits, including #5436). The commit applies cleanly to 146df3c4, and no upstream commit between the two touches the changed functions.

Failed:

  • serve/test_keepawake.py::test_service_holds_for_the_whole_request_including_early_close failed in every full python -m pytest serve run here, with and without this commit (one run on 146df3c4 alone, two with this commit). Run alone, the file fails about once in 20 runs, the same with and without (1 of 20 each). It was failing before this change.

Not run:

  • A picture reader on the GPU (resident or elastic). The kill and restart go through the same restart() and _vision_up as #1317's timeout, but I did not run them on a card.
  • Windows and macOS: Popen.kill() there was not exercised.
  • The desktop app and the web page were not driven. The live runs cancel by shutting the socket down, as a browser's Stop closes the connection.
  • No CUDA, HIP, SYCL or Metal build: the change is Python only.

Limits

  • A picture counts as the same only by its bytes as sent. The same picture sent re-encoded, or as an http(s) URL (fetched after the wait), does not keep a cancelled encode going.
  • When another request ends a cancelled picture's encode, a later question about that picture reads it again from the start (the trade-off above).
  • A cancel while the engine reads a prompt still waits for the end of the current prompt chunk. That is in the engine and is separate from this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01N976PznRue6hXdFtmrbJNj


🔄 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/5495 **Author:** [@Keoian](https://github.com/Keoian) **Created:** 11/7/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `serve-vision-cancel-encode` --- ### 📝 Commits (1) - [`566a962`](https://github.com/Niko1221/Strata/commit/566a9625191b2cdf78578b13537315926190007f) [Engine] serve: a picture's encode whose client left no longer holds every request ### 📊 Changes **3 files changed** (+463 additions, -36 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+5 -0) 📝 `serve/server.py` (+212 -36) ➕ `serve/test_vision_cancel.py` (+246 -0) </details> ### 📄 Description ## Problem and result If the client leaves while a picture is being read (the web page's Stop, a closed connection), the server stops answering for the rest of that picture's encode. Nothing crashes and nothing is logged. - The picture reader (strata-vision, llama.cpp mtmd) cannot stop in the middle of a picture. - The encode holds the engine's single turn, so every request waits behind it, text included. - #1999 added a cancel check before each picture. The maintainer's reply there names what is left: "a one-picture request that is abandoned mid-encode keeps the encoder busy until that picture is done". On a CPU reader that can be more than a minute. With this PR, a picture's encode whose client left goes on only while that is useful: - **Another request waits for the turn** (text, another picture, `/unload?wait`). The reader is killed within 0.1 s of seeing that, started again at once (0.9-1.2 s on the CPU below), and the turn is handed on. The half-written output file is deleted. - **A waiting request sends the same picture** (the follow-up question about it, a retry, Regenerate). The encode goes on and that request uses it, so the picture is not read twice. - **Nobody waits.** The encode goes on and is cached, as before, so a follow-up typed later finds the picture read. - A request whose client stays is never stopped. Measured on 2x Xeon E5-2696 v3 and 2x Tesla P100 16 GB (layer split, power-capped at 150 W). Model: Qwen3.8-Flash-Next (a Q4_K_XL / IQ4_NL pack). The picture reader ran on the CPU (`"vision": {"gpu": false, "threads": 16, "max_tokens": 3600}`, Q8_0 mmproj). Each case used fresh pictures (a random 6-character code in a white box on a noisy background, so nothing was cached), sent over HTTP and cancelled the way a browser does: the socket was shut down. Times run from sending a request to the end of its answer. | case | before | after | |---|---|---| | text sent 0.5 s after a 2560x1440 picture (3600 image tokens, ~80 s encode) was cancelled 5 s in | 73.2 s | 1.9 s | | Stop, resend the same picture, Stop again, then text | 69.3 s | 1.6 s | | text after a cancelled request with two 1600x900 pictures | 13.7 s | 1.4 s | | the same, `"stream": false` | 13.6 s | 1.3 s, 1.8 s | | the same, `/v1/messages` | 13.4 s | 1.3 s | | follow-up about the cancelled 1440p picture, sent 5 s after the cancel (while it is still read) | 83.4 s | 83.0 s, 83.8 s; the code is read right | | text queued behind a picture and cancelled while queued | gives its place up, the picture's request is unaffected | same | The cost: if another request ended a cancelled picture's encode, a later question about that picture reads it again from the start. In the first row, the follow-up sent after the text took 94.1 s instead of 15.0 s (when it was cached), and it read the code right. Log lines name each outcome: ``` [strata] images: a picture's client left and another request waits for the turn: its encode was stopped 0.5 s after the client left (the encoder starts again with the next request) [strata] images: the image encoder was started again in 0.9 s [strata] images: a picture whose client left was read to the end (73.8 s after it left): a waiting request sends the same picture ``` ## Related discussion #1999 (closed). It added the cancel event to `prepare()` and named this case as the remaining limit. This is a focused fix with no new setting and no changed default, so there was no separate scope discussion. ## Approach - `Service.fifo` becomes a `Turn`: a `threading.Lock` that also counts the requests blocked on it. Everything that used the lock (`acquire` with or without a timeout, `acquire(blocking=False)`, `release`, `with`) works the same. A refused non-blocking try does not count as waiting. `unload_waiting` counts as waiting. - While a request waits for the turn, it registers its pictures by the hash of their bytes as sent (`Vision.wanting`). A waiting request with the same picture keeps the cancelled encode going. - `Vision._readline` takes an optional `stop`. It checks it every 0.1 s and, when it is true, kills the encoder the same way #1317's timeout does, then raises `ClientGone`. `stop` is passed only when the request has a cancel event, so the other callers are unchanged. - A resident (not elastic) encoder that was stopped starts again in the same turn, so the next request finds it running and the engine is not running while it starts. If that start fails, the next picture's `_vision_up` starts it, and a failure there is a 503 as at load. An elastic encoder starts with its next picture, as before. - `docs/DETAILS.md` (Images): one paragraph on what a Stop does while a picture is read. Compatibility: the change only matters once a client has left during an encode. Requests whose client stays take the same turns in the same order. The extra work is a 0.1 s poll while an encode runs and one more SHA-256 of each picture's bytes as sent (one for the encode, one more for a request that has to wait for the turn). ## Validation **Passed:** - `python -m serve.test_vision_cancel`, on this branch (`146df3c4` plus this commit), Ubuntu, Python 3.14: 7 tests OK. They use a fake reader: a request that stays is never stopped; a cancelled encode is stopped as soon as another request waits and the reader is started again before the turn is handed on, with no half-written file left; it is kept while nobody waits; it is kept for a waiting request with the same picture; a reader that did not start again starts with the next picture; `unload_waiting` counts as waiting; `Turn` counts its waiters. - `python -m pytest serve`, same machine. With this commit: 1467 passed, 49 skipped, 1 failed (below). On `146df3c4` alone: 1460 passed, 49 skipped, the same 1 failed. The 7 more are this PR's tests. The full run was made before a test-only fix: `test_unload_with_wait_counts_as_waiting` passed, but its thread ended in an AttributeError on the mock engine (one pytest warning more than on `146df3c4`). Since the fix, that file passes 3 of 3 with no warning. - The live runs in the table: 2x P100 rig, configuration as above. One run of each case before and one after, each with fresh pictures, plus a second run after for the follow-up case and the `"stream": false` case (the two numbers in those rows). The server code was this commit's `serve/server.py` changes on my P100 working branch (upstream `9f7d977d` plus P100 engine commits, including #5436). The commit applies cleanly to `146df3c4`, and no upstream commit between the two touches the changed functions. **Failed:** - `serve/test_keepawake.py::test_service_holds_for_the_whole_request_including_early_close` failed in every full `python -m pytest serve` run here, with and without this commit (one run on `146df3c4` alone, two with this commit). Run alone, the file fails about once in 20 runs, the same with and without (1 of 20 each). It was failing before this change. **Not run:** - A picture reader on the GPU (resident or elastic). The kill and restart go through the same `restart()` and `_vision_up` as #1317's timeout, but I did not run them on a card. - Windows and macOS: `Popen.kill()` there was not exercised. - The desktop app and the web page were not driven. The live runs cancel by shutting the socket down, as a browser's Stop closes the connection. - No CUDA, HIP, SYCL or Metal build: the change is Python only. ## Limits - A picture counts as the same only by its bytes as sent. The same picture sent re-encoded, or as an `http(s)` URL (fetched after the wait), does not keep a cancelled encode going. - When another request ends a cancelled picture's encode, a later question about that picture reads it again from the start (the trade-off above). - A cancel while the engine reads a prompt still waits for the end of the current prompt chunk. That is in the engine and is separate from this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01N976PznRue6hXdFtmrbJNj --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#5468
No description provided.