⚡ Bolt: Offload synchronous SQLite persistence calls to avoid blocking event loop#82
⚡ Bolt: Offload synchronous SQLite persistence calls to avoid blocking event loop#82Adityasingh-8858 wants to merge 1 commit into
Conversation
…g event loop 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 improves FastAPI backend responsiveness by moving synchronous SQLite persistence operations off the asyncio event loop and into a thread pool, preventing event-loop blocking during DB reads/writes.
Changes:
- Offloaded SQLite writes (
create_transfer_record,set_agent_b) toawait asyncio.to_thread(...)in async endpoints. - Offloaded SQLite reads (
list_transfers,get_transfer) toawait asyncio.to_thread(...)to avoid blocking under concurrent load. - Added a Bolt journal entry documenting the rationale and the recommended pattern going forward.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| backend/main.py | Wrapes synchronous persistence calls in asyncio.to_thread within async FastAPI endpoints to avoid blocking the event loop. |
| .jules/bolt.md | Documents the learning and standard action (“always offload sync SQLite calls”) for future changes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
💡 What
Wrapped all direct SQLite persistence calls (
create_transfer_record,set_agent_b,list_transfers,get_transfer) inbackend/main.pyusingawait asyncio.to_thread(). Added an entry to Bolt's journal (.jules/bolt.md) explaining the rationale.🎯 Why
Executing standard Python synchronous SQLite operations inside FastAPI
async defendpoints blocks the entire asyncio event loop for the duration of the disk I/O. By offloading these operations to a thread pool withasyncio.to_thread, the main event loop remains free to handle concurrent incoming requests, significantly improving the backend's concurrency and responsiveness.📊 Impact
Prevents event loop starvation during database reads/writes. In high-concurrency situations, this allows the server to process concurrent API requests rather than blocking all connections while waiting for a single synchronous disk write to complete.
🔬 Measurement
Run the server under load or use a script (e.g.
test_persistence_sync.py) to verify that async tasks (like a heartbeat printer) continue firing seamlessly while database writes are occurring, confirming the event loop is no longer blocked. Verified all automated pytest suites passed flawlessly.PR created automatically by Jules for task 8555564245407436068 started by @Deepaksingh7238