Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python asyncio: Event Loop Is Closed and Blocking Calls

asyncio.run closes its loop, so a cached client or pool dies with the second call. Where the loop lifetime belongs, and how to keep blocking IO from stalling every task.

~5 min readPython
#Python#asyncio#Concurrency#Debugging
3D illustration of a circular event loop ring with a jammed amber brick and a closed padlock node

Introduction & Quick Summary

Symptom. A long-running service, worker or test suite prints RuntimeError: Event loop is closed at shutdown, on the second invocation of a script, or inside a library you only call once; Task exception was never retrieved follows. In other runs nothing crashes but throughput collapses: one endpoint that calls requests.get() or does a large pandas operation freezes every concurrent request until it returns, and /healthz times out at exactly the same moment.

Cause. asyncio.run() creates a loop, runs your coroutine, then closes the loop. Anything bound to that loop — an aiohttp.ClientSession, an httpx.AsyncClient, a database pool, a Future, a Queue — is dead afterwards, so reusing a module-level client raises "Event loop is closed" the second time. Blocking calls have a different cause: the event loop is a single thread that runs coroutines one at a time, and time.sleep, requests, psycopg2, subprocess.run or a CPU-heavy loop holds that thread, so no other task can progress.

Fix. Give the loop the lifetime of the process. Create clients and pools inside the application's lifespan (or inside each asyncio.run() call), close them before the loop stops, and never cache a loop object. For blocking work, offload with asyncio.to_thread() for IO-bound C extensions and loop.run_in_executor() for a sized thread pool, or move to a native async driver; keep CPU-bound work in a process pool. Then confirm the loop is actually responsive with asyncio debug logging before tuning anything else.

Core Mechanics, Signatures & Reproducible Setup

asyncio.run(coro) does, in order: create a new event loop, install it as current, run coro to completion, cancel remaining tasks, run shutdown_asyncgens() and shutdown_default_executor(), then close the loop. Each of those steps explains a failure you will see:

python
import asyncio, httpx client = httpx.AsyncClient() # created with no running loop: fine, but bound lazily async def fetch(): return (await client.get("https://example.com")).status_code asyncio.run(fetch()) # loop A asyncio.run(fetch()) # RuntimeError: Event loop is closed (loop A is gone)

The reproducible, correct shape:

python
import asyncio, httpx async def main(): async with httpx.AsyncClient(timeout=10) as client: # lives inside the loop r = await client.get("https://example.com") return r.status_code if __name__ == "__main__": print(asyncio.run(main()))

For a persistent server, FastAPI/Starlette own the loop; resources belong in the lifespan:

python
from contextlib import asynccontextmanager import httpx from fastapi import FastAPI @asynccontextmanager async def lifespan(app: FastAPI): app.state.http = httpx.AsyncClient(timeout=httpx.Timeout(10, connect=3)) try: yield finally: await app.state.http.aclose() # close before the loop stops app = FastAPI(lifespan=lifespan)

Detecting blocking calls needs evidence, not intuition. Turn on asyncio's debug mode, which logs every callback that occupies the loop longer than slow_callback_duration (100 ms by default):

python
import asyncio, logging logging.basicConfig(level=logging.DEBUG) asyncio.get_running_loop().set_debug(True) asyncio.get_running_loop().slow_callback_duration = 0.05

Executing <Task ...> took 1.234 seconds names the coroutine and the line that blocked. That log is the whole diagnosis.

Key Comparison Table

ApproachBlocks the loopGood forCost
await client.get() (httpx/aiohttp)NoHTTP, the default choiceOne connection pool per loop
asyncio.to_thread(fn, *a)NoBlocking libs, file IO, requestsDefault executor, 32 threads shared
loop.run_in_executor(pool, fn)NoSized pools, priority separationPool lifecycle you must manage
time.sleep() / requests.get()YesNothing in async codeWhole loop stalls
ProcessPoolExecutorNoCPU-bound workPickling, __main__ guard
asyncio.sleep(0)NoYielding between CPU chunksStill single-threaded
Error textReal meaningFirst check
RuntimeError: Event loop is closedLoop that owned the object was closedWhere was the client created?
RuntimeError: This event loop is already runningasyncio.run inside running loopAre you in Jupyter or nested coroutine?
Task was destroyed but it is pending!Task still alive at loop closeAwait or cancel in shutdown
Unclosed client sessionClient never awaited aclose()Lifespan/finally block
attached to a different loopObject created under another loopModule-level state

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. A module-level client shared between asyncio.run() calls

