Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/formal-cross-layer-tlc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 19 additions & 3 deletions api/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion scripts/ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
71 changes: 71 additions & 0 deletions tests/test_session_state_durability.py
Original file line number Diff line number Diff line change
@@ -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"]