fix(perf): offload sync SQLite in async task/costs handlers (#751)#831
Conversation
The read/CRUD task handlers and the costs handlers did blocking SQLite work directly inside async def, pinning that disk I/O to the event-loop thread and serializing every concurrent request behind it. Declare the purely-synchronous handlers plain def so FastAPI dispatches them to its anyio threadpool (the acceptance-criteria option, and the pattern already used in interactive_sessions_v2.py). slowapi's rate-limit decorator mirrors the wrapped function's sync/async nature, so rate limiting is unaffected. update_task's GitHub auto-close still fires correctly: core's _close_issue_background already handles the no-running-loop case by spawning a non-daemon thread. Converted: list_tasks, get_task, update_task, delete_task, get_assignment_status, get_task_run, get_costs_summary, get_costs_by_task, get_costs_by_agent_endpoint. Execution-control and streaming handlers stay async (out of scope). Demo: 5 concurrent requests with 0.4s DB each -> 0.41s elapsed (parallel) vs 2.0s if serialized.
|
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in: 41 minutes Your organization has reached its usage spending cap. Adjust your spending cap in the billing tab. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
ReviewThis is my first pass on this PR (no earlier review from me to reconcile with — the only other comment is CodeRabbit's rate-limit notice, which carries no content). Scope and approach match Verified by reading the diff and the relevant code paths (couldn't execute pytest in this sandbox):
Minor, non-blocking observations:
No correctness, security, or test-coverage issues found beyond the above. The regression test is a good structural guard (asserts sync-ness by name rather than re-testing business logic already covered elsewhere), and the PR description's concurrency demo + reasoning about slowapi/auto-close is accurate based on the code as it stands. 🤖 Generated with Claude Code |
Addresses claude[bot] review note: extend the stay-async guard from 3 to all 7 handlers named in the PR's Known Limitations (adds start_execution, stop_task, resume_task, approve_tasks_endpoint), so an accidental def-flip of any of them is caught.
|
Thanks for the review. Triage of the two non-blocking notes:
|
Follow-up reviewBoth items from my previous pass are resolved:
No new issues in the diff. The 9 handler conversions ( 🤖 Generated with Claude Code |
Closes #751
Problem
async defhandlers intasks_v2.pyandcosts_v2.pycalled blocking SQLitedirectly, pinning that disk I/O to the event-loop thread and serializing every
concurrent request behind it.
Fix
Declare the purely-synchronous handlers plain
def. FastAPI then dispatches themto its anyio threadpool instead of the event loop — the acceptance-criteria
option ("handlers declared plain
def") and the pattern already used ininteractive_sessions_v2.py.Converted (9 handlers):
tasks_v2.py:list_tasks,get_task,update_task,delete_task,get_assignment_status,get_task_runcosts_v2.py:get_costs_summary,get_costs_by_task,get_costs_by_agent_endpointWhy this is safe
slowapi.Limiter.limitbranches oniscoroutinefunction, producing a sync wrapper for sync handlers (extension.py:717).update_task→tasks.update_statusfires_close_issue_background, which already handles the no-running-loop case (threadpool thread) by spawning a non-daemon thread — the same path the CLI uses.awaitexisted in any converted handler, so nothing needed unwinding.Evidence
Regression test (
tests/ui/test_async_handler_offload_751.py): structurallyasserts the 9 handlers are sync and that streaming/orchestration handlers stay async.
Concurrency demo — 5 concurrent requests, 0.4s blocking DB call each:
Before the fix these would serialize to ~2.0s on the event loop.
Existing suites: 121 passed (
test_costs_v2,test_v2_routers_integration,test_tasks_v2_engine,test_tasks_start_failure) + ruff clean.Known Limitations
asyncby design —start_execution,start_single_task,stop_task,resume_task,approve_tasks_endpoint,stream_task_output_lines,stream_task_events.These orchestrate the conductor/runtime, spawn worker threads, use
BackgroundTasks, or returnStreamingResponse, and rely on the running eventloop. They're outside the cited "blocking SQLite serializes requests" scope and
converting them carries real risk.