Scripts, cron jobs, Celery workers and AWS Lambda handlers often call asyncio.run() per invocation. A global aiohttp.ClientSession survives the call but its loop does not.

python
# Broken: the session outlives the loop session: aiohttp.ClientSession | None = None async def get(url): global session if session is None: session = aiohttp.ClientSession() async with session.get(url) as r: return await r.text() # second invocation: Event loop is closed # Fixed: one session per loop, cached per loop _sessions: dict[asyncio.AbstractEventLoop, aiohttp.ClientSession] = {} def session() -> aiohttp.ClientSession: loop = asyncio.get_running_loop() if loop not in _sessions or _sessions[loop].closed: _sessions[loop] = aiohttp.ClientSession() return _sessions[loop]

Cache the loop as the key, not a single global, and drop entries when the loop closes.

2. Uvicorn reload leaves stale connections

--reload restarts the worker process, so finally blocks that only close on request completion never run. Wrap resource creation in the lifespan, as above, and add a shutdown hook for anything created lazily:

python
@app.on_event("shutdown") # legacy, still runs on 0.x FastAPI async def close_pool(): await app.state.http.aclose()

Prefer lifespan; the on_event decorators are deprecated in recent FastAPI and removed from the docs.

3. requests inside an async endpoint

requests.get() performs a blocking socket read. Under ten concurrent requests, p99 becomes the sum of all ten, which looks like a database problem until you log loop latency.

python
# Quick fix without rewriting the integration import asyncio, requests def _get(url: str) -> str: return requests.get(url, timeout=5).text async def get_page(url: str) -> str: return await asyncio.to_thread(_get, url) # thread pool, loop stays free

to_thread uses the loop's default executor, which has min(32, cpu_count + 4) workers. A hundred concurrent calls queue behind 32 threads; size a dedicated ThreadPoolExecutor when that matters.

4. Tests that pass alone and fail in the suite

pytest-asyncio gives each test its own loop by default. A session-scoped fixture that opens an AsyncClient, a SQLAlchemy AsyncEngine, or a redis.asyncio pool therefore hands a loop-bound object to a different loop.

python
import pytest, httpx @pytest.fixture async def client(): # function scope matches the test's loop async with httpx.AsyncClient(base_url="http://test") as c: yield c # or, pytest-asyncio 0.23+: keep one loop for the whole session pytestmark = pytest.mark.asyncio(loop_scope="session")

If a fixture must be session-scoped, dispose of the engine in the same fixture and mark every test with the same loop_scope.

5. Blocking inside async with hides the stall

Holding a lock or a database transaction across an await serialises everything for the duration, and the traceback only shows the await:

python
async def handle(order): async with lock: # fine await db.execute(...) # another task may wait here for seconds await send_receipt(order) # do not send email while holding the lock

Move external calls outside the critical section, or use a bounded semaphore instead of one lock for the whole handler.

6. Graceful shutdown without "pending task" noise

On SIGTERM, cancel with a timeout and await the cancellations; then let asyncio.run close the loop. Task groups make the fan-out explicit:

python
async def main(): async with asyncio.TaskGroup() as tg: tg.create_task(worker(1)) tg.create_task(worker(2)) # here every task has finished or the group raised ExceptionGroup try: asyncio.run(asyncio.wait_for(main(), timeout=25)) except (asyncio.TimeoutError, TimeoutError): log.warning("shutdown timed out, tasks cancelled")

Common Compilation/Runtime Mistakes & Version Gotchas

  • asyncio.get_event_loop() in Python 3.12+. It emits DeprecationWarning when no loop is running and, in 3.14, raises. Use asyncio.get_running_loop() inside coroutines and asyncio.run() at the entry point.
  • Nested asyncio.run(). Calling it inside a running loop raises "already running". If a library forces this, restructure with await or isolate it in a thread: await asyncio.to_thread(asyncio.run, coro()).
  • nest_asyncio in production. It patches the loop to allow re-entry, which defeats cancellation and can deadlock a server. It is a notebook workaround.
  • Windows proactor loop. subprocess and some socket APIs behave differently; asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) is the usual workaround when a third-party client refuses the default policy.
  • Timeouts that never fire. httpx.Timeout(10) applies per operation unless you pass it whole; asyncio.wait_for cancels the task but a blocking call inside it cannot be interrupted. Only loop-friendly code responds to cancellation.

Related tools. Reproduce slow endpoints outside Python with the cURL builder and --max-time to separate network latency from loop starvation, convert deadline values and trace timestamps with the timestamp converter, and inspect the JSON payloads your async client receives with the JSON formatter when a timeout hides a slow upstream response.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.