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:
chengyongruandB Rishabh Rao authored and chengyongru committed 2026-10-29 13:11:21 +08:00
1 parent 2853287ed7
commit 1c28b20c7e
2 files changed
+29 -2

No files matched your search

+3 -2
View File
@@ -35,6 +35,7 @@ from nanobot.process_runtime import (
process_identity_record,
process_is_running,
)
from nanobot.utils.atomic_file import read_atomic_bytes, replace_atomic
@dataclass(frozen=True)
@@ -772,7 +773,7 @@ class GatewayClientLease:
def _read_state(self) -> dict[str, object]:
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):
return {"auto_stop": False, "clients": {}}
if isinstance(payload, dict):
@@ -800,7 +801,7 @@ class GatewayClientLease:
handle.write("\n")
handle.flush()
os.fsync(handle.fileno())
temporary.replace(self.state_path)
replace_atomic(temporary, self.state_path)
finally:
temporary.unlink(missing_ok=True)