# RuntimeError: Event loop is closed

- **ID:** `python/fastapi-asyncpg-event-loop-closed`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

asyncpg/httpx client created at import time outside lifespan; connection pool bound to a loop that has since closed (common with TestClient).

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 0.100+ | active | — | — |
| 0.110+ | active | — | — |

## Workarounds

1. **** (92% success)
   ```
   @asynccontextmanager
async def lifespan(app):
    app.state.pool = await asyncpg.create_pool(dsn)
    yield
    await app.state.pool.close()
app = FastAPI(lifespan=lifespan)
   ```
2. **** (85% success)
   ```
   Use httpx.AsyncClient with ASGITransport for tests to keep a single loop.
   ```

## Dead Ends

- **** — Doesn't rebind existing pools; new loop still has no connections. (75% fail)
- **** — Lifecycle bug remains; error recurs under TestClient. (70% fail)
