{"id":977,"hash":"0da5eb1fdd135a784c5943cf3b2ce16fb3905923250b3802c0b6550d6680d207","pattern":"aiohttp client_exception ServerDisconnectedError - is this the API server&#39;s issue or aiohttp or my code?","full_message":"I'm getting an aiohttp client_exception.ServerDisconnectedError whenever I do more than ~200 requests to an API I'm hitting using asyncio & aiohttp. It doesn't seem to be my code because it works consistently with smaller number of requests, but fails on any larger number. Trying to understand if this error is related to aiohttp, or my code, or with the API endpoint itself? There doesn't seem to be much info online around this.\n\n  Traceback (most recent call last):\n  File \"C:/usr/PycharmProjects/api_framework/api_framework.py\", line 27, in <module>\n    stuff = abc.do_stuff_2()\n  File \"C:\\usr\\PycharmProjects\\api_framework\\api\\abc\\abc.py\", line 72, in do_stuff\n    self.queue_manager(self.do_stuff(json_data))\n  File \"C:\\usr\\PycharmProjects\\api_framework\\api\\abc\\abc.py\", line 115, in queue_manager\n    loop.run_until_complete(future)\n  File \"C:\\Python36x64\\lib\\asyncio\\base_events.py\", line 466, in run_until_complete\n    return future.result()\n  File \"C:\\usr\\PycharmProjects\\api_framework\\api\\abc\\abc.py\", line 96, in do_stuff\n    result = await asyncio.gather(*tasks)\n  File \"C:\\usr\\PycharmProjects\\api_framework\\api\\abc\\abc.py\", line 140, in async_post\n    async with session.post(self.api_attr.api_endpoint + resource, headers=self.headers, data=data) as response:\n  File \"C:\\Python36x64\\lib\\site-packages\\aiohttp\\client.py\", line 843, in __aenter__\n    self._resp = await self._coro\n  File \"C:\\Python36x64\\lib\\site-packages\\aiohttp\\client.py\", line 387, in _request\n    await resp.start(conn)\n  File \"C:\\Python36x64\\lib\\site-packages\\aiohttp\\client_reqrep.py\", line 748, in start\n    message, payload = await self._protocol.read()\n  File \"C:\\Python36x64\\lib\\site-packages\\aiohttp\\streams.py\", line 533, in read\n    await self._waiter\naiohttp.client_exceptions.ServerDisconnectedError: None\n\nhere's some of the code to generate the async requests:\n\n    def some_other_method(self):\n        self.queue_manager(self.do_stuff(all_the_tasks))\n\n    def queue_manager(self, method):\n        print('starting event queue')\n        loop = asyncio.get_event_loop()\n        future = asyncio.ensure_future(method)\n        loop.run_until_complete(future)\n        loop.close()\n\n    async def async_post(self, resource, session, data):\n        async with session.post(self.api_attr.api_endpoint + resource, headers=self.headers, data=data) as response:\n            resp = await response.read()\n        return resp\n\n    async def do_stuff(self, data):\n        print('queueing tasks')\n\n        tasks = []\n        async with aiohttp.ClientSession() as session:\n            for row in data:\n                task = asyncio.ensure_future(self.async_post('my_api_endpoint', session, row))\n                tasks.append(task)\n            result = await asyncio.gather(*tasks)\n            self.load_results(result)\n\nOnce the tasks have completed, self.load_results() method just parses the json and updates the DB.","ecosystem":"pypi","package_name":"python-3.x","package_version":null,"solution":"It is most likely caused by the configuration of the HTTP server. There are at least two possible reasons for the ServerDisconnectedError:\n\nThe server could limit the number of parallel TCP connections that can be made from a single IP address. By default, aiohttp already limits the number of parallel connections to 100. You can try reducing the limit and see if it solve the issue. To do so, you can create a custom TCPConnector with a different limit value and pass it to the ClientSession:\n\n        connector = aiohttp.TCPConnector(limit=50)\n        async with aiohttp.ClientSession(connector=connector) as session:\n            # Use your session as usual here\n\nThe server could limit the duration of a TCP connection. By default, aiohttp uses HTTP keep-alive so that the same TCP connection can be used for multiple requests. This improves performances since a new TCP connection does not have to be made for each request. However, some servers limit the duration of a TCP connection, and if you use the same TCP connection for many requests, the server can close it before you are done with it. You can disable HTTP keep-alive as a workaround. To do so, you can create a custom TCPConnector with the parameter force_close set to True, and pass it to the ClientSession:\n\n        connector = aiohttp.TCPConnector(force_close=True)\n        async with aiohttp.ClientSession(connector=connector) as session:\n            # Use your session as usual here\n\nI had the same issue and disabling HTTP keep-alive  was the solution for me. Hope this helps.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/51248714/aiohttp-client-exception-serverdisconnectederror-is-this-the-api-servers-issu","votes":23,"created_at":"2026-04-19T04:52:07.352147+00:00","updated_at":"2026-04-19T04:52:07.352147+00:00"}