Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

Time-skip and tune job records are replaced with a wait for a reader (Windows 'Access is denied' when the host reads the file at that moment: it ended jobs at random)

Niko122120b377d30de5
diff --git a/strata_control/atomic.py b/strata_control/atomic.py
new file mode 100644
index 000000000..44871817d
--- /dev/null
+++ b/strata_control/atomic.py
@@ -0,0 +1,42 @@
+"""Replace a file that another program may be reading, on Windows as well as everywhere else.
+
+A job record (a Time-skip `run.json`, a tuning `job.json`) is rewritten at every phase and READ by the app's host every couple of seconds.
+On Windows, `os.replace` onto a file that is open in another process fails with PermissionError ("Access is denied") for as long as the
+reader holds it. That is a split second, but a job saves many times, so a Time-skip or a tune died at random with that error. The fix is
+to wait that split second and try again; any other error, and a PermissionError that does not go away, is raised as before.
+"""
+
+from __future__ import annotations
+
+import os
+import time
+from pathlib import Path
+from typing import Union
+
+PathLike = Union[str, "os.PathLike[str]"]
+
+#: how long a held file is waited for, in total (seconds) and between tries
+PATIENCE_S = 5.0
+STEP_S = 0.02
+
+
+def replace(src: PathLike, dst: PathLike, *, patience: float = PATIENCE_S, sleep=time.sleep, clock=time.monotonic) -> None:
+ deadline = clock() + patience
+ step = STEP_S
+ while True:
+ try:
+ os.replace(src, dst)
+ return
+ except PermissionError:
+ if os.name != "nt" or clock() >= deadline:
+ raise
+ sleep(step)
+ step = min(step * 1.5, 0.25)
+
+
+def write_text(path: PathLike, text: str, *, encoding: str = "utf-8") -> None:
+ """Write `text` to `path` through a neighbouring temporary file, so a reader never sees half a record."""
+ target = Path(path)
+ tmp = target.with_suffix(target.suffix + ".tmp") if target.suffix else target.with_name(target.name + ".tmp")
+ tmp.write_text(text, encoding=encoding)
+ replace(tmp, target)
diff --git a/strata_control/time_skip.py b/strata_control/time_skip.py
index 94f4e3ff9..564e188cd 100644
--- a/strata_control/time_skip.py
+++ b/strata_control/time_skip.py
@@ -39,6 +39,8 @@ from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Callable, Iterable, Sequence
+from strata_control import atomic
+
SCHEMA_VERSION = 1
# --------------------------------------------------------------------------- phases and states
@@ -367,7 +369,7 @@ class TimeSkipRun:
def save(self) -> None:
tmp = self.state_file.with_suffix(".tmp")
tmp.write_text(json.dumps(self.to_dict(), indent=2), encoding="utf-8")
- os.replace(tmp, self.state_file)
+ atomic.replace(tmp, self.state_file)
@staticmethod
def load(root: Path, run_id: str) -> dict[str, Any]:
diff --git a/strata_control/tuning.py b/strata_control/tuning.py
index 3522d1780..c9c8394c4 100644
--- a/strata_control/tuning.py
+++ b/strata_control/tuning.py
@@ -57,6 +57,8 @@ from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any, Callable, Sequence
+from strata_control import atomic
+
SCHEMA_VERSION = 1
# --------------------------------------------------------------------------- backends
@@ -356,7 +358,7 @@ class Tuner:
def save(self) -> None:
tmp = self.state_file.with_suffix(".tmp")
tmp.write_text(json.dumps(self.to_dict(), indent=2), encoding="utf-8")
- os.replace(tmp, self.state_file)
+ atomic.replace(tmp, self.state_file)
def _report_or_none(self) -> dict[str, Any] | None:
if self.state not in ("completed", "cancelled", "failed"):
diff --git a/tests/time_skip/test_atomic.py b/tests/time_skip/test_atomic.py
new file mode 100644
index 000000000..caa7414c6
--- /dev/null
+++ b/tests/time_skip/test_atomic.py
@@ -0,0 +1,62 @@
+"""atomic.replace: a file another program is reading is waited for, not failed on (Windows "Access is denied")."""
+
+from __future__ import annotations
+
+import os
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+from unittest import mock
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
+
+from strata_control import atomic # noqa: E402
+
+
+class TestReplace(unittest.TestCase):
+ def setUp(self) -> None:
+ self.tmp = Path(tempfile.mkdtemp(prefix="strata-atomic-"))
+
+ def test_plain_replace(self):
+ (self.tmp / "a.tmp").write_text("new")
+ (self.tmp / "a").write_text("old")
+ atomic.replace(self.tmp / "a.tmp", self.tmp / "a")
+ self.assertEqual((self.tmp / "a").read_text(), "new")
+
+ def test_a_held_file_is_waited_for_on_windows_and_the_error_is_kept_elsewhere(self):
+ real = os.replace
+ calls = {"n": 0}
+
+ def held(src, dst):
+ calls["n"] += 1
+ if calls["n"] < 4:
+ raise PermissionError(5, "Access is denied")
+ return real(src, dst)
+
+ (self.tmp / "b.tmp").write_text("new")
+ slept = []
+ with mock.patch("os.replace", held), mock.patch.object(atomic.os, "name", "nt"):
+ atomic.replace(self.tmp / "b.tmp", self.tmp / "b", sleep=slept.append)
+ self.assertEqual(calls["n"], 4)
+ self.assertEqual(len(slept), 3)
+ self.assertEqual((self.tmp / "b").read_text(), "new")
+ # on a system without this behaviour a PermissionError is a real error
+ (self.tmp / "c.tmp").write_text("x")
+ with mock.patch("os.replace", side_effect=PermissionError(13, "denied")), mock.patch.object(atomic.os, "name", "posix"):
+ with self.assertRaises(PermissionError):
+ atomic.replace(self.tmp / "c.tmp", self.tmp / "c")
+
+ def test_a_file_that_stays_held_is_given_up_on_after_the_patience(self):
+ now = [0.0]
+ with mock.patch("os.replace", side_effect=PermissionError(5, "Access is denied")), mock.patch.object(atomic.os, "name", "nt"):
+ with self.assertRaises(PermissionError):
+ atomic.replace("x", "y", patience=1.0, sleep=lambda s: now.__setitem__(0, now[0] + 0.5), clock=lambda: now[0])
+
+ def test_write_text_leaves_no_temporary_file(self):
+ atomic.write_text(self.tmp / "run.json", "{}")
+ self.assertEqual(sorted(p.name for p in self.tmp.iterdir()), ["run.json"])
+
+
+if __name__ == "__main__":
+ unittest.main()