Skip to content

The agent that grows with you

fix(state): reap only proven database holders

fangliquanflqfe2452560523
diff --git a/hermes_state.py b/hermes_state.py
index f4d6351aa92..843e01e038a 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -3946,6 +3946,29 @@ def _is_inactive_orphan_desktop_holder(
)
+def _concrete_state_db_holder_pids(
+ db_path: Path, holders: List[Tuple[int, str]]
+) -> List[int]:
+ """Return unique PIDs proven to hold this DB or one of its sidecars."""
+ canonical_db = os.path.normcase(os.path.abspath(os.fspath(db_path)))
+ watched = {
+ canonical_db,
+ canonical_db + "-wal",
+ canonical_db + "-shm",
+ }
+ pids: List[int] = []
+ seen = set()
+ for pid, path in holders:
+ canonical_path = os.path.normcase(
+ os.path.abspath(path.removesuffix(" (deleted)"))
+ )
+ if pid <= 0 or pid in seen or canonical_path not in watched:
+ continue
+ seen.add(pid)
+ pids.append(pid)
+ return pids
+
+
def _read_proc_cmdline(pid: int) -> Optional[str]:
"""Read /proc/<pid>/cmdline, world-readable even when fd table is not.
@@ -5184,9 +5207,7 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
now = time.time()
candidates = []
- for pid, _path in holders:
- if pid <= 0:
- continue
+ for pid in _concrete_state_db_holder_pids(self.db_path, holders):
try:
process = psutil.Process(pid)
statuses = [
diff --git a/tests/state/test_fts_runtime_rebuild.py b/tests/state/test_fts_runtime_rebuild.py
index a969d2bea8b..c93eb7bf131 100644
--- a/tests/state/test_fts_runtime_rebuild.py
+++ b/tests/state/test_fts_runtime_rebuild.py
@@ -30,6 +30,7 @@ from hermes_state import (
SCHEMA_SQL,
SessionDB,
_FTS_TRIGGERS,
+ _concrete_state_db_holder_pids,
_is_inactive_orphan_desktop_holder,
)
@@ -91,6 +92,31 @@ def _base_fts_triggers(db_path):
class TestRuntimeFtsRebuild:
+ def test_reap_candidates_exclude_uninspectable_holder_suspicions(
+ self, tmp_path
+ ):
+ db_path = tmp_path / "state.db"
+
+ assert _concrete_state_db_holder_pids(
+ db_path,
+ [
+ (222, "uninspectable holder: python -m hermes_cli.main serve --port 0"),
+ (-1, "open-file scan failed"),
+ ],
+ ) == []
+
+ def test_reap_candidates_deduplicate_multiple_proven_watched_fds(self, tmp_path):
+ db_path = tmp_path / "state.db"
+
+ assert _concrete_state_db_holder_pids(
+ db_path,
+ [
+ (222, str(db_path)),
+ (222, f"{db_path}-wal"),
+ (222, f"{db_path}-shm (deleted)"),
+ ],
+ ) == [222]
+
def test_inactive_orphan_reap_predicate_preserves_live_or_ambiguous_holders(self):
common = {
"ppid": 1,