You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(routing): asyncio.gather without return_exceptions=True leaks coroutines on partial document fetch failure #251
route_query() in routing.py uses asyncio.gather(*fetch_coros) without return_exceptions=True. If any single document fetch raises DependencyUnavailable, asyncio.gather immediately propagates the exception and cancels all remaining in-flight fetch coroutines, leaving them un-awaited (resource leak) and giving the caller no way to distinguish a single-document transient error from a total outage.
Root Cause
backend/app/api/routing.py around line 95:
fetch_coros= [_fetch_doc(doc_id, tenant_id, store) fordoc_idindocument_ids]
docs=awaitasyncio..gather(*fetch_coros) # ← no return_exceptions=True
When _fetch_doc raises for doc_id[0], the gather cancels doc_id[1..N]'s coroutines mid-flight, and those coroutines' async DB sessions may not be properly released.
DB connection drops intermittently; _fetch_doc('doc_aaa') raises DependencyUnavailable.
asyncio.gather immediately propagates the exception; _fetch_doc('doc_bbb') and _fetch_doc('doc_ccc') are cancelled mid-flight.
Caller receives 503. But doc_bbb and doc_ccc are perfectly accessible.
Two SQLAlchemy async sessions are abandoned without close() → connection pool exhaustion under sustained traffic.
Proposed Fix
results=awaitasyncio.gather(*fetch_coros, return_exceptions=True)
# Separate exceptions from results:docs= []
fordoc_id, resultinzip(document_ids, results):
ifisinstance(result, DependencyUnavailable):
raiseresult# re-raise infrastructure errorelifisinstance(result, Exception):
raiseresult# unexpected errorelifresultisNone:
raisedocument_not_found(doc_id) # now we know WHICH doc failedelse:
docs.append(result)
This guarantees all coroutines run to completion (or explicit error), sessions are properly released, and the 404 message correctly names the missing doc_id.
Activity
piyushmakhija28 commentedon Jun 21, 2026
Fixed in PR #280 (Sprint R3/R4: fix(sprint-r3): root-cause fixes for all 39 bugs from code review round 3 + Sprint R4 round 4)