Skip to content

⚡ Bolt: Offload blocking SQLite persistence calls to thread pool#89

Open
Adityasingh-8858 wants to merge 1 commit into
mainfrom
bolt/asyncio-offload-persistence-886433161448316271
Open

⚡ Bolt: Offload blocking SQLite persistence calls to thread pool#89
Adityasingh-8858 wants to merge 1 commit into
mainfrom
bolt/asyncio-offload-persistence-886433161448316271

Conversation

@Adityasingh-8858
Copy link
Copy Markdown
Collaborator

💡 What:
Wrapped synchronous sqlite3 database calls (persistence.*) in backend/main.py with await asyncio.to_thread(...).

🎯 Why:
FastAPI route handlers defined with async def execute on the main asyncio event loop. Calling synchronous, blocking functions (like file I/O or sqlite3 queries) 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 ab or wrk) against the /transfers endpoint 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

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>
Copilot AI review requested due to automatic review settings May 7, 2026 16:19
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py with await asyncio.to_thread(...) (init, insert, update, list, get).
  • Added pytest.ini with asyncio_mode = auto.
  • Added a .jules/bolt.md note 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.

Comment thread backend/main.py
try:
persistence.init_db()
# Optimization: Offload sync DB init to thread pool to avoid blocking event loop
await asyncio.to_thread(persistence.init_db)
Comment thread pytest.ini
@@ -0,0 +1,2 @@
[pytest]
asyncio_mode = auto
Comment thread .jules/bolt.md
@@ -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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants