diff --git a/.github/workflows/formal-cross-layer-tlc.yml b/.github/workflows/formal-cross-layer-tlc.yml index fbb52287..8cd0f825 100644 --- a/.github/workflows/formal-cross-layer-tlc.yml +++ b/.github/workflows/formal-cross-layer-tlc.yml @@ -6,11 +6,13 @@ on: - "formal/**" - "api/core/broker.py" - "api/core/handover_recovery.py" + - "api/utils/files.py" - "api/routers/input.py" - "api/routers/lifecycle.py" - "api/server.py" - "tests/test_mvp_control_safety.py" - "tests/test_handover_restart_recovery.py" + - "tests/test_session_state_durability.py" - "scripts/ci/test.sh" - ".github/workflows/formal-cross-layer-tlc.yml" workflow_dispatch: diff --git a/api/utils/files.py b/api/utils/files.py index bf87730d..05219d0b 100644 --- a/api/utils/files.py +++ b/api/utils/files.py @@ -58,7 +58,15 @@ def flock(*_args): ] -def _atomic_write_text(path: str, content: str) -> None: +def _atomic_write_text( + path: str, content: str, *, durable_directory: bool = False +) -> None: + """Atomically replace one file; optionally persist its directory entry. + + File fsync protects the replacement contents, but POSIX rename durability + requires a parent-directory fsync before reporting a state transition + committed. Do not silently swallow a failed session-state commit. + """ directory = os.path.dirname(path) or "." os.makedirs(directory, exist_ok=True) fd, tmp_path = tempfile.mkstemp(prefix="tmp-", dir=directory, text=True) @@ -68,6 +76,12 @@ def _atomic_write_text(path: str, content: str) -> None: handle.flush() os.fsync(handle.fileno()) os.replace(tmp_path, path) + if durable_directory: + dir_fd = os.open(directory, os.O_RDONLY) + try: + os.fsync(dir_fd) + finally: + os.close(dir_fd) finally: if os.path.exists(tmp_path): os.unlink(tmp_path) @@ -406,7 +420,7 @@ def ensure_user_profile(user_dir: str) -> None: def write_session_dir(path: str) -> None: - _atomic_write_text(SESSION_FILE, path) + _atomic_write_text(SESSION_FILE, path, durable_directory=True) set_current_session_dir(path) @@ -752,7 +766,9 @@ def link_wine_user_dir(user_dir: str) -> None: def write_session_state(session_dir: str, state: str) -> None: - _atomic_write_text(os.path.join(session_dir, "session.state"), state) + _atomic_write_text( + os.path.join(session_dir, "session.state"), state, durable_directory=True + ) def get_instance_mode() -> str: diff --git a/scripts/ci/test.sh b/scripts/ci/test.sh index b2dca0f8..7a3e63d6 100755 --- a/scripts/ci/test.sh +++ b/scripts/ci/test.sh @@ -40,4 +40,5 @@ pytest \ tests/test_mvp_control_api.py \ tests/test_mvp_mouse_lease.py \ tests/test_mvp_inflight_cancel.py \ - tests/test_handover_restart_recovery.py + tests/test_handover_restart_recovery.py \ + tests/test_session_state_durability.py diff --git a/tests/test_session_state_durability.py b/tests/test_session_state_durability.py new file mode 100644 index 00000000..b7a6a261 --- /dev/null +++ b/tests/test_session_state_durability.py @@ -0,0 +1,71 @@ +"""Crash-durability checks for authoritative session pointer/state commits. + +Temporary synthetic files only; no host Wine profile, real session or input. +""" +import os +import stat +from unittest.mock import patch + +import pytest + +from api.utils import files + + +def test_session_pointer_and_state_fsync_parent_directories(tmp_path, monkeypatch): + actual_fsync = os.fsync + calls = [] + + def recorded_fsync(fd): + mode = os.fstat(fd).st_mode + calls.append("directory" if stat.S_ISDIR(mode) else "file") + return actual_fsync(fd) + + monkeypatch.setattr(files.os, "fsync", recorded_fsync) + with ( + patch.object(files, "SESSION_FILE", str(tmp_path / "active-session")), + patch.object(files, "set_current_session_dir"), + ): + files.write_session_dir(str(tmp_path / "A")) + assert calls == ["file", "directory"] + assert (tmp_path / "active-session").read_text() == str(tmp_path / "A") + + calls.clear() + state_dir = tmp_path / "A" + state_dir.mkdir() + files.write_session_state(str(state_dir), "active") + assert calls == ["file", "directory"] + assert (state_dir / "session.state").read_text() == "active" + + +def test_replace_failure_preserves_previous_state_and_removes_temp(tmp_path): + state_dir = tmp_path / "B" + state_dir.mkdir() + state = state_dir / "session.state" + state.write_text("suspended") + with patch.object(files.os, "replace", side_effect=OSError("synthetic crash")): + with pytest.raises(OSError, match="synthetic crash"): + files.write_session_state(str(state_dir), "active") + assert state.read_text() == "suspended" + assert sorted(p.name for p in state_dir.iterdir()) == ["session.state"] + + +def test_directory_fsync_failure_is_not_reported_as_committed(tmp_path, monkeypatch): + state_dir = tmp_path / "C" + state_dir.mkdir() + real_fsync = os.fsync + called_directory = [] + + def reject_directory(fd): + if stat.S_ISDIR(os.fstat(fd).st_mode): + called_directory.append(True) + raise OSError("directory sync unavailable") + return real_fsync(fd) + + monkeypatch.setattr(files.os, "fsync", reject_directory) + with pytest.raises(OSError, match="directory sync unavailable"): + files.write_session_state(str(state_dir), "active") + assert called_directory == [True] + # Rename may have happened before directory fsync failed. A new process + # must verify/reconcile state rather than assume this returned success. + assert (state_dir / "session.state").read_text() == "active" + assert sorted(p.name for p in state_dir.iterdir()) == ["session.state"]