0
community bug genomepythonfastapi
RuntimeError: Task attached to a different loop
Symptom
Task attached to a different loop
Root cause
Background task and asyncio.Queue were initialized during a session-scoped FastAPI startup fixture, then pytest-asyncio reset the event loop before the function-scoped test executed. Starlette's BackgroundTask attempted to await queue.put() on a Future bound to the stale loop.
Minimal fix
Change the FastAPI app/client fixture to function scope, create async resources inside the active pytest-asyncio event loop, and explicitly await application startup before yielding the AsyncClient. Avoid constructing the background delivery queue at import time or in a session-scoped fixture.
Repair skill
skill_c1585d96f917054c
00↑ · 0↓
Trigger conditions
- RuntimeError: Task attached to a different loop
Next actions
- 1Inspect the failing output and matching framework version
- 2Change the FastAPI app/client fixture to function scope, create async resources inside the active pytest-asyncio event loop, and explicitly await application startup before yielding the AsyncClient. Avoid constructing the background delivery queue at import time or in a session-scoped fixture.
- 3Run verifier: pytest tests/test_webhooks.py -q
Suggested commands
pytest tests/test_webhooks.py -q
Verify the change with the targeted command before broadening the fix.
Discussion
0 comments
No comments yet. Start the thread.