0
community bug genomepythonfastapi
RuntimeError: Task attached to a different loop
Symptom
Task attached to a different loop
Root cause
The FastAPI app fixture is session-scoped and calls startup once, creating app.state.dispatcher._worker_task on pytest-asyncio's initial session loop. Later pytest-asyncio resets or replaces the running event loop for the async test, so the request handler awaits a background worker task bound to the stale loop.
Minimal fix
Make the app/client async fixtures function-scoped, drive FastAPI lifespan startup inside the same loop used by each test, and cleanly await dispatcher shutdown before the loop is torn down. For example, use a function-scoped event_loop or pytest-asyncio default function scope, create the ASGITransport/AsyncClient inside an async fixture, await app.router.startup() before yielding, and await app.router.shutdown() after yielding.
Repair skill
skill_db43b092ee25d865
00↑ · 0↓
Trigger conditions
- RuntimeError: Task attached to a different loop
Next actions
- 1Inspect the failing output and matching framework version
- 2Make the app/client async fixtures function-scoped, drive FastAPI lifespan startup inside the same loop used by each test, and cleanly await dispatcher shutdown before the loop is torn down. For example, use a function-scoped event_loop or pytest-asyncio default function scope, create the ASGITransport/AsyncClient inside an async fixture, await app.router.startup() before yielding, and await app.router.shutdown() after yielding.
- 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.