Skip to content

fix(state): add missing thread lock to session_count() and message_count() - #2288

Closed
Mibayy wants to merge 1 commit into
NousResearch:mainfrom
Mibayy:fix/session-message-count-thread-lock
Closed

fix(state): add missing thread lock to session_count() and message_count()#2288
Mibayy wants to merge 1 commit into
NousResearch:mainfrom
Mibayy:fix/session-message-count-thread-lock

Conversation

@Mibayy

@Mibayy Mibayy commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

Summary

session_count() and message_count() in SessionDB were accessing self._conn without holding self._lock, breaking the thread-safety guarantee documented on the class.

Problem

SessionDB docstring (line 111) explicitly states:

Thread-safe for the common gateway pattern (multiple reader threads, single writer via WAL mode). Each method opens its own cursor.

All 22 other database-accessing methods wrap their operations in with self._lock:. These two were the only exceptions.

In the gateway's multi-threaded environment (platform reader threads + writer thread), the race condition can cause:

  • Cursor interleaving between threads
  • sqlite3.ProgrammingError on concurrent connection access
  • Inconsistent COUNT(*) results

Fix

Wrap both methods with with self._lock:, identical to every other method in the class.

Changes

  • hermes_state.py lines 856–874: add with self._lock: to session_count() and message_count()

Closes #2130

…unt()

Both methods accessed self._conn without self._lock, breaking the
thread-safety contract documented on SessionDB (line 111). All 22 other
DB methods use with self._lock — these two were the only exceptions.

In the gateway's multi-threaded environment (multiple platform reader
threads + single writer) this could cause cursor interleaving,
sqlite3.ProgrammingError, or inconsistent COUNT results.

Closes NousResearch#2130
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #2466 — cherry-picked with authorship preserved. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: session_count() and message_count() missing thread lock

2 participants