Skip to content

The agent that grows with you

fix(state): keep the repair fingerprint from cancelling POSIX advisory locks

kshitijb3f14c853442
diff --git a/hermes_state.py b/hermes_state.py
index cad93e95c84..cfe94c98071 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -1845,8 +1845,8 @@ def _bump_schema_cookie(conn: sqlite3.Connection) -> None:
#
# * a sidecar attempt ledger (``<db>.repair-attempts.json``) that refuses
# further surgery after ``_MAX_PERSISTENT_REPAIR_ATTEMPTS`` failures on
-# the SAME damaged file (fingerprint = size + mtime; any successful repair
-# or replacement changes it and resets the count);
+# the SAME damaged file (fingerprint = size + a bounded content sample; any
+# successful repair or replacement changes it and resets the count);
# * backup dedupe + a retention cap in ``_backup_db_file`` — an identical
# damaged file is never copied twice, and only the newest
# ``_MAX_MALFORMED_BACKUPS`` forensic copies are kept.
@@ -1906,16 +1906,47 @@ def _db_fingerprint(db_path: Path) -> "Optional[str]":
size plus the head/tail slices that any real repair, truncation or
restore necessarily changes. Stable across passes that merely touch
mtime; still resets the attempt count after genuine recovery.
+
+ The content read runs under ``offline_file_access`` because it takes a raw
+ descriptor, and ``close()`` on ANY descriptor cancels every POSIX advisory
+ lock this process holds on the file — including a peer connection's
+ RESERVED lock (see ``hermes_cli.sqlite_safe_read`` rule 1). This function
+ is reached from ``repair_state_db_schema``'s exhaustion probe BEFORE
+ ``_backup_db_file``'s ``has_live_connection`` guard, and the repair path is
+ entered by one SessionDB while the gateway holds others, so a live peer is
+ the expected case rather than a theoretical one. When one exists we fall
+ back to ``size:mtime_ns``: a live connection means the backup is refused
+ and repair HARD STOPs (#69603), so no surgery runs and nothing moves mtime
+ behind our back — the content key is only load-bearing on the offline
+ repair path, which is exactly where the guard lets it through.
"""
try:
st = db_path.stat()
- with open(db_path, "rb") as fh:
- head = fh.read(_FINGERPRINT_SAMPLE_BYTES)
- if st.st_size > _FINGERPRINT_SAMPLE_BYTES:
- fh.seek(max(0, st.st_size - _FINGERPRINT_SAMPLE_BYTES))
- tail = fh.read(_FINGERPRINT_SAMPLE_BYTES)
- else:
- tail = b""
+ try:
+ from hermes_cli.sqlite_safe_read import (
+ LiveConnectionError,
+ offline_file_access,
+ )
+ except ImportError:
+ # Scaffold/embed installs ship hermes_state without hermes_cli. No
+ # tracked connections exist there, so the raw read is safe.
+ LiveConnectionError = () # type: ignore[assignment]
+ offline_file_access = None # type: ignore[assignment]
+ try:
+ with (
+ contextlib.nullcontext()
+ if offline_file_access is None
+ else offline_file_access(db_path, what="fingerprint")
+ ):
+ with open(db_path, "rb") as fh:
+ head = fh.read(_FINGERPRINT_SAMPLE_BYTES)
+ if st.st_size > _FINGERPRINT_SAMPLE_BYTES:
+ fh.seek(max(0, st.st_size - _FINGERPRINT_SAMPLE_BYTES))
+ tail = fh.read(_FINGERPRINT_SAMPLE_BYTES)
+ else:
+ tail = b""
+ except LiveConnectionError:
+ return f"{st.st_size}:{st.st_mtime_ns}"
digest = hashlib.sha256(head + tail).hexdigest()[:32]
return f"{st.st_size}:{digest}"
except OSError:
@@ -2108,8 +2139,20 @@ def _backup_db_file(db_path: Path) -> "Tuple[Optional[Path], Optional[str]]":
)
logger.error("Refusing forensic backup of %s: %s", db_path, reason)
return None, reason
- except OSError:
- pass
+ except OSError as exc:
+ # Fail CLOSED. This guard exists for the nearly-full volume, which
+ # is exactly where stat()/disk_usage() is most likely to fail — and
+ # proceeding would take the multi-GB copy that finishes off the
+ # disk. A refused backup is a HARD STOP (#69603), so repair simply
+ # does not run until a human frees space, which is the safe side.
+ reason = (
+ f"could not determine free space on {db_path.parent} ({exc}); "
+ "refusing the forensic copy rather than risk filling the "
+ f"volume. Free disk space, then retry (or recover manually "
+ f'with `sqlite3 {db_path} ".recover"`).'
+ )
+ logger.error("Refusing forensic backup of %s: %s", db_path, reason)
+ return None, reason
# Copy to a staging name that does NOT match the backup prefix, then
# rename into place only once every copy has succeeded. A copy that
# fails partway (ENOSPC, kill) would otherwise leave a prefix-matching
diff --git a/tests/test_state_db_repair_loop_mtime.py b/tests/test_state_db_repair_loop_mtime.py
index 7fc4b23fa53..b0fc6b8701c 100644
--- a/tests/test_state_db_repair_loop_mtime.py
+++ b/tests/test_state_db_repair_loop_mtime.py
@@ -23,6 +23,7 @@ from __future__ import annotations
import os
import shutil
+import sqlite3
import time
from pathlib import Path
from unittest.mock import patch
@@ -243,3 +244,129 @@ def test_repair_aborts_when_backup_refused_for_disk(tmp_path):
report = hermes_state.repair_state_db_schema(db)
assert not report.get("repaired")
assert "free" in (report.get("error") or "").lower()
+
+
+# ---------------------------------------------------------------------------
+# Lock safety: the content fingerprint must not cancel POSIX advisory locks
+# ---------------------------------------------------------------------------
+
+
+def test_fingerprint_takes_no_raw_fd_while_a_connection_is_live(tmp_path):
+ """The content read must not ``open()`` a DB that has a live connection.
+
+ ``close()`` on ANY descriptor cancels every POSIX advisory lock this
+ process holds on the file (https://sqlite.org/howtocorrupt.html), so a
+ peer connection's RESERVED lock is silently dropped and another process
+ can write into a file the holder still believes it owns. The exhaustion
+ probe runs BEFORE ``_backup_db_file``'s ``has_live_connection`` guard, so
+ the fingerprint has to guard itself.
+ """
+ import builtins
+
+ from hermes_cli.sqlite_safe_read import connect_tracked
+
+ db = tmp_path / "state.db"
+ conn = sqlite3.connect(str(db))
+ conn.execute("CREATE TABLE t(a)")
+ conn.commit()
+ conn.close()
+
+ live = connect_tracked(db, isolation_level=None, check_same_thread=False)
+ try:
+ opened: list[str] = []
+ real_open = builtins.open
+
+ def spy(target, *a, **kw):
+ if str(target).endswith("state.db"):
+ opened.append(str(target))
+ return real_open(target, *a, **kw)
+
+ with patch.object(builtins, "open", spy):
+ fp = _db_fingerprint(db)
+
+ assert not opened, f"raw fd taken on a live DB: {opened}"
+ # Must still return an identity, or the attempt ledger silently stops
+ # counting (fp None => "not exhausted" => the loop never terminates).
+ assert fp is not None
+ finally:
+ live.close()
+
+
+def test_live_connection_keeps_its_write_lock_across_a_repair_pass(tmp_path):
+ """End-to-end: a peer must not be able to steal the holder's write lock.
+
+ The peer runs in a SUBPROCESS on purpose. POSIX advisory locks are owned
+ per-process, so a same-process peer shares the holder's lock ownership and
+ cannot demonstrate the cancellation — it stays blocked either way, which
+ makes the test vacuous.
+
+ Rollback-journal mode only — WAL coordinates through ``-shm`` rather than
+ POSIX advisory locks, so it is immune. DELETE mode is what Hermes falls
+ back to on NFS/SMB/FUSE/ZFS and on SQLite builds vulnerable to the
+ WAL-reset bug, so it is a real deployment shape, not a corner case.
+ """
+ import subprocess
+ import sys
+ import textwrap
+
+ from hermes_cli.sqlite_safe_read import connect_tracked
+
+ db = tmp_path / "state.db"
+ conn = sqlite3.connect(str(db))
+ conn.execute("PRAGMA journal_mode=DELETE")
+ conn.execute("CREATE TABLE sessions(id TEXT)")
+ conn.commit()
+ conn.close()
+
+ peer_script = tmp_path / "peer.py"
+ peer_script.write_text(
+ textwrap.dedent(
+ """
+ import sqlite3, sys
+ con = sqlite3.connect(sys.argv[1], timeout=0.3, isolation_level=None)
+ try:
+ con.execute("BEGIN IMMEDIATE")
+ con.execute("INSERT INTO sessions VALUES('peer')")
+ con.execute("COMMIT")
+ print("WROTE")
+ except sqlite3.OperationalError:
+ print("BLOCKED")
+ """
+ )
+ )
+
+ def _peer_can_write() -> bool:
+ out = subprocess.run(
+ [sys.executable, str(peer_script), str(db)],
+ capture_output=True,
+ text=True,
+ timeout=60,
+ ).stdout.strip()
+ assert out in {"WROTE", "BLOCKED"}, f"unexpected peer output: {out!r}"
+ return out == "WROTE"
+
+ live = connect_tracked(db, isolation_level=None, check_same_thread=False)
+ try:
+ live.execute("BEGIN IMMEDIATE")
+ live.execute("INSERT INTO sessions VALUES('holder')")
+ assert not _peer_can_write(), "peer wrote before the fingerprint (bad fixture)"
+
+ _db_fingerprint(db)
+
+ assert not _peer_can_write(), (
+ "the fingerprint cancelled the holder's POSIX advisory lock"
+ )
+ live.execute("COMMIT")
+ finally:
+ live.close()
+
+
+def test_backup_refused_when_free_space_cannot_be_determined(tmp_path):
+ """Fail CLOSED: a nearly-full volume is where disk_usage is likeliest to
+ fail, and proceeding is the multi-GB copy that finishes off the disk."""
+ db = _damaged_db(tmp_path)
+ with patch("shutil.disk_usage", side_effect=OSError("statvfs failed")):
+ path, reason = _backup_db_file(db)
+ assert path is None
+ assert reason is not None and "free space" in reason.lower()
+ assert not _existing_malformed_backups(db)