The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): lightpanda session records are written atomically
Maciej Lisiewski54acc3a5b956
diff --git a/tests/tools/test_browser_lightpanda.py b/tests/tools/test_browser_lightpanda.pyindex aa8148494fa..21e8d601e2f 100644--- a/tests/tools/test_browser_lightpanda.py+++ b/tests/tools/test_browser_lightpanda.py@@ -716,3 +716,18 @@ class TestLightpandaSessionLifecycle:dead_owner.write_text(json.dumps({"owner_pid": 2**22 + 7}), encoding="utf-8")assert browser_lightpanda.reap_orphaned_lightpanda() == 0assert not bad.exists() and not dead_owner.exists()++ def test_orphan_sweep_mid_record_write_keeps_the_live_record(self, tmp_path, monkeypatch):+ """A sweep running while the owner writes its record must not see a torn (empty) file:+ it would unlink it as corrupt and the live server would never be reaped after a crash."""+ from tests.file_write_observer import observe_file_writes+ from tools import browser_lightpanda++ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ server = browser_lightpanda.LightpandaServer("live", 9222, MagicMock(pid=os.getpid()), "x.log")+ monkeypatch.setitem(browser_lightpanda._servers, "live", server)+ with observe_file_writes(browser_lightpanda.reap_orphaned_lightpanda):+ browser_lightpanda._write_record(server)+ record = browser_lightpanda._state_dir() / "live.json"+ assert record.exists()+ assert json.loads(record.read_text(encoding="utf-8"))["port"] == 9222diff --git a/tools/browser_lightpanda.py b/tools/browser_lightpanda.pyindex 904eda56ce6..2c4f8c6578a 100644--- a/tools/browser_lightpanda.py+++ b/tools/browser_lightpanda.py@@ -19,6 +19,8 @@ from dataclasses import dataclassfrom pathlib import Pathfrom typing import Dict, Optional, Tuple+from utils import atomic_json_write+logger = logging.getLogger(__name__)LIGHTPANDA_INSTALL_URL = "https://lightpanda.io/docs/run-locally/installation/one-liner"@@ -189,7 +191,9 @@ def _write_record(server: LightpandaServer) -> None:record = {"pid": server.proc.pid, "port": server.port, "owner_pid": os.getpid(),"start_time": server.start_time, "started_at": time.time()}try:- (_state_dir() / f"{server.session_name}.json").write_text(json.dumps(record), encoding="utf-8")+ # Temp + rename: an orphan sweep (another Hermes, or a cleanup thread) unlinks a record it+ # cannot parse, so a torn in-place write would drop a live server's record.+ atomic_json_write(_state_dir() / f"{server.session_name}.json", record, indent=None)except OSError as e:logger.debug("could not write lightpanda record for %s: %s", server.session_name, e)