Event loop is closed 🐍 Python

RuntimeError: Event loop is closed

Async code tried to use an event loop that has already been shut down.

Seen on: Python

Meaning

Common on Windows with asyncio.run() and aiohttp/httpx clients cleaned up after the loop closes, and when mixing asyncio.run() calls with long-lived clients.

Common causes

  • Client sessions not closed before the loop ends
  • Calling asyncio.run() repeatedly with shared resources
  • Windows ProactorEventLoop cleanup noise

⚡ Quick fix

  1. Use async with for clients/sessions
  2. Run everything inside a single asyncio.run(main())
  3. On Windows, close clients explicitly before returning

Detailed fix by platform

Python

  1. python
    async with httpx.AsyncClient() as client:
        r = await client.get(url)

How to diagnose

  1. Resource — Which client is cleaned up late?
  2. Pattern — Multiple asyncio.run() calls?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.