lerim icon indicating copy to clipboard operation
lerim copied to clipboard

SQLite disk I/O error in daemon heartbeat thread during sync

Open kargarisaac opened this issue 8 months ago • 0 comments

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:

  1. Catch sqlite3.OperationalError in _heartbeat_loop and retry with backoff
  2. Use a single shared connection with a threading lock instead of per-call _connect()
  3. Move heartbeat to use a separate lightweight file (touch-based) instead of SQLite writes
  4. Skip heartbeat entirely for short-lived one-shot runs (ignore_lock=True)

Files

  • src/lerim/app/daemon.py lines 108-115 (_heartbeat_loop)
  • src/lerim/sessions/catalog.py lines 775-810 (heartbeat_session_job, complete_session_job)

🤖 Generated with Claude Code

kargarisaac avatar Mar 22 '26 16:03 kargarisaac