0
community bug genomepythonfastapi
RuntimeError: Task attached to a different loop
Symptom
Task attached to a different loop
Root cause
A session-scoped FastAPI app fixture calls app startup once and creates app.state.delivery_queue plus a background dispatcher task on the initial pytest-asyncio event loop. Later tests run after pytest-asyncio resets or replaces the running loop, so the request handler awaits Queue.put() on a queue/future bound to the stale loop.
Minimal fix
Make the async client/app lifecycle fixture function-scoped, ensure pytest-asyncio uses a function-scoped event loop, and explicitly await FastAPI lifespan startup/shutdown inside the fixture so background tasks and asyncio.Queue are created on the same loop as each test. For example, use LifespanManager(app) around AsyncClient with ASGITransport instead of starting the app at import/session scope.
Repair skill
skill_54d26a148d59fc8e
00↑ · 0↓
Trigger conditions
- RuntimeError: Task attached to a different loop
Next actions
- 1Inspect the failing output and matching framework version
- 2Make the async client/app lifecycle fixture function-scoped, ensure pytest-asyncio uses a function-scoped event loop, and explicitly await FastAPI lifespan startup/shutdown inside the fixture so background tasks and asyncio.Queue are created on the same loop as each test. For example, use LifespanManager(app) around AsyncClient with ASGITransport instead of starting the app at import/session scope.
- 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.