Error message

RuntimeError: Event loop is closed

Something asked asyncio to run work on a loop that has already been closed. The usual culprit is an object that remembers the loop it was first used in, such as an HTTP client with open connections, and that you then use from another loop: a second asyncio.run(), or a test client that starts a fresh loop for every call.

Updated

Examples run on 21 November 2026 with Python 3.13.18, FastAPI 0.165.0, httpx 0.28.1 and sentry-sdk 2.11.0, 2.40.0 and 2.75.0. Errilo is in private beta.

What the message says

The error is a plain RuntimeError raised by asyncio itself, in BaseEventLoop._check_closed, when code calls call_soon (or schedules a callback) on a closed loop. In our FastAPI test, the last frames of the stack are the ones below: the error comes from deep inside the HTTP library, not from your code, which is why the stack alone doesn’t point to the cause.

RuntimeError: Event loop is closed
  httpcore._async.http11        _response_closed
  httpcore._backends.anyio      aclose
  anyio._backends._asyncio      aclose
  asyncio.selector_events       abort
  asyncio.selector_events       _force_close
  asyncio.base_events           call_soon
  asyncio.base_events           _check_closed

Cause: an object outlives its loop

asyncio.run() creates a loop, runs your coroutine, then closes the loop. Objects created or used inside keep references to it. An httpx.AsyncClient keeps connections open between requests, and each connection is tied to the loop that opened it. Use the same client from a later loop and the first thing that touches a connection of the old loop fails.

It happens in three typical places:

Minimal reproduction

Two asyncio.run() calls, one client, against a local HTTP/1.1 server:

repro.py
import asyncio
import httpx

client = httpx.AsyncClient()   # its connections belong to the loop that uses it first


async def fetch():
    return (await client.get("http://127.0.0.1:8000/")).status_code


print(asyncio.run(fetch()))    # 200
print(asyncio.run(fetch()))    # RuntimeError: Event loop is closed

The first call returns 200. The second raises RuntimeError: Event loop is closed. The same cause inside a FastAPI app, with a module-level client:

main.py
# main.py
import httpx
from fastapi import FastAPI

app = FastAPI()
shared = httpx.AsyncClient()   # module-level client, reused by every request


@app.get("/proxy")
async def proxy():
    r = await shared.get("http://127.0.0.1:8000/")
    return {"status": r.status_code}
test_main.py
from fastapi.testclient import TestClient

client = TestClient(app, raise_server_exceptions=False)
[client.get("/proxy").status_code for _ in range(2)]   # [200, 500]

with TestClient(app) as client:   # one loop for the whole block
    ...

Without with, the first call answers 200 and the second 500. With with TestClient(app), both answer 200: the block keeps one loop for all the calls.

Fixes

One loop for everything that shares an object. In a script, a single asyncio.run(main()), with the client created and closed inside it:

repro.py
import asyncio
import httpx


async def main():
    async with httpx.AsyncClient() as client:   # created and closed inside the loop
        for _ in range(2):
            print((await client.get("http://127.0.0.1:8000/")).status_code)


asyncio.run(main())   # one call, one loop for the whole program

In FastAPI, create clients and pools in the lifespan, so they belong to the loop that serves the app, and close them there:

main.py
from contextlib import asynccontextmanager

import httpx
from fastapi import FastAPI


@asynccontextmanager
async def lifespan(app: FastAPI):
    app.state.http = httpx.AsyncClient()   # created in the loop that serves the app
    yield
    await app.state.http.aclose()


app = FastAPI(lifespan=lifespan)


@app.get("/proxy")
async def proxy():
    r = await app.state.http.get("http://127.0.0.1:8000/")
    return {"status": r.status_code}

In tests, use with TestClient(app) as client, which also runs the lifespan. With this version, three calls in a row answered 200 in our test.

What the Sentry SDK captures, and when

All of the following was observed with sentry_sdk.init() and nothing else, identically with 2.11.0, 2.40.0 and 2.75.0:

Events sent by sentry-sdk for “Event loop is closed”, by situation
SituationEvents
Second asyncio.run() in a script, the error caught by your own except0: the SDK never sees an exception you handle. With sentry_sdk.capture_exception() in the handler: 1, mechanism generic, handled
FastAPI route that uses the stale client (TestClient without with)1, mechanism starlette, unhandled, with the request; the response is a 500
A task that raised and was never awaited, logged by asyncio as “Task exception was never retrieved”1, from the logging integration (logger asyncio), handled. In our test the task raised the error on purpose, so this shows the mechanism for any task exception, not a case where asyncio itself raises this error
An AsyncClient never closed, its loop closed, then garbage collection0. We did not get Python to print an error here on Linux with httpx 0.28.1

In short, the SDK reports the error when it escapes a request or when asyncio logs it, and doesn’t when your code swallows it. The last case, where an error is printed at exit by an object’s destructor, is not something we reproduced: we can’t say whether it would be reported.

How Errilo shows it

The issue is titled RuntimeError: Event loop is closed. Errilo groups on the exception type and the frames of the stack, not on the message, and the frames depend on what was running: our three events, with the same message, got three different fingerprints, so one message can open several issues. Open the stack to find your code (the route, in our test) above the HTTP library, and look there for a client or pool created outside the loop that serves the request. Setup for FastAPI is in the FastAPI guide.

Errilo has no inbound filters on the server. This error points to a real bug, in the request path as in a job: we wouldn’t filter it.

Early access

The private beta is open, by invitation, to a handful of teams running a JavaScript, TypeScript, PHP or Python app in production. Join the waitlist and we’ll write to invite you.