Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(db): add WAL checkpoint before engine disposal (#3198)

LearningCircuitab0df8cfeddf
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index 6f83cd703f..c19ce464b4 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -709,11 +709,32 @@ class DatabaseManager:
with self._connections_lock:
return set(self.connections.keys())
+ def _checkpoint_wal(self, engine, context: str = ""):
+ """Checkpoint WAL before disposing engine to flush pending writes."""
+ try:
+ with engine.connect() as conn:
+ # TRUNCATE returns (busy, log_pages, checkpointed_pages).
+ # busy=1 means a reader/writer held a WAL lock and the WAL
+ # was NOT truncated — surface that so we can tell from logs
+ # whether the helper actually shrank the file.
+ result = conn.execute(
+ text("PRAGMA wal_checkpoint(TRUNCATE)")
+ ).fetchone()
+ if result and result[0] == 1:
+ logger.debug(
+ f"WAL checkpoint busy {context} — WAL not truncated"
+ )
+ except Exception:
+ logger.debug(f"WAL checkpoint failed {context}", exc_info=True)
+
def close_user_database(self, username: str):
"""Close a user's database connection."""
with self._connections_lock:
if username in self.connections:
try:
+ self._checkpoint_wal(
+ self.connections[username], f"for {username}"
+ )
self.connections[username].dispose()
except Exception:
logger.warning(
@@ -727,6 +748,7 @@ class DatabaseManager:
with self._connections_lock:
for username, engine in list(self.connections.items()):
try:
+ self._checkpoint_wal(engine, f"for {username}")
engine.dispose()
except Exception:
logger.debug(f"Error disposing engine for {username}")
diff --git a/src/local_deep_research/web/auth/connection_cleanup.py b/src/local_deep_research/web/auth/connection_cleanup.py
index b65d31ee52..29aa48cb9e 100644
--- a/src/local_deep_research/web/auth/connection_cleanup.py
+++ b/src/local_deep_research/web/auth/connection_cleanup.py
@@ -159,6 +159,7 @@ def cleanup_idle_connections(session_manager, db_manager):
with db_manager._connections_lock:
for username, engine in list(db_manager.connections.items()):
try:
+ db_manager._checkpoint_wal(engine, f"for {username}")
engine.dispose()
disposed += 1
except Exception: