fix(state): add missing thread locks to 4 SessionDB methods

search_sessions(), clear_messages(), delete_session(), and
prune_sessions() all accessed self._conn without acquiring self._lock.
Every other method in the class uses the lock. In multi-threaded
contexts (gateway serving concurrent platform messages), these
unprotected methods can cause sqlite3.ProgrammingError from concurrent
cursor operations on the same connection.
This commit is contained in:
teknium1 2026-03-17 03:50:06 -07:00
parent ce7418e274
commit efa778a0ef

View file

@ -809,6 +809,7 @@ class SessionDB:
offset: int = 0, offset: int = 0,
) -> List[Dict[str, Any]]: ) -> List[Dict[str, Any]]:
"""List sessions, optionally filtered by source.""" """List sessions, optionally filtered by source."""
with self._lock:
if source: if source:
cursor = self._conn.execute( cursor = self._conn.execute(
"SELECT * FROM sessions WHERE source = ? ORDER BY started_at DESC LIMIT ? OFFSET ?", "SELECT * FROM sessions WHERE source = ? ORDER BY started_at DESC LIMIT ? OFFSET ?",
@ -871,6 +872,7 @@ class SessionDB:
def clear_messages(self, session_id: str) -> None: def clear_messages(self, session_id: str) -> None:
"""Delete all messages for a session and reset its counters.""" """Delete all messages for a session and reset its counters."""
with self._lock:
self._conn.execute( self._conn.execute(
"DELETE FROM messages WHERE session_id = ?", (session_id,) "DELETE FROM messages WHERE session_id = ?", (session_id,)
) )
@ -882,6 +884,7 @@ class SessionDB:
def delete_session(self, session_id: str) -> bool: def delete_session(self, session_id: str) -> bool:
"""Delete a session and all its messages. Returns True if found.""" """Delete a session and all its messages. Returns True if found."""
with self._lock:
cursor = self._conn.execute( cursor = self._conn.execute(
"SELECT COUNT(*) FROM sessions WHERE id = ?", (session_id,) "SELECT COUNT(*) FROM sessions WHERE id = ?", (session_id,)
) )
@ -900,6 +903,7 @@ class SessionDB:
import time as _time import time as _time
cutoff = _time.time() - (older_than_days * 86400) cutoff = _time.time() - (older_than_days * 86400)
with self._lock:
if source: if source:
cursor = self._conn.execute( cursor = self._conn.execute(
"""SELECT id FROM sessions """SELECT id FROM sessions