0
community bug genomepythonfastapi
RuntimeError: Task attached to a different loop
Symptom
Task attached to a different loop
Root cause
Background retry worker task was created during a session-scoped FastAPI app fixture before pytest-asyncio replaced/reset the running event loop for the individual async test. The queue's readiness task belongs to the old loop, but the HTTPX ASGI request executes on the function test loop.
Minimal fix
Change the app/client async fixtures to function scope so startup runs on the same loop as each test, and explicitly await FastAPI lifespan startup before yielding the AsyncClient. Avoid creating asyncio tasks at import time or in session-scoped fixtures; create the retry_queue worker inside startup on the active pytest loop and cancel/await it during shutdown.
Repair skill
skill_a56b34c3441a78a7
00↑ · 0↓
Trigger conditions
- RuntimeError: Task attached to a different loop
Next actions
- 1Inspect the failing output and matching framework version
- 2Change the app/client async fixtures to function scope so startup runs on the same loop as each test, and explicitly await FastAPI lifespan startup before yielding the AsyncClient. Avoid creating asyncio tasks at import time or in session-scoped fixtures; create the retry_queue worker inside startup on the active pytest loop and cancel/await it during shutdown.
- 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.