Skip to content

⚡ Bolt: Use AsyncGroq and offload SQLite to unblock FastAPI event loop#84

Open
Adityasingh-8858 wants to merge 1 commit into
mainfrom
bolt-unblock-fastapi-loop-7830573953371600428
Open

⚡ Bolt: Use AsyncGroq and offload SQLite to unblock FastAPI event loop#84
Adityasingh-8858 wants to merge 1 commit into
mainfrom
bolt-unblock-fastapi-loop-7830573953371600428

Conversation

@Adityasingh-8858
Copy link
Copy Markdown
Collaborator

💡 What:

  • Swapped the synchronous Groq client for AsyncGroq in the /ai-voice and /initiate-transfer endpoints.
  • Wrapped blocking SQLite database calls (create_transfer_record, set_agent_b, list_transfers, get_transfer) in asyncio.to_thread.
  • Documented these architectural performance constraints in the .jules/bolt.md journal.

🎯 Why:
The FastAPI framework operates on a single-threaded asynchronous event loop. Calling synchronous network operations or blocking local file I/O completely halted the event loop while those tasks completed, devastating concurrency. By making these I/O operations asynchronous or offloading them to threads, the event loop remains responsive and can serve other concurrent requests efficiently.

📊 Impact:
Massively improves application throughput under load. It prevents AI generation (which can take hundreds of milliseconds) and local DB writes from stalling all other active HTTP connections.

🔬 Measurement:
Load testing with multiple concurrent requests to the /health endpoint while simultaneously requesting an /ai-voice generation. Prior to this change, the health checks would hang until the generation finished. Now, they return immediately. You can verify this logic directly in backend/main.py.


PR created automatically by Jules for task 7830573953371600428 started by @Deepaksingh7238

- Replaced synchronous `Groq` client with `AsyncGroq` in `backend/main.py`.
- Offloaded blocking SQLite calls using `asyncio.to_thread`.
- Added journal entry for event loop optimization learning.

Co-authored-by: Deepaksingh7238 <110552872+Deepaksingh7238@users.noreply.github.com>
@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.

Copilot AI review requested due to automatic review settings May 1, 2026 16:13
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 improves FastAPI concurrency by making Groq LLM calls non-blocking (switching to AsyncGroq) and offloading synchronous SQLite persistence operations to background threads, and records the rationale in the Bolt journal.

Changes:

  • Replace sync Groq usage with AsyncGroq + await in /ai-voice and /initiate-transfer.
  • Offload blocking SQLite persistence calls (create_transfer_record, set_agent_b, list_transfers, get_transfer) via asyncio.to_thread.
  • Add .jules/bolt.md documenting the event-loop blocking constraint and the chosen mitigation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
backend/main.py Uses AsyncGroq and asyncio.to_thread to avoid blocking the FastAPI event loop during network + SQLite I/O.
.jules/bolt.md Documents the async/performance rationale and recommended patterns.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/main.py
Comment on lines 504 to +506
global groq_client
if groq_client is None:
groq_client = Groq(api_key=GROQ_API_KEY)
groq_client = AsyncGroq(api_key=GROQ_API_KEY)
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

Same concurrency issue as in /ai-voice: concurrent /initiate-transfer requests can race on the global groq_client initialization and create multiple AsyncGroq clients. Use a shared initialization helper with a lock, or create/attach the client once in lifespan startup to ensure only one instance per process.

Copilot uses AI. Check for mistakes.
Comment thread backend/main.py
Comment on lines 9 to +10
import asyncio
from groq import Groq
from groq import AsyncGroq
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

AsyncGroq likely manages an underlying async HTTP client; with the switch from the sync Groq client, the app should close groq_client on shutdown (e.g., in lifespan after yield, call await groq_client.aclose() if it exists) to avoid leaking connections/file descriptors during reloads or graceful shutdown.

Copilot uses AI. Check for mistakes.
Comment thread backend/main.py
Comment on lines 317 to +320
global groq_client
if groq_client is None:
groq_client = Groq(api_key=GROQ_API_KEY)
chat_completion = groq_client.chat.completions.create(
groq_client = AsyncGroq(api_key=GROQ_API_KEY)
chat_completion = await groq_client.chat.completions.create(
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

The lazy initialization of the global groq_client is not concurrency-safe: multiple concurrent requests can observe groq_client is None and create multiple AsyncGroq instances, potentially leaking connections. Consider guarding initialization with an asyncio.Lock or initializing the client once during app startup (lifespan) and reusing it via app.state.

Copilot uses AI. Check for mistakes.
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