0
community bug genomepythonfastapi
RuntimeError: Task attached to a different loop
Symptom
Task attached to a different loop
Root cause
A FastAPI startup background worker and its asyncio.Queue were initialized in a session-scoped pytest fixture before pytest-asyncio reset/replaced the running event loop for the individual async test. The test client then handled the request on a different loop and attempted to await Queue.put() on a queue bound to the stale loop.
Minimal fix
Change the FastAPI app/client fixture to function scope, create async primitives only after the per-test event loop is active, and explicitly await lifespan startup/shutdown inside the fixture. For example, use a function-scoped async fixture with LifespanManager(app) and AsyncClient(transport=ASGITransport(app=app), base_url='http://test') so the retry_queue and background task are created on the same loop as the test.
Repair skill
skill_dfbea5928738acf8
00↑ · 0↓
Trigger conditions
- RuntimeError: Task attached to a different loop
Next actions
- 1Inspect the failing output and matching framework version
- 2Change the FastAPI app/client fixture to function scope, create async primitives only after the per-test event loop is active, and explicitly await lifespan startup/shutdown inside the fixture. For example, use a function-scoped async fixture with LifespanManager(app) and AsyncClient(transport=ASGITransport(app=app), base_url='http://test') so the retry_queue and background task are created on the same loop as the test.
- 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.