fix: add missing self._lock to KnowledgeGraph.close()

TDD: test first, failed, fixed, passed.

Igor fixed query_relationship/timeline/stats in an earlier commit.
close() was the last method touching self._connection without
holding the lock.

Closes #883.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
MSL
2026-04-14 13:09:10 -07:00
parent 205867a09f
commit 3094c0bd10
2 changed files with 17 additions and 3 deletions
+4 -3
View File
@@ -99,9 +99,10 @@ class KnowledgeGraph:
def close(self): def close(self):
"""Close the database connection.""" """Close the database connection."""
if self._connection is not None: with self._lock:
self._connection.close() if self._connection is not None:
self._connection = None self._connection.close()
self._connection = None
def _entity_id(self, name: str) -> str: def _entity_id(self, name: str) -> str:
return name.lower().replace(" ", "_").replace("'", "") return name.lower().replace(" ", "_").replace("'", "")
+13
View File
@@ -0,0 +1,13 @@
"""TDD: KnowledgeGraph.close() must hold self._lock."""
import inspect
from mempalace.knowledge_graph import KnowledgeGraph
class TestKGCloseLock:
def test_close_holds_lock(self):
src = inspect.getsource(KnowledgeGraph.close)
assert "self._lock" in src, (
"close() does not acquire self._lock. "
"Closing while a read/write is in progress can corrupt data."
)