[GH-ISSUE #914] strata-vision helper leaks /tmp/strata-vision-* temp dirs (unbounded growth -> OOM on tmpfs /tmp) #396

Closed
opened 2026-11-04 01:42:37 +07:00 by bsakaguchi · 6 comments
Owner

Originally created by @cubacrazy on GitHub (Oct 5, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/914

On v0.1.38 (CUDA 13.4 build, RTX 3090 Ti, CachyOS), strata-vision creates a fresh mkdtemp directory /tmp/strata-vision-<rand>/ for every vision request, filled with .sve scratch files (~150-200 MB per dir), and never removes them - neither after the request nor on helper exit.

Observed after ~1 day of normal native-vision traffic: 120 leaked dirs ~ 12 GB. On distros where /tmp is tmpfs (CachyOS, Fedora defaults), this consumes physical RAM directly and pushes the overflow into zram, which is also RAM - our 62 GiB box hit 62/62 GiB used with 420 MB available and nearly OOMed.

Repro: run the vision path repeatedly (--vision --vram-reserve-mib 700, OpenAI-style image_url requests), watch ls -d /tmp/strata-vision-* | wc -l grow monotonically; each dir contains multiple ~10 MB .sve files.

Expected: helper unlinks its temp dirs after each request (or at least on clean exit), or reuses a single scratch dir per helper process.

Workaround we run meanwhile: 0 */6 * * * find /tmp -maxdepth 1 -name 'strata-vision-*' -type d -mmin +360 -exec rm -rf {} +

Originally created by @cubacrazy on GitHub (Oct 5, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/914 On v0.1.38 (CUDA 13.4 build, RTX 3090 Ti, CachyOS), `strata-vision` creates a fresh `mkdtemp` directory `/tmp/strata-vision-<rand>/` for every vision request, filled with `.sve` scratch files (~150-200 MB per dir), and never removes them - neither after the request nor on helper exit. Observed after ~1 day of normal native-vision traffic: **120 leaked dirs ~ 12 GB**. On distros where `/tmp` is tmpfs (CachyOS, Fedora defaults), this consumes physical RAM directly and pushes the overflow into zram, which is also RAM - our 62 GiB box hit 62/62 GiB used with 420 MB available and nearly OOMed. Repro: run the vision path repeatedly (`--vision --vram-reserve-mib 700`, OpenAI-style `image_url` requests), watch `ls -d /tmp/strata-vision-* | wc -l` grow monotonically; each dir contains multiple ~10 MB `.sve` files. Expected: helper unlinks its temp dirs after each request (or at least on clean exit), or reuses a single scratch dir per helper process. Workaround we run meanwhile: `0 */6 * * * find /tmp -maxdepth 1 -name 'strata-vision-*' -type d -mmin +360 -exec rm -rf {} +`
Author
Owner

@talisp commented on GitHub (Oct 5, 2026):

Data point (Linux, 0.1.38 and 0.1.39, NVIDIA): here it is one directory per server start, not per request. It is created in serve/server.py:1275 (Vision.__init__, tempfile.mkdtemp(prefix="strata-vision-")). It holds one <hash>.sve per unique image (~9–10 MB each; repeated images reuse it). The cache keeps at most 64 of them (the oldest are deleted), so a single run stays bounded (~600 MB), but the directory itself is never removed when the server stops.

A directory from yesterday's run is still here: 64 files (the cap), 596 MB. Today's: 16 files, 147 MB.

So setups that restart the server or the helper often (idle unload, crashes, updates) accumulate one such directory per restart. Removing self.dir on exit, or reusing one fixed per-install cache directory, would cover that. (Edited: the first version said the per-image files were never removed; they are, past 64.)

<!-- gh-comment-id:6001502942 --> @talisp commented on GitHub (Oct 5, 2026): Data point (Linux, 0.1.38 and 0.1.39, NVIDIA): here it is one directory per **server start**, not per request. It is created in `serve/server.py:1275` (`Vision.__init__`, `tempfile.mkdtemp(prefix="strata-vision-")`). It holds one `<hash>.sve` per unique image (~9–10 MB each; repeated images reuse it). The cache keeps at most 64 of them (the oldest are deleted), so a single run stays bounded (~600 MB), but **the directory itself is never removed** when the server stops. A directory from yesterday's run is still here: 64 files (the cap), 596 MB. Today's: 16 files, 147 MB. So setups that restart the server or the helper often (idle unload, crashes, updates) accumulate one such directory per restart. Removing `self.dir` on exit, or reusing one fixed per-install cache directory, would cover that. (Edited: the first version said the per-image files were never removed; they are, past 64.)
Author
Owner

@fouyzjl commented on GitHub (Oct 6, 2026):

Concrete numbers on v0.1.39 (Ubuntu, RTX 4090, vision.max_tokens: 8192 — the per-image budget raised from the 1,024 default), which make the disk side of this worse than the ~150-200 MB/request and ~600 MB/run in the report above:

The 64-entry cap is a count, not bytes (serve/server.py:1368), and one entry is one unique image. At an 8192-token image budget a full 2481x3508 page becomes a 79.4 MiB <hash>.sve, so one server process's directory reaches ~5 GB (64 x 79 MB), not ~600 MB. We measured 2.7 GB with mixed sizes, then 4.4 GB after 16 full-page images displaced them. With no removal on exit, every server start adds another such directory. (Our /tmp is ext4, so for us this is disk; on a tmpfs it would be ~5 GB of RAM per run dir.)

Related: a cache hit does not refresh an entry's position (if key in self.cache: return self.cache[key]), so an image that is requested repeatedly can still sit at the front of the FIFO and be evicted.

Two paths we instrumented while chasing a truncated stream upstream of us (analysis, not an observed failure):

  • encode() unlinks the oldest <hash>.sve unconditionally on overflow (:1369-1370, no reference check), while another request can be between its encode() and the f.write(path.read_bytes()) that combines the images (:2180-2182);
  • self.embeddings.path is per-service state, set when a request's images are combined (:2183) and re-read in run() (:2257); with two image requests in flight, B's combine can overwrite A's path, and the finally (:2459-2460) then unlinks whatever was set last.

Reproduction attempt, for the record: 16 image requests, 4 at a time, all unique images, cache held at its 64-entry cap -> 16/16 streams ended with data: [DONE], no error. The four "concurrent" requests serialized (serving: 1), so those two windows never overlapped here. Reporting them as latent only; the practical impact we see is the disk side above.

<!-- gh-comment-id:6008795615 --> @fouyzjl commented on GitHub (Oct 6, 2026): Concrete numbers on v0.1.39 (Ubuntu, RTX 4090, `vision.max_tokens: 8192` — the per-image budget raised from the 1,024 default), which make the disk side of this worse than the ~150-200 MB/request and ~600 MB/run in the report above: **The 64-entry cap is a count, not bytes** (`serve/server.py:1368`), and one entry is one unique image. At an 8192-token image budget a full 2481x3508 page becomes a **79.4 MiB** `<hash>.sve`, so one server process's directory reaches **~5 GB** (64 x 79 MB), not ~600 MB. We measured 2.7 GB with mixed sizes, then 4.4 GB after 16 full-page images displaced them. With no removal on exit, every server start adds another such directory. (Our `/tmp` is ext4, so for us this is disk; on a tmpfs it would be ~5 GB of RAM per run dir.) Related: a cache **hit** does not refresh an entry's position (`if key in self.cache: return self.cache[key]`), so an image that is requested repeatedly can still sit at the front of the FIFO and be evicted. Two paths we instrumented while chasing a truncated stream upstream of us (analysis, not an observed failure): - `encode()` unlinks the oldest `<hash>.sve` unconditionally on overflow (`:1369-1370`, no reference check), while another request can be between its `encode()` and the `f.write(path.read_bytes())` that combines the images (`:2180-2182`); - `self.embeddings.path` is per-service state, set when a request's images are combined (`:2183`) and re-read in `run()` (`:2257`); with two image requests in flight, B's combine can overwrite A's path, and the `finally` (`:2459-2460`) then unlinks whatever was set last. Reproduction attempt, for the record: 16 image requests, 4 at a time, all unique images, cache held at its 64-entry cap -> 16/16 streams ended with `data: [DONE]`, no error. The four "concurrent" requests serialized (`serving: 1`), so those two windows never overlapped here. Reporting them as latent only; the practical impact we see is the disk side above.
Author
Owner

@Niko1221 commented on GitHub (Oct 6, 2026):

Fixed in 0.1.40: when the server ends (Ctrl+C or a normal stop), it removes the image encoder's scratch directory, so /tmp/strata-vision-* no longer piles up one per start. An idle unload keeps the directory (the encoded images stay cached for the restart). A directory left by a crash or kill is not swept; the find /tmp ... -mmin +360 cron is still the way to clean those.

<!-- gh-comment-id:6010941534 --> @Niko1221 commented on GitHub (Oct 6, 2026): Fixed in 0.1.40: when the server ends (Ctrl+C or a normal stop), it removes the image encoder's scratch directory, so /tmp/strata-vision-* no longer piles up one per start. An idle unload keeps the directory (the encoded images stay cached for the restart). A directory left by a crash or kill is not swept; the `find /tmp ... -mmin +360` cron is still the way to clean those.
Author
Owner

@fouyzjl commented on GitHub (Oct 6, 2026):

Correction to my previous comment, with the evidence I did not have then: the encode() eviction path does bite in production.

serve/server.py:2182 in prepare():

Exception occurred during processing of request from ('127.0.0.1', 40576)
Traceback (most recent call last):
  ...
  File "/opt/strata/serve/server.py", line 3158, in do_POST
    self._openai(req)
  File "/opt/strata/serve/server.py", line 3379, in _openai
    ids, thinking, max_new = svc.prepare(messages, tools, kw, max_new)
  File "/opt/strata/serve/server.py", line 2182, in prepare
    f.write(path.read_bytes())
  File "/usr/lib/python3.10/pathlib.py", line 1126, in read_bytes
    with self.open(mode='rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/strata-vision-371ddri3/bfe5c78305479e3bd45d5b2a40cb5e11.sve'

The path returned by encode() was gone by the time prepare() combined it: the file was unlinked by another request's cache overflow (:1369-1370). The handler thread ends there, so the response is never written - a client that was streaming sees the connection end without data: [DONE]. Our fronting proxy treats that as an unconfirmed completion and pauses; that is how we noticed.

What made it reachable here, since it was not in my earlier 4-at-a-time attempt: the two image requests were not queued behind each other at the engine (serving: 1 only serializes the engine's own work). prepare() - the image encoding, the combine, the tokenizer - runs before the engine is engaged, so two callers can be inside it at the same time even on a single-slot server.

So the unlink is not merely latent. A cheap fix on the same line: do not unlink on eviction (drop the dict entry only), or defer the unlink to the end of the request that owns the path. Worth a look either way; thanks for the dir-leak fix in advance.

<!-- gh-comment-id:6011335723 --> @fouyzjl commented on GitHub (Oct 6, 2026): Correction to my previous comment, with the evidence I did not have then: **the `encode()` eviction path does bite in production.** `serve/server.py:2182` in `prepare()`: ``` Exception occurred during processing of request from ('127.0.0.1', 40576) Traceback (most recent call last): ... File "/opt/strata/serve/server.py", line 3158, in do_POST self._openai(req) File "/opt/strata/serve/server.py", line 3379, in _openai ids, thinking, max_new = svc.prepare(messages, tools, kw, max_new) File "/opt/strata/serve/server.py", line 2182, in prepare f.write(path.read_bytes()) File "/usr/lib/python3.10/pathlib.py", line 1126, in read_bytes with self.open(mode='rb') as f: FileNotFoundError: [Errno 2] No such file or directory: '/tmp/strata-vision-371ddri3/bfe5c78305479e3bd45d5b2a40cb5e11.sve' ``` The path returned by `encode()` was gone by the time `prepare()` combined it: the file was unlinked by another request's cache overflow (`:1369-1370`). The handler thread ends there, so the response is never written - a client that was streaming sees the connection end without `data: [DONE]`. Our fronting proxy treats that as an unconfirmed completion and pauses; that is how we noticed. What made it reachable here, since it was not in my earlier 4-at-a-time attempt: the two image requests were not queued behind each other at the engine (`serving: 1` only serializes the engine's own work). `prepare()` - the image encoding, the combine, the tokenizer - runs before the engine is engaged, so two callers can be inside it at the same time even on a single-slot server. So the unlink is not merely latent. A cheap fix on the same line: do not unlink on eviction (drop the dict entry only), or defer the unlink to the end of the request that owns the path. Worth a look either way; thanks for the dir-leak fix in advance.
Author
Owner

@fouyzjl commented on GitHub (Oct 6, 2026):

Follow-up with a deterministic, single-request reproduction - no concurrency needed, and it corrects my previous explanation:

encode() returns a path for each image of a request, and the request then reads those paths to combine them (:2180-2182). If the request's first image is a cache hit whose entry is the oldest in self.cache, and a later image of the same request misses, that insert evicts the oldest entry - the first image's own file - before the combine reads it:

encoded = [self.vision.encode(src) for src in images]   # img1 = hit (oldest), img2 = miss -> insert -> evicts img1
...
with open(combined, "wb") as f:
    for path, _ in encoded:
        f.write(path.read_bytes())                      # img1: FileNotFoundError

Reproduced on the first try (320x240 PNGs, one completion each):

  1. send image X (insert; becomes the oldest entry once 63 more unique images are inserted);
  2. send 63 unique images (fills the 64-entry cache, X now at the front);
  3. send one request with [X, Y] (X = hit, Y = new):
    -> RemoteDisconnected: Remote end closed connection without response, 0.01 s, and
File "/opt/strata/serve/server.py", line 2182, in prepare
    f.write(path.read_bytes())
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/strata-vision-371ddri3/<X>.sve'
  1. sending X alone afterwards: 200 (re-encoded);
  2. sending [Y2, X] (miss first, hit second): 200 - so it is the order, and the eviction of an image the same request already holds.

This also explains why my earlier 4-at-a-time attempt reproduced nothing: one image per request never puts an evicted entry and a reader in the same request. A cache hit does not refresh an entry's position (if key in self.cache: return self.cache[key]), so a repeatedly sent image (a page sent with every figure-crop/figure-review request) walks to the front of the FIFO and is the first to be evicted.

The unlink on :1370 is therefore hit by ordinary single-caller traffic, not only by an unusual concurrent one. Not unlinking there (popping the dict entry only), or deferring it past the request that owns the path, would remove it.

<!-- gh-comment-id:6011355032 --> @fouyzjl commented on GitHub (Oct 6, 2026): Follow-up with a **deterministic, single-request reproduction** - no concurrency needed, and it corrects my previous explanation: `encode()` returns a path for each image of a request, and the request then reads those paths to combine them (`:2180-2182`). If the request's **first** image is a cache hit whose entry is the oldest in `self.cache`, and a **later** image of the same request misses, that insert evicts the oldest entry - the first image's own file - before the combine reads it: ``` encoded = [self.vision.encode(src) for src in images] # img1 = hit (oldest), img2 = miss -> insert -> evicts img1 ... with open(combined, "wb") as f: for path, _ in encoded: f.write(path.read_bytes()) # img1: FileNotFoundError ``` Reproduced on the first try (320x240 PNGs, one completion each): 1. send image `X` (insert; becomes the oldest entry once 63 more unique images are inserted); 2. send 63 unique images (fills the 64-entry cache, `X` now at the front); 3. send **one request with `[X, Y]`** (`X` = hit, `Y` = new): -> `RemoteDisconnected: Remote end closed connection without response`, 0.01 s, and ``` File "/opt/strata/serve/server.py", line 2182, in prepare f.write(path.read_bytes()) FileNotFoundError: [Errno 2] No such file or directory: '/tmp/strata-vision-371ddri3/<X>.sve' ``` 4. sending `X` alone afterwards: `200` (re-encoded); 5. sending `[Y2, X]` (miss first, hit second): `200` - so it is the order, and the eviction of an image the same request already holds. This also explains why my earlier 4-at-a-time attempt reproduced nothing: one image per request never puts an evicted entry and a reader in the same request. A cache hit does not refresh an entry's position (`if key in self.cache: return self.cache[key]`), so a repeatedly sent image (a page sent with every figure-crop/figure-review request) walks to the front of the FIFO and is the first to be evicted. The unlink on `:1370` is therefore hit by ordinary single-caller traffic, not only by an unusual concurrent one. Not unlinking there (popping the dict entry only), or deferring it past the request that owns the path, would remove it.
Author
Owner

@fouyzjl commented on GitHub (Oct 6, 2026):

A three-line fix for the reproduction above, verified here (whole-page + figure-crop style multi-image requests, cache held at its 64-entry cap):

             if key in self.cache:
-                return self.cache[key]
+                cached = self.cache[key]
+                if cached[0].exists():
+                    return cached
+                # the file is gone although the entry is not: encode it again
+                del self.cache[key]
             img, out = self.dir / f"{key}.img", self.dir / f"{key}.sve"
@@
-                self.cache.pop(old)[0].unlink(missing_ok=True)
+                self.cache.pop(old)          # do not unlink: a request may still read this path
@@
-                for path, _ in encoded:
-                    f.write(path.read_bytes())
+                for (path, _), src in zip(encoded, images):
+                    try:
+                        f.write(path.read_bytes())
+                    except FileNotFoundError:       # evicted after encode(): encode this one again
+                        with self.fifo:
+                            path, _ = self.vision.encode(src)
+                        f.write(path.read_bytes())

Measured here, before -> after:

case before after
[X, Y], X = oldest hit, Y = miss (the reproduction) RemoteDisconnected 200
delete a cached <hash>.sve by hand, then send that image RemoteDisconnected 200
same, as [X, Y] RemoteDisconnected 200
[Y, X] (miss first) 200 200

The exists() check on a hit is what makes the retry effective: without it encode() returns the same missing path and the second read fails the same way (I measured that too). The unlink is the actual bug; the other two are belt and braces for anything else that removes scratch files while the server runs (a find /tmp -delete style cleanup did it to us once).

Happy to send this as a PR if that is easier than applying it by hand - just say which branch you want it against. (It is a local patch here, with the original kept aside.)

<!-- gh-comment-id:6011517590 --> @fouyzjl commented on GitHub (Oct 6, 2026): A three-line fix for the reproduction above, verified here (whole-page + figure-crop style multi-image requests, cache held at its 64-entry cap): ```diff if key in self.cache: - return self.cache[key] + cached = self.cache[key] + if cached[0].exists(): + return cached + # the file is gone although the entry is not: encode it again + del self.cache[key] img, out = self.dir / f"{key}.img", self.dir / f"{key}.sve" @@ - self.cache.pop(old)[0].unlink(missing_ok=True) + self.cache.pop(old) # do not unlink: a request may still read this path @@ - for path, _ in encoded: - f.write(path.read_bytes()) + for (path, _), src in zip(encoded, images): + try: + f.write(path.read_bytes()) + except FileNotFoundError: # evicted after encode(): encode this one again + with self.fifo: + path, _ = self.vision.encode(src) + f.write(path.read_bytes()) ``` Measured here, before -> after: | case | before | after | | --- | --- | --- | | `[X, Y]`, X = oldest hit, Y = miss (the reproduction) | `RemoteDisconnected` | `200` | | delete a cached `<hash>.sve` by hand, then send that image | `RemoteDisconnected` | `200` | | same, as `[X, Y]` | `RemoteDisconnected` | `200` | | `[Y, X]` (miss first) | `200` | `200` | The `exists()` check on a hit is what makes the retry effective: without it `encode()` returns the same missing path and the second read fails the same way (I measured that too). The unlink is the actual bug; the other two are belt and braces for anything else that removes scratch files while the server runs (a `find /tmp -delete` style cleanup did it to us once). Happy to send this as a PR if that is easier than applying it by hand - just say which branch you want it against. (It is a local patch here, with the original kept aside.)
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#396
No description provided.