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.

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:
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:
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:
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):
import asyncio, logging
logging.basicConfig(level=logging.DEBUG)
asyncio.get_running_loop().set_debug(True)
asyncio.get_running_loop().slow_callback_duration = 0.05Executing <Task ...> took 1.234 seconds names the coroutine and the line that blocked. That log is the whole diagnosis.
Key Comparison Table
| Approach | Blocks the loop | Good for | Cost |
|---|---|---|---|
await client.get() (httpx/aiohttp) | No | HTTP, the default choice | One connection pool per loop |
asyncio.to_thread(fn, *a) | No | Blocking libs, file IO, requests | Default executor, 32 threads shared |
loop.run_in_executor(pool, fn) | No | Sized pools, priority separation | Pool lifecycle you must manage |
time.sleep() / requests.get() | Yes | Nothing in async code | Whole loop stalls |
ProcessPoolExecutor | No | CPU-bound work | Pickling, __main__ guard |
asyncio.sleep(0) | No | Yielding between CPU chunks | Still single-threaded |
| Error text | Real meaning | First check |
|---|---|---|
RuntimeError: Event loop is closed | Loop that owned the object was closed | Where was the client created? |
RuntimeError: This event loop is already running | asyncio.run inside running loop | Are you in Jupyter or nested coroutine? |
Task was destroyed but it is pending! | Task still alive at loop close | Await or cancel in shutdown |
Unclosed client session | Client never awaited aclose() | Lifespan/finally block |
attached to a different loop | Object created under another loop | Module-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.
# 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:
@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.
# 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 freeto_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.
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:
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 lockMove 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:
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 emitsDeprecationWarningwhen no loop is running and, in 3.14, raises. Useasyncio.get_running_loop()inside coroutines andasyncio.run()at the entry point.- Nested
asyncio.run(). Calling it inside a running loop raises "already running". If a library forces this, restructure withawaitor isolate it in a thread:await asyncio.to_thread(asyncio.run, coro()). nest_asyncioin 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.
subprocessand 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_forcancels 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 toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.