Skip to content

The agent that grows with you

fix(state): gate the lock-free read pool on a confirmed WAL header, not the assumed mode

teknium1295edf2557b9
diff --git a/hermes_state.py b/hermes_state.py
index 76fbaeaa9fc..8854972428c 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -53,7 +53,9 @@ from hermes_state_dbfile import (
RetiredGenerationCaptureError, capture_retired_wal_generation, refuse_deleted_wal_generation,
)
from hermes_state_messages import SessionMessagesMixin
-from hermes_state_wal import _WAL_INCOMPAT_MARKERS, apply_database_pragmas, apply_wal_with_fallback
+from hermes_state_wal import (
+ _WAL_INCOMPAT_MARKERS, _on_disk_journal_mode, apply_database_pragmas, apply_wal_with_fallback,
+)
from hermes_state_repair import _claim_repair_attempt, preflight_db_writability, repair_state_db_schema
from hermes_state_titles import SessionTitlesMixin
from hermes_state_usage import SessionUsageMixin
@@ -615,7 +617,12 @@ class SessionDB(
)
try:
conn.row_factory = sqlite3.Row
- self._wal_active = apply_wal_with_fallback(conn, db_label="state.db") == "wal"
+ mode = apply_wal_with_fallback(conn, db_label="state.db")
+ # "wal" is also the *assumed* mode when the on-disk probe was blocked by a concurrent opener
+ # (#86515): the lock-free mode=ro read pool needs a confirmed WAL header, so confirm it here.
+ # Unknown -> reads queue on the writer lock (slow but correct) instead of racing SQLITE_BUSY
+ # on a file that may really be in rollback-journal mode.
+ self._wal_active = mode == "wal" and _on_disk_journal_mode(conn) == "wal"
apply_database_pragmas(conn, db_label="state.db")
conn.execute("PRAGMA foreign_keys=ON")
self._fts_cjk_loaded = load_fts5_cjk_extension(conn)
diff --git a/tests/hermes_state/test_wal_active_confirmed.py b/tests/hermes_state/test_wal_active_confirmed.py
new file mode 100644
index 00000000000..144dca67ef1
--- /dev/null
+++ b/tests/hermes_state/test_wal_active_confirmed.py
@@ -0,0 +1,40 @@
+"""#86515: the lock-free mode=ro read pool needs a CONFIRMED WAL header. When the on-disk probe at open
+is blocked by a concurrent opener, apply_wal_with_fallback reports "wal" as the assumed mode with nothing
+touched; SessionDB must not turn that assumption into pooled readers on a file that is really DELETE."""
+import sqlite3
+
+from hermes_state import SessionDB
+
+
+def test_blocked_probe_on_delete_file_does_not_enable_read_pool(tmp_path):
+ path = tmp_path / "state.db"
+ db = SessionDB(db_path=path)
+ db.create_session("s", "cli")
+ db.close()
+ offline = sqlite3.connect(str(path)) # sole opener: a DELETE switch here is safe
+ assert offline.execute("PRAGMA journal_mode=DELETE").fetchone()[0].lower() == "delete"
+ offline.close()
+
+ sibling = sqlite3.connect(str(path), isolation_level=None)
+ sibling.execute("BEGIN EXCLUSIVE") # blocks the header read: the probe comes back None
+ try:
+ conn = db._open_writer_conn()
+ finally:
+ sibling.execute("ROLLBACK")
+ sibling.close()
+ try:
+ assert db._wal_active is False
+ assert db._checkout_read_conn() is None # reads queue on the writer lock, never SQLITE_BUSY
+ finally:
+ conn.close()
+
+
+def test_confirmed_wal_file_keeps_read_pool(tmp_path):
+ path = tmp_path / "state.db"
+ db = SessionDB(db_path=path)
+ try:
+ if sqlite3.connect(str(path)).execute("PRAGMA journal_mode").fetchone()[0].lower() != "wal":
+ return # vulnerable-SQLite or WAL-refusing filesystem: nothing to confirm
+ assert db._wal_active is True
+ finally:
+ db.close()