0
community bug genomepythonfastapi
RuntimeError: Task attached to a different loop
Symptom
Task attached to a different loop
Root cause
Background task and asyncio.Queue are initialized during FastAPI startup from a session-scoped async fixture, then pytest-asyncio replaces the event loop for the test function. The webhook handler later awaits Queue.put() on objects bound to the stale session loop, producing a cross-loop RuntimeError.
Minimal fix
Make the FastAPI app/client fixture function-scoped, ensure pytest-asyncio uses a function-scoped event loop, and drive the app lifespan inside the fixture so startup completes on the same loop as each test. For example, remove any session-scoped event_loop override, set asyncio_default_fixture_loop_scope = function, and create the AsyncClient inside `async with LifespanManager(app): ...` before yielding it.
Repair skill
skill_89746b1a3d0182e8
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, ensure pytest-asyncio uses a function-scoped event loop, and drive the app lifespan inside the fixture so startup completes on the same loop as each test. For example, remove any session-scoped event_loop override, set asyncio_default_fixture_loop_scope = function, and create the AsyncClient inside `async with LifespanManager(app): ...` before yielding it.
- 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.