Fix #3370: the vision work folder is made again when a temp cleaner removed it

This commit is contained in:
Niko1221 committed 2026-10-20 14:13:34 +02:00
1 parent e346198ff3
commit 76186bb595
2 files changed
+55

No files matched your search

+9
View File
@@ -2560,6 +2560,13 @@ class Vision:
args += ["--min-tokens", str(cfg["min_tokens"])]
return args
def _ensure_dir(self):
"""#3370: a temp cleaner (Windows Storage Sense) removes the empty work folder while the encoder is unloaded; the next
start then failed with WinError 267 for as long as the server lived. Make it again; pictures cached in it are gone."""
if not self.dir.is_dir():
self.dir.mkdir(parents=True, exist_ok=True)
self.cache.clear()
def _start(self):
"""Start the reader. When a GPU reader that has a CPU reader to fall back on does not start (the GPU runtime does not
load, the card is not in its build, the card is busy), the CPU reader is started instead, for good, with one log line
@@ -2577,6 +2584,7 @@ class Vision:
def _start_reader(self):
args, log, env = self.spawn
self._ensure_dir()
# cwd is the encoder's directory: the ENC lines name the files relative to it (#480: no space in the line)
self.proc = popen("the image encoder", args, role="vision", stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=log or subprocess.DEVNULL, text=True, encoding="utf-8", bufsize=1, env=env,
@@ -2739,6 +2747,7 @@ class Vision:
data = self.normalize(source if isinstance(source, bytes) else self.load(source))
key = hashlib.sha256(data).hexdigest()[:32]
with self.lock:
self._ensure_dir()
if key in self.cache:
self.cache[key] = self.cache.pop(key) # most recently used last
return self.cache[key]
+46
View File
@@ -0,0 +1,46 @@
"""#3370: the encoder's work folder is made again when a temp cleaner removed it while the encoder was unloaded.
python -m serve.test_vision_workdir
"""
import io
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest import mock
import serve.server as server
class WorkDir(unittest.TestCase):
def test_start_makes_a_removed_work_folder_again(self):
proc = SimpleNamespace(stdin=io.StringIO(), stdout=io.StringIO("READY\n"), kill=lambda: None,
wait=lambda timeout=None: 0, poll=lambda: None)
with tempfile.TemporaryDirectory() as tmp:
v = server.Vision.__new__(server.Vision)
v.spawn = (["strata-vision"], None, None)
v.dir = Path(tmp) / "strata-vision-gone"
v.cache = {"k": (v.dir / "k.sve", 4)}
seen = []
def fake_popen(*a, **k):
seen.append(Path(k["cwd"]).is_dir()) # the real popen raises WinError 267 when it is not
return proc
with mock.patch.object(server, "popen", fake_popen):
v._start()
self.assertEqual(seen, [True])
self.assertTrue(v.dir.is_dir())
self.assertEqual(v.cache, {}) # its cached embeddings went with the folder
def test_an_existing_folder_and_its_cache_are_left_alone(self):
with tempfile.TemporaryDirectory() as tmp:
v = server.Vision.__new__(server.Vision)
v.dir = Path(tmp)
v.cache = {"k": (v.dir / "k.sve", 4)}
v._ensure_dir()
self.assertEqual(len(v.cache), 1)
if __name__ == "__main__":
unittest.main()