# FastAPI "RuntimeError: There is no current event loop" in CI

> Fix "RuntimeError: There is no current event loop in thread" in CI - an async FastAPI test ran without pytest-asyncio managing a loop, or on a Python where get_event_loop no longer creates one.

Source: https://latchkey.dev/learn/python/fastapi-pytest-asyncio-no-current-event-loop-in-ci  
Updated: 2026-06-30

An async test or fixture called `asyncio.get_event_loop()` when no loop was running. On recent Python versions that no longer auto-creates a loop, and without pytest-asyncio driving the test, the coroutine has nothing to run on.

## Diagnose it: what did pytest actually collect?

Most pytest failures that only happen in CI are collection or import-path problems rather than test failures. The runner has a different working directory, a different `sys.path`, and usually no editable install, so a test module that imports your package locally may not resolve at all.

```Terminal
# what would run, without running it
pytest --collect-only -q | tail -20

# where pytest thinks the root is (drives conftest and import mode)
pytest --collect-only 2>&1 | grep -i rootdir

# is the package importable at all from the runner cwd?
python -c "import yourpackage, sys; print(yourpackage.__file__)"
python -c "import sys; print(sys.path)"
```

> Exit code 5 means zero tests were collected. It is a configuration result, not a passing run, and CI that only checks for a non-zero exit will treat it as success unless you assert on the collected count.

## Make a zero-test run fail the build

```Terminal
# fail explicitly when nothing is collected
pytest --strict-markers -q
test "${PIPESTATUS[0]}" -ne 5 || { echo "pytest collected no tests"; exit 1; }
```

## FAQ

### What causes FastAPI "RuntimeError: there is no current event loop" in CI?

There are 2 common causes: pytest-asyncio is not driving the async test and get_event_loop with no running loop on newer python. Without pytest-asyncio (and its mode), an async def test is collected but never awaited, so loop-dependent code fails.

### How do I fix FastAPI "RuntimeError: there is no current event loop" in CI?

There are 2 fixes depending on which cause you have: install and enable pytest-asyncio and get the running loop instead of a current one. Work through them in order, since the first is the most common.

### What does FastAPI "RuntimeError: there is no current event loop" in CI actually mean?

An async FastAPI test fails with "RuntimeError: There is no current event loop in thread 'MainThread'" or a coroutine "was never awaited" warning and no assertions run.

### How do I stop FastAPI "RuntimeError: there is no current event loop" in CI happening again?

Drive async tests with pytest-asyncio and an explicit mode. The prevention section lists 3 changes that keep it from recurring.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
