⚡ Bolt: Offload blocking SQLite persistence calls to thread pool#89
⚡ Bolt: Offload blocking SQLite persistence calls to thread pool#89Adityasingh-8858 wants to merge 1 commit into
Conversation
This commit refactors 6 synchronous `persistence` API calls in `backend/main.py` by wrapping them in `await asyncio.to_thread()`. This change prevents SQLite operations from blocking the FastAPI event loop, improving concurrency. Additionally, a `pytest.ini` was added to properly configure `pytest-asyncio` for the test suite. Co-authored-by: Deepaksingh7238 <110552872+Deepaksingh7238@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent FastAPI’s asyncio event loop from being blocked by synchronous SQLite persistence operations by offloading those calls to a thread pool.
Changes:
- Wrapped SQLite persistence calls in
backend/main.pywithawait asyncio.to_thread(...)(init, insert, update, list, get). - Added
pytest.iniwithasyncio_mode = auto. - Added a
.jules/bolt.mdnote documenting the rationale/pattern for offloading blocking I/O.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
backend/main.py |
Offloads synchronous persistence calls to a thread pool from async route handlers/lifespan. |
pytest.ini |
Adds pytest configuration for asyncio mode. |
.jules/bolt.md |
Documents the “offload blocking DB calls” learning/action item. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| try: | ||
| persistence.init_db() | ||
| # Optimization: Offload sync DB init to thread pool to avoid blocking event loop | ||
| await asyncio.to_thread(persistence.init_db) |
| @@ -0,0 +1,2 @@ | |||
| [pytest] | |||
| asyncio_mode = auto | |||
| @@ -0,0 +1,3 @@ | |||
| ## 2023-10-27 - Offload synchronous database calls to prevent event loop blocking | |||
| **Learning:** Calling synchronous blocking functions (like `sqlite3` persistence queries) directly inside FastAPI `async def` route handlers blocks the entire asyncio event loop, causing requests to queue up and dramatically reducing concurrency. | |||
| **Action:** Always wrap synchronous file I/O or database operations with `await asyncio.to_thread(func, *args)` when inside an asynchronous context in FastAPI to offload them to a separate thread pool. | |||
💡 What:
Wrapped synchronous
sqlite3database calls (persistence.*) inbackend/main.pywithawait asyncio.to_thread(...).🎯 Why:
FastAPI route handlers defined with
async defexecute on the mainasyncioevent loop. Calling synchronous, blocking functions (like file I/O orsqlite3queries) directly inside them blocks the entire event loop. This causes concurrent requests to queue up, significantly reducing application throughput and responsiveness. By offloading these blocking calls to a thread pool, the main event loop remains free to handle incoming connections.📊 Impact:
Substantial improvement in concurrent request handling. When under load, endpoints interacting with the database (
/initiate-transfer,/transfers, etc.) will no longer stall other asynchronous operations (like websockets or subsequent requests).🔬 Measurement:
This can be verified by running a concurrency test (e.g., using
aborwrk) against the/transfersendpoint while monitoring the latency of a separate, non-database asynchronous endpoint. The asynchronous test suite was also executed (PYTHONPATH=backend python -m pytest backend/) and all 11 tests passed successfully, confirming no regressions were introduced.PR created automatically by Jules for task 886433161448316271 started by @Deepaksingh7238