Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aiohttp - ClientConnectionError: Connector is closed

Tags:

python

aiohttp

My code:

import aiohttp
import asyncio

connector = aiohttp.TCPConnector(limit=1)

async def get(url):
    print('started thread', url)
    session = aiohttp.ClientSession(connector=connector)
    response = await session.get(url, timeout=5)
    await session.close()
    print(await response.text())


loop = asyncio.get_event_loop()
tasks = [
    loop.create_task(get('http://httpbin.org/ip')),
    loop.create_task(get('http://httpbin.org/ip')),
    loop.create_task(get('http://httpbin.org/ip')),
    loop.create_task(get('http://httpbin.org/ip')),
    loop.create_task(get('http://httpbin.org/ip')),
    loop.create_task(get('http://httpbin.org/ip')),
]
loop.run_until_complete(asyncio.wait(tasks))
connector.close()

After 1 request, i get error: aiohttp.client_exceptions.ClientConnectionError: Connector is closed.

like image 966
yesterdaycode Avatar asked Oct 23 '25 10:10

yesterdaycode


1 Answers

This error happens when you try to make a request on a connector that you've already closed. You're calling session.close() in your get function, which in turn is closing its connector. This would normally be fine, but since you're initializing one connector and explicitly passing it to each session, it is being used by all of them.

As soon as one of the requests finishes, session.close() closes the shared connector, causing all of the other active requests to immediately fail with the error you're seeing.

There's a bunch of ways to alleviate this, depending on what you're trying to do:

  1. Don't pass an explicit connector to each session (it'll create its own)
  2. Create a single session where you're creating a connector, and wait to call close on it until after run_until_complete (where you're currently trying to close the connector)
  3. If you really need a single connector and multiple sessions for each request, you'll have to add them to a list in get and then close them all after.

I'm not sure what your use case is, but if #1 and #2 really don't work, you may want to check out semaphores.

like image 72
Luke Sapan Avatar answered Oct 24 '25 22:10

Luke Sapan