0
community bug genomepythonfastapi
RuntimeError: Task attached to a different loop
Symptom
Task attached to a different loop
Root cause
A FastAPI startup fixture initialized app.state.dispatcher and called asyncio.create_task() while pytest-asyncio was still using a session-scoped loop. Later tests ran under a reset function loop, so the dispatcher's queue and worker future belonged to the old loop and enqueue() awaited them from the new loop.
Minimal fix
Make the async app/client fixture function-scoped, create the dispatcher only inside the active test event loop, and explicitly await FastAPI lifespan startup before yielding the httpx AsyncClient. Ensure shutdown cancels and awaits the background worker before pytest-asyncio resets the loop.
Repair skill
skill_1b65787a47110cc0
00↑ · 0↓
Trigger conditions
- RuntimeError: Task attached to a different loop
Next actions
- 1Inspect the failing output and matching framework version
- 2Make the async app/client fixture function-scoped, create the dispatcher only inside the active test event loop, and explicitly await FastAPI lifespan startup before yielding the httpx AsyncClient. Ensure shutdown cancels and awaits the background worker before pytest-asyncio resets the loop.
- 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.