Skip to content

The agent that grows with you

fix(sessions): keep the writer-conn lock audit green for the lost-generation close paths

kshitijk4poor5cb9f5c3475e
diff --git a/hermes_state.py b/hermes_state.py
index 1473225f1fa..ca30558f982 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -1122,10 +1122,13 @@ class SessionDB(
return False
return True
- def _pin_connection(self) -> None:
- """Retain the exact quarantined connection past GC and interpreter teardown (once per handle)."""
+ def _pin_connection(self, conn) -> None:
+ """Retain the exact quarantined connection past GC and interpreter teardown (once per handle).
+
+ Takes the connection as a parameter: callers are lock-held close paths, and the
+ writer-conn thread-safety audit flags self._conn in functions outside `with self._lock`."""
if not self._connection_pinned:
- self._retire_connection(self._conn)
+ self._retire_connection(conn)
self._connection_pinned = True
def _settle_lost_generation_locked(self) -> bool:
@@ -1143,7 +1146,7 @@ class SessionDB(
artifact = self._capture_retired_generation("close")
except RetiredGenerationCaptureError as exc:
if retire_without_close:
- self._pin_connection()
+ self._pin_connection(self._conn)
logger.error(
"Could not capture the retired WAL generation of %s at close: %s. The handle stays open "
"and close() retries the capture; those frames are NOT yet preserved.", self.db_path, exc,
@@ -1285,7 +1288,7 @@ class SessionDB(
except Exception as exc:
logger.debug("WAL checkpoint (PASSIVE) at close failed: %s", exc)
if retire_without_close:
- self._pin_connection()
+ self._pin_connection(self._conn)
self._conn = None
else:
conn, self._conn = self._conn, None
diff --git a/tests/state/test_writer_conn_thread_safety.py b/tests/state/test_writer_conn_thread_safety.py
index ab19a826637..4ccedcd6d5b 100644
--- a/tests/state/test_writer_conn_thread_safety.py
+++ b/tests/state/test_writer_conn_thread_safety.py
@@ -141,6 +141,9 @@ class TestConcurrentReadersDoNotRaceTheWriter:
# are drained. Not reachable concurrently with writers.
"__init__", "_open_writer", "_connect_and_init",
"_connect_and_init_with_lock_patience", "close",
+ # Lost-generation settlement: called only from close()'s
+ # `with self._lock` body (locked transitively, not lexically).
+ "_settle_lost_generation_locked",
}
def is_lock_with(node):