Skip to content

fix(database): resolve SQLite concurrency lock errors during parallel scans (#32) - #494

Merged
eshaanag merged 2 commits into
eshaanag:mainfrom
Myparadox-creator:fix/sqlite-concurrent-ingestion-lock-32
Sep 1, 2026
Merged

fix(database): resolve SQLite concurrency lock errors during parallel scans (#32)#494
eshaanag merged 2 commits into
eshaanag:mainfrom
Myparadox-creator:fix/sqlite-concurrent-ingestion-lock-32

Conversation

@Myparadox-creator

@Myparadox-creator Myparadox-creator commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Closes

#32

Problem

FastAPI handles requests asynchronously and spawns background tasks for repository ingestion and rescans. When multiple users submit repository scans concurrently, multiple tasks execute write operations against the SQLite database simultaneously. Since SQLite supports only a single concurrent writer and uncommitted transactions were held during long CPU metric extraction loops, this triggered \sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked\ errors, failing concurrent ingestion jobs.

Solution

  1. Config & Concurrency Throttling:

    • Added \MAX_CONCURRENT_INGESTIONS\ configuration parameter (default: \2) in \�ackend/config.py.
    • Introduced \get_ingestion_semaphore()\ in \�ackend/features/repo_ingestion/router.py\ to queue and throttle concurrent background ingestion and rescan jobs without overwhelming SQLite writer locks.
  2. Connection Parameters & PRAGMAs:

    • Configured SQLite connection pool with \connect_args={'check_same_thread': False, 'timeout': 60}.
    • Applied \PRAGMA journal_mode = WAL;, \PRAGMA busy_timeout = 30000;, and \PRAGMA synchronous = NORMAL;\ on all SQLite connections.
  3. Exponential Backoff Retry & Transaction Hardening:

    • Enhanced \commit_with_retry\ in \�ackend/database.py\ to handle transient \sqlite3.OperationalError\ locks with exponential backoff up to 5 retries.
    • Refactored _update_job\ to reuse open session objects when provided rather than opening conflicting simultaneous writer connections.
    • Replaced all raw \�wait db.commit()\ calls with \�wait commit_with_retry(db)\ across repository ingestion, rescans, cancellation, and deletion endpoints.
  4. Testing:

    • Added multi-worker concurrent write stress tests and semaphore verification in \�ackend/tests/test_sqlite_concurrency.py.
    • 100% test pass rate (290 backend pytest tests passing; 116 frontend vitest tests passing; frontend build clean).

@vercel

vercel Bot commented Sep 1, 2026

Copy link
Copy Markdown

@Myparadox-creator is attempting to deploy a commit to the Eshaan's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added backend Backend changes docs Documentation changes labels Sep 1, 2026
@github-actions github-actions Bot added the frontend Frontend changes label Sep 1, 2026
@eshaanag eshaanag added ECSoC26 Required tag — marks PR as counted for ECSoC 2026 scoring ECSoC26-L2 Medium — 10 pts (auto-applied by Sentinel) good-pr Bonus XP: exceptional PR (+15 XP) labels Sep 1, 2026
@eshaanag
eshaanag merged commit eac9652 into eshaanag:main Sep 1, 2026
12 of 13 checks passed
@ecsoc-sentinel ecsoc-sentinel Bot added ECSoC26-L3 Difficult — 15 pts (auto-applied by Sentinel) and removed ECSoC26-L2 Medium — 10 pts (auto-applied by Sentinel) labels Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend changes docs Documentation changes ECSoC26-L3 Difficult — 15 pts (auto-applied by Sentinel) ECSoC26 Required tag — marks PR as counted for ECSoC 2026 scoring frontend Frontend changes good-pr Bonus XP: exceptional PR (+15 XP)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants