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:
parent
ce7418e274
commit
efa778a0ef
1 changed files with 48 additions and 44 deletions
|
|
@ -809,6 +809,7 @@ class SessionDB:
|
|||
offset: int = 0,
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""List sessions, optionally filtered by source."""
|
||||
with self._lock:
|
||||
if source:
|
||||
cursor = self._conn.execute(
|
||||
"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:
|
||||
"""Delete all messages for a session and reset its counters."""
|
||||
with self._lock:
|
||||
self._conn.execute(
|
||||
"DELETE FROM messages WHERE session_id = ?", (session_id,)
|
||||
)
|
||||
|
|
@ -882,6 +884,7 @@ class SessionDB:
|
|||
|
||||
def delete_session(self, session_id: str) -> bool:
|
||||
"""Delete a session and all its messages. Returns True if found."""
|
||||
with self._lock:
|
||||
cursor = self._conn.execute(
|
||||
"SELECT COUNT(*) FROM sessions WHERE id = ?", (session_id,)
|
||||
)
|
||||
|
|
@ -900,6 +903,7 @@ class SessionDB:
|
|||
import time as _time
|
||||
cutoff = _time.time() - (older_than_days * 86400)
|
||||
|
||||
with self._lock:
|
||||
if source:
|
||||
cursor = self._conn.execute(
|
||||
"""SELECT id FROM sessions
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue