Skip to content

Personal AI, On Personal Devices

Python

Close KnowledgeStore connections in connectors_router endpoints (#209)

Jack Hamilton9e7366fc05d8
diff --git a/src/openjarvis/connectors/store.py b/src/openjarvis/connectors/store.py
index 2e0cfbc8..24df031b 100644
--- a/src/openjarvis/connectors/store.py
+++ b/src/openjarvis/connectors/store.py
@@ -132,6 +132,12 @@ class KnowledgeStore(MemoryBackend):
self._conn.row_factory = sqlite3.Row
self._setup()
+ def __enter__(self) -> "KnowledgeStore":
+ return self
+
+ def __exit__(self, *exc_info: object) -> None:
+ self.close()
+
# ------------------------------------------------------------------
# Internal setup
# ------------------------------------------------------------------
diff --git a/src/openjarvis/server/connectors_router.py b/src/openjarvis/server/connectors_router.py
index bac50057..1738459e 100644
--- a/src/openjarvis/server/connectors_router.py
+++ b/src/openjarvis/server/connectors_router.py
@@ -108,12 +108,12 @@ def create_connectors_router():
try:
from openjarvis.connectors.store import KnowledgeStore
- store = KnowledgeStore()
- rows = store._conn.execute(
- "SELECT COUNT(*) FROM knowledge_chunks WHERE source = ?",
- (connector_id,),
- ).fetchone()
- chunks = rows[0] if rows else 0
+ with KnowledgeStore() as store:
+ rows = store._conn.execute(
+ "SELECT COUNT(*) FROM knowledge_chunks WHERE source = ?",
+ (connector_id,),
+ ).fetchone()
+ chunks = rows[0] if rows else 0
except Exception:
pass
@@ -260,10 +260,10 @@ def create_connectors_router():
from openjarvis.connectors.store import KnowledgeStore
from openjarvis.connectors.sync_engine import SyncEngine
- store = KnowledgeStore()
- pipeline = IngestionPipeline(store)
- engine = SyncEngine(pipeline)
- engine.sync(instance)
+ with KnowledgeStore() as store:
+ pipeline = IngestionPipeline(store)
+ engine = SyncEngine(pipeline)
+ engine.sync(instance)
logger.info(
"Auto-ingested %s after connect",
connector_id,
@@ -479,10 +479,10 @@ def create_connectors_router():
from openjarvis.connectors.store import KnowledgeStore
from openjarvis.connectors.sync_engine import SyncEngine
- store = KnowledgeStore()
- pipeline = IngestionPipeline(store=store)
- engine = SyncEngine(pipeline=pipeline)
- engine.sync(inst)
+ with KnowledgeStore() as store:
+ pipeline = IngestionPipeline(store=store)
+ engine = SyncEngine(pipeline=pipeline)
+ engine.sync(inst)
logger.info("Sync completed for %s", connector_id)
_sync_state[connector_id] = {"state": "complete", "error": None}
except Exception as exc:
diff --git a/tests/connectors/test_store.py b/tests/connectors/test_store.py
index 3b0b7eca..c7f2f354 100644
--- a/tests/connectors/test_store.py
+++ b/tests/connectors/test_store.py
@@ -333,3 +333,27 @@ def test_memory_retrieve_event_emitted(tmp_path: Path) -> None:
types = [e.event_type for e in bus.history]
assert EventType.MEMORY_RETRIEVE in types
+
+
+def test_context_manager_closes_connection(tmp_path: Path) -> None:
+ """Used as a context manager, KnowledgeStore closes its connection on exit."""
+ import sqlite3
+
+ with KnowledgeStore(db_path=tmp_path / "ctx.db") as ks:
+ _store(ks, content="inside with", source="test")
+
+ with pytest.raises(sqlite3.ProgrammingError):
+ ks._conn.execute("SELECT 1")
+
+
+def test_context_manager_closes_on_exception(tmp_path: Path) -> None:
+ """The connection is closed even when the with block raises."""
+ import sqlite3
+
+ ks = KnowledgeStore(db_path=tmp_path / "ctx_exc.db")
+ with pytest.raises(RuntimeError):
+ with ks:
+ raise RuntimeError("boom")
+
+ with pytest.raises(sqlite3.ProgrammingError):
+ ks._conn.execute("SELECT 1")