Skip to content

fix: add missing self._lock to query_relationship, timeline, and stats in KnowledgeGraph#884

Merged
igorls merged 1 commit intoMemPalace:developfrom
shafdev:fix/kg-missing-lock
Apr 14, 2026
Merged

fix: add missing self._lock to query_relationship, timeline, and stats in KnowledgeGraph#884
igorls merged 1 commit intoMemPalace:developfrom
shafdev:fix/kg-missing-lock

Conversation

@shafdev
Copy link
Copy Markdown
Contributor

@shafdev shafdev commented Apr 14, 2026

What does this PR do?

Closes #883

KnowledgeGraph uses a single shared sqlite3.Connection opened with check_same_thread=False, protected by self._lock. Every write method (add_entity, add_triple, invalidate) and the query_entity read method correctly acquire self._lock before accessing the connection — but three read methods did not:

  • query_relationship()
  • timeline()
  • stats()

This means concurrent MCP tool calls (e.g. mempalace_kg_add and mempalace_kg_timeline arriving simultaneously) can interleave on the shared connection object, producing garbled results or a ProgrammingError: recursive use of cursor crash on some SQLite builds.

How to test

Run the code-inspection check on the unfixed upstream develop:

import inspect
from mempalace.knowledge_graph import KnowledgeGraph

for method in ["add_triple", "query_entity", "query_relationship", "timeline", "stats"]:
    src = inspect.getsource(getattr(KnowledgeGraph, method))
    has = "self._lock" in src
    print(f"{method:<25} {'✓ locked' if has else '✗ MISSING LOCK'}")

Before this PR you see ✗ MISSING LOCK for query_relationship, timeline, stats. After this PR all five show ✓ locked.

Or just run the full test suite — all 864 tests pass:

python3 -m pytest tests/ -v

Checklist

  • Tests pass (python -m pytest tests/ -v) — 864 passed
  • No hardcoded paths
  • Linter passes (ruff check . and ruff format --check .)

@igorls igorls added bug Something isn't working area/kg Knowledge graph labels Apr 14, 2026
@igorls igorls merged commit 0369227 into MemPalace:develop Apr 14, 2026
6 checks passed
igorls added a commit that referenced this pull request Apr 16, 2026
Bumps version across pyproject.toml, mempalace/version.py, README badge,
and uv.lock. Finalizes the 3.3.0 CHANGELOG section (was still labeled
'Unreleased') and adds a 3.3.1 section covering the multi-language
entity-detection infra and the five new locales landed since 2026-04-13.

Highlights:
- Multi-language entity detection infra (#911) + script-aware word
  boundaries for combining-mark scripts (#932) + BCP 47 case-insensitive
  locale resolution (#928) + i18n patterns wired into miner/palace/
  entity_registry (#931)
- Five new fully-supported locales: pt-br (#156), ru (#760), it (#907),
  hi (#773), id (#778)
- UTF-8 encoding fix on read_text() calls for non-UTF-8 Windows locales
  (#946)
- KnowledgeGraph lock correctness (#884, #887)
- Various smaller fixes and improvements
@igorls igorls mentioned this pull request Apr 16, 2026
8 tasks
shafdev pushed a commit to shafdev/mempalace that referenced this pull request Apr 17, 2026
Bumps version across pyproject.toml, mempalace/version.py, README badge,
and uv.lock. Finalizes the 3.3.0 CHANGELOG section (was still labeled
'Unreleased') and adds a 3.3.1 section covering the multi-language
entity-detection infra and the five new locales landed since 2026-04-13.

Highlights:
- Multi-language entity detection infra (MemPalace#911) + script-aware word
  boundaries for combining-mark scripts (MemPalace#932) + BCP 47 case-insensitive
  locale resolution (MemPalace#928) + i18n patterns wired into miner/palace/
  entity_registry (MemPalace#931)
- Five new fully-supported locales: pt-br (MemPalace#156), ru (MemPalace#760), it (MemPalace#907),
  hi (MemPalace#773), id (MemPalace#778)
- UTF-8 encoding fix on read_text() calls for non-UTF-8 Windows locales
  (MemPalace#946)
- KnowledgeGraph lock correctness (MemPalace#884, MemPalace#887)
- Various smaller fixes and improvements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/kg Knowledge graph bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: thread safety — query_relationship(), timeline(), stats() missing self._lock in KnowledgeGraph

2 participants