[PR #3546] [CLOSED] serve: a cached image file is never deleted while a request still needs it #4682

Closed
opened 2026-11-07 15:38:18 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3546
Author: @usakim97
Created: 10/21/2026
Status: ❌ Closed

Base: main ← Head: f-vision-cache-evict


📝 Commits (1)

  • 509e161 serve: a cached image file is never deleted while a request still needs it

📊 Changes

3 files changed (+157 additions, -5 deletions)

View changed files

📝 serve/server.py (+28 -4)
📝 serve/test_server.py (+1 -1)
➕ serve/test_vision_cache.py (+128 -0)

📄 Description

The failure

A request that sends images encodes them under the FIFO turn, then copies the cached
.sve files into one combined file after the turn is released. Another request can
take the turn in between, encode enough pictures to evict the first request's entries
(the cache keeps 64), and unlink those files. The first request's
write_temporary(combined, ...) then raises FileNotFoundError on read_bytes().

Nothing catches it: the handler dies before writing a response, so the client sees a
dropped connection — behind a reverse proxy, 502 with an empty body. It reproduces
with a long conversation that resends 30+ frames per turn: measured on a 140k-token
session, every request failed in 2.7 s while the same server answered 200 to curl with
146k-token prompts, 41 MB bodies, images, tools, streaming and h2.

The fix

  • Vision.hold(paths) / Vision.release(paths): a request holds the files it has
    encoded from inside the turn that encoded them until its combined file is written.
    Eviction skips held files. This closes the window completely: no request can evict a
    file another request has not copied yet.
  • A cache hit whose file is gone is not a hit. encode() checks path.is_file() and
    encodes the picture again instead of returning a path the caller cannot read (a
    Windows temp cleaner can also remove one; _ensure_dir already covers the whole
    directory disappearing).
  • release() ends the hold, so the next eviction may take the file.

Tests

serve/test_vision_cache.py — 6 tests, no GPU, a fake encoder writes the .sve:
a hit is reused; a hit whose file is gone re-encodes; a held file survives 199 evictions
and is evicted after release; a request keeps its own files while it encodes 100
pictures; the map stays at 64; the combined file holds every picture.

serve.test_server.VisionCacheEviction keeps passing (its helper gains the new
in_use field). Full serve suite: 899 tests, the only failure is
test_fatal_recovery.test_http_health_and_next_request_recovery, which fails the same
way on unmodified main on this machine.

Note, not changed here

The 64-entry cache is small for a frame-review session: every turn re-encodes its
pictures. Correctness no longer depends on the size, so this PR leaves the bound alone.


🔄 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/3546 **Author:** [@usakim97](https://github.com/usakim97) **Created:** 10/21/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `f-vision-cache-evict` --- ### 📝 Commits (1) - [`509e161`](https://github.com/Niko1221/Strata/commit/509e161b6868d1cdb95cd9e187388fdd64564c52) serve: a cached image file is never deleted while a request still needs it ### 📊 Changes **3 files changed** (+157 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+28 -4) 📝 `serve/test_server.py` (+1 -1) ➕ `serve/test_vision_cache.py` (+128 -0) </details> ### 📄 Description ## The failure A request that sends images encodes them under the FIFO turn, then copies the cached `.sve` files into one combined file **after** the turn is released. Another request can take the turn in between, encode enough pictures to evict the first request's entries (the cache keeps 64), and unlink those files. The first request's `write_temporary(combined, ...)` then raises `FileNotFoundError` on `read_bytes()`. Nothing catches it: the handler dies before writing a response, so the client sees a dropped connection — behind a reverse proxy, `502` with an empty body. It reproduces with a long conversation that resends 30+ frames per turn: measured on a 140k-token session, every request failed in 2.7 s while the same server answered 200 to curl with 146k-token prompts, 41 MB bodies, images, tools, streaming and h2. ## The fix - `Vision.hold(paths)` / `Vision.release(paths)`: a request holds the files it has encoded from inside the turn that encoded them until its combined file is written. Eviction skips held files. This closes the window completely: no request can evict a file another request has not copied yet. - A cache hit whose file is gone is not a hit. `encode()` checks `path.is_file()` and encodes the picture again instead of returning a path the caller cannot read (a Windows temp cleaner can also remove one; `_ensure_dir` already covers the whole directory disappearing). - `release()` ends the hold, so the next eviction may take the file. ## Tests `serve/test_vision_cache.py` — 6 tests, no GPU, a fake encoder writes the `.sve`: a hit is reused; a hit whose file is gone re-encodes; a held file survives 199 evictions and is evicted after release; a request keeps its own files while it encodes 100 pictures; the map stays at 64; the combined file holds every picture. `serve.test_server.VisionCacheEviction` keeps passing (its helper gains the new `in_use` field). Full `serve` suite: 899 tests, the only failure is `test_fatal_recovery.test_http_health_and_next_request_recovery`, which fails the same way on unmodified `main` on this machine. ## Note, not changed here The 64-entry cache is small for a frame-review session: every turn re-encodes its pictures. Correctness no longer depends on the size, so this PR leaves the bound alone. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:38:18 +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#4682
No description provided.