fix(gateway): reuse Windows-safe atomic lease I/O
Adopt the read_atomic_bytes/replace_atomic integration proposed by balabommablock-cpu, preserving UTF-8 decoding and the existing lease locks. Exercise a real snapshot reader across an actual replacement instead of only checking helper calls. Adapted-from: https://github.com/balabommablock-cpu/nanobot/commit/8d671457935e44579f37ceffe6dbaf61b36b00be Related to HKUDS/nanobot#6514 Co-authored-by: B Rishabh Rao <262621052+balabommablock-cpu@users.noreply.github.com>
This commit is contained in:
1 parent
2853287ed7
commit
1c28b20c7e
2 files changed
+29
-2
No files matched your search
@@ -35,6 +35,7 @@ from nanobot.process_runtime import (
|
|||||||
process_identity_record,
|
process_identity_record,
|
||||||
process_is_running,
|
process_is_running,
|
||||||
)
|
)
|
||||||
|
from nanobot.utils.atomic_file import read_atomic_bytes, replace_atomic
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@@ -772,7 +773,7 @@ class GatewayClientLease:
|
|||||||
|
|
||||||
def _read_state(self) -> dict[str, object]:
|
def _read_state(self) -> dict[str, object]:
|
||||||
try:
|
try:
|
||||||
payload: object = json.loads(self.state_path.read_text(encoding="utf-8"))
|
payload: object = json.loads(read_atomic_bytes(self.state_path).decode("utf-8"))
|
||||||
except (OSError, json.JSONDecodeError, ValueError):
|
except (OSError, json.JSONDecodeError, ValueError):
|
||||||
return {"auto_stop": False, "clients": {}}
|
return {"auto_stop": False, "clients": {}}
|
||||||
if isinstance(payload, dict):
|
if isinstance(payload, dict):
|
||||||
@@ -800,7 +801,7 @@ class GatewayClientLease:
|
|||||||
handle.write("\n")
|
handle.write("\n")
|
||||||
handle.flush()
|
handle.flush()
|
||||||
os.fsync(handle.fileno())
|
os.fsync(handle.fileno())
|
||||||
temporary.replace(self.state_path)
|
replace_atomic(temporary, self.state_path)
|
||||||
finally:
|
finally:
|
||||||
temporary.unlink(missing_ok=True)
|
temporary.unlink(missing_ok=True)
|
||||||
|
|
||||||
|
|||||||
@@ -553,6 +553,32 @@ def test_restart_stops_then_starts_the_background_gateway(tmp_path, monkeypatch)
|
|||||||
assert result.ok is True
|
assert result.ok is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_lease_reader_keeps_snapshot_during_replacement(tmp_path, monkeypatch):
|
||||||
|
"""A real lease read retains its contents while a writer publishes the next state."""
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
from nanobot.utils import atomic_file
|
||||||
|
|
||||||
|
runtime = GatewayRuntime(paths=_paths(tmp_path))
|
||||||
|
client = GatewayClientLease(runtime, kind="tui", token="client")
|
||||||
|
client.acquire()
|
||||||
|
previous = client._read_state()
|
||||||
|
current = {**previous, "auto_stop": True}
|
||||||
|
open_snapshot = atomic_file._open_snapshot
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def publish_while_reading(path):
|
||||||
|
with open_snapshot(path) as snapshot:
|
||||||
|
client._write_state(current)
|
||||||
|
yield snapshot
|
||||||
|
|
||||||
|
with monkeypatch.context() as patch:
|
||||||
|
patch.setattr(atomic_file, "_open_snapshot", publish_while_reading)
|
||||||
|
assert client._read_state() == previous
|
||||||
|
assert client._read_state() == current
|
||||||
|
assert list(runtime.paths.run_dir.glob("*.tmp")) == []
|
||||||
|
|
||||||
|
|
||||||
def test_last_interactive_client_stops_an_on_demand_gateway(tmp_path, monkeypatch):
|
def test_last_interactive_client_stops_an_on_demand_gateway(tmp_path, monkeypatch):
|
||||||
runtime = GatewayRuntime(paths=_paths(tmp_path), platform_name="Linux")
|
runtime = GatewayRuntime(paths=_paths(tmp_path), platform_name="Linux")
|
||||||
monkeypatch.setattr(runtime, "_process_identity", lambda pid: pid)
|
monkeypatch.setattr(runtime, "_process_identity", lambda pid: pid)
|
||||||
|
|||||||
Reference in new issue
Block a user