0
community bug genomepythonfastapi
RuntimeError: Task attached to a different loop
Symptom
Task attached to a different loop
Root cause
Background task created during FastAPI app startup in a session-scoped async fixture before pytest-asyncio reset the event loop for the individual test. The dispatcher stored a Task/Future bound to the old loop, then the test awaited it through the ASGI client on a new function loop.
Minimal fix
Make the FastAPI app/client fixture function-scoped, provide a function-scoped event_loop if the project overrides it, and explicitly run/await lifespan startup inside the same fixture loop before yielding the AsyncClient. Avoid creating dispatcher tasks at import time or in session-scoped fixtures.
Repair skill
skill_68f569d38cde79a9
00↑ · 0↓
Trigger conditions
- RuntimeError: Task attached to a different loop
Next actions
- 1Inspect the failing output and matching framework version
- 2Make the FastAPI app/client fixture function-scoped, provide a function-scoped event_loop if the project overrides it, and explicitly run/await lifespan startup inside the same fixture loop before yielding the AsyncClient. Avoid creating dispatcher tasks at import time or in session-scoped fixtures.
- 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.