SQLite disk I/O error in daemon heartbeat thread during sync
Problem
During run_sync_once(), the heartbeat background thread (_heartbeat_loop in daemon.py:111) corrupts the SQLite sessions database (~/.lerim/index/sessions.sqlite3). The error occurs consistently across fresh DBs, different journal modes (WAL and DELETE), and after removing macOS extended attributes.
Error
sqlite3.OperationalError: disk I/O error
or
sqlite3.DatabaseError: database disk image is malformed
At catalog.py:782 (heartbeat_session_job) and catalog.py:801 (complete_session_job).
Root Cause
The heartbeat runs in a separate daemon thread (threading.Thread) that writes to the same SQLite DB as the main thread. Both use _connect() which creates new connections, but SQLite's WAL mode + macOS APFS + com.apple.provenance extended attribute creates a race condition that corrupts the journal.
Reproduction
from lerim.app.daemon import run_sync_once
run_sync_once(
run_id=None, agent_filter=None, no_extract=False,
force=False, max_sessions=5, dry_run=False,
ignore_lock=True, trigger='manual',
)
Fails after 1-3 sessions with disk I/O error.
Suggested Fix
Options:
- Catch
sqlite3.OperationalErrorin_heartbeat_loopand retry with backoff - Use a single shared connection with a threading lock instead of per-call
_connect() - Move heartbeat to use a separate lightweight file (touch-based) instead of SQLite writes
- Skip heartbeat entirely for short-lived one-shot runs (
ignore_lock=True)
Files
src/lerim/app/daemon.pylines 108-115 (_heartbeat_loop)src/lerim/sessions/catalog.pylines 775-810 (heartbeat_session_job,complete_session_job)
🤖 Generated with Claude Code