You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Making a request which has a very large payload (e.g. 50MB) usually receives a 413 error from the server it is calling. This means raise_for_status() returns a ClientResponseError as expected, which can be handled by downstream code.
This works as expected with a simple await session.post(...), but inside an async with session.post(...) block, attempts to raise the status error cause a ClientOSError with a broken pipe.
To Reproduce
importjsonimportsysfromaiohttpimportClientSessionfromaiohttp.client_exceptionsimportClientResponseErrorasyncwithClientSession(base_url='https://www.google.com') assess:
large_data= [
*[[0.000000,0.000000+i*0.00001] foriinrange(0, 10000000)],
]
payload= {"data": large_data}
assertsys.getsizeof(json.dumps(payload)) >1024*500sess=ClientSession(base_url='https://www.google.com')
# This workstry:
res=awaitsess.request('post', '/zsdfj', json=payload)
res.raise_for_status()
exceptClientResponseError:
print("Request failed")
# This fails with a BrokenPipetry:
asyncwithsess.request('post', '/zsdfj', json=payload) asres:
res.raise_for_status()
exceptClientResponseError:
print("Request in context manager failed")
Expected behavior
Both requests should result in a ClientReponseError.
Logs/tracebacks
Traceback (most recent call last):
File "/opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 644, in write_bytesawait writer.write_eof()
File "/opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/http_writer.py", line 158, in write_eofawaitself.drain()
File "/opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/http_writer.py", line 171, in drainawaitself._protocol._drain_helper()
File "/opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/base_protocol.py", line 90, in _drain_helperawait asyncio.shield(waiter)
aiohttp.client_exceptions.ClientOSError: [Errno 32] Broken pipe
---------------------------------------------------------------------------
ClientOSError Traceback (most recent call last)
Cell In[22], line 24
22try:
23asyncwith sess.request('post', '/zsdfj', json=payload) as res:
---> 24 res.raise_for_status()
25except ClientResponseError:
26print("Request in context manager failed")
File /opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/client.py:1213, in _RequestContextManager.__aexit__(self, exc_type, exc, tb)
1201 async def __aexit__(
1202 self,
1203 exc_type: Optional[Type[BaseException]],
(...)
1210 # explicitly. Otherwise connection error handling should kick in
1211 # and close/recycle the connection as required.
1212 self._resp.release()
-> 1213 await self._resp.wait_for_close()
File /opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/client_reqrep.py:1094, in ClientResponse.wait_for_close(self)
1092 async def wait_for_close(self) -> None:
1093 if self._writer is not None:
-> 1094 await self._writer
1095 self.release()
File /opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/client_reqrep.py:644, in ClientRequest.write_bytes(self, writer, conn)
642 protocol.set_exception(new_exc)
643except asyncio.CancelledError:
--> 644 await writer.write_eof()
645exceptExceptionas exc:
646 protocol.set_exception(exc)
File /opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/http_writer.py:158, in StreamWriter.write_eof(self, chunk)
155if chunk:
156self._write(chunk)
--> 158 await self.drain()
160self._eof =True
File /opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/http_writer.py:171, in StreamWriter.drain(self)
163"""Flush the write buffer.164165 The intended use is to write
(...)
168await w.drain()
169"""170ifself._protocol.transport isnotNone:
--> 171 await self._protocol._drain_helper()
File /opt/conda/envs/py38/lib/python3.8/site-packages/aiohttp/base_protocol.py:90, in BaseProtocol._drain_helper(self)
88 waiter =self._loop.create_future()
89self._drain_waiter = waiter
---> 90 await asyncio.shield(waiter)
ClientOSError: [Errno 32] Broken pipe
I think the server has disconnected prematurely, causing that exception to happen while trying to close the connection cleanly. Might have to try and suppress the exception there in __aexit__()...
Describe the bug
Making a request which has a very large payload (e.g. 50MB) usually receives a 413 error from the server it is calling. This means
raise_for_status()
returns aClientResponseError
as expected, which can be handled by downstream code.This works as expected with a simple
await session.post(...)
, but inside anasync with session.post(...)
block, attempts to raise the status error cause aClientOSError
with a broken pipe.To Reproduce
Expected behavior
Both requests should result in a
ClientReponseError
.Logs/tracebacks
Python Version
aiohttp Version
multidict Version
yarl Version
OS
$ uname -a Linux 5.10.0-28-cloud-amd64 #1 SMP Debian 5.10.209-2 (2024-01-31) x86_64 GNU/Linux $ lsb_release -a Distributor ID: Debian Description: Debian GNU/Linux 11 (bullseye) Release: 11 Codename: bullseye
Related component
Client
Additional context
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: