Should not throw exception for EofStream Exception in WebSocket.
Long story short
Exception will be thrown when try to read from Websocket stream, if the socket stream has been closed.
Expected behaviour
No exception should be thrown, instead a message with type aiohttp.WSMsgType.closed should be return
Actual behaviour
Exception is thrown.
Steps to reproduce
Run the program below, while ws is the WebSocket instance. While the socket is closed by the remote server, the async for loop will throw exception.
import aiohttp
async for msg in ws:
# continue to process the msg
Exception log is below:
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x107c2f550>
Traceback (most recent call last):
File "/anaconda3/lib/python3.6/site-packages/aiohttp/client_ws.py", line 204, in receive
msg = await self._reader.read()
File "/anaconda3/lib/python3.6/site-packages/aiohttp/streams.py", line 575, in read
return await super().read()
File "/anaconda3/lib/python3.6/site-packages/aiohttp/streams.py", line 549, in read
raise EofStream
aiohttp.streams.EofStream
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.6/site-packages/aiohttp/client_ws.py", line 168, in close
msg = await self._reader.read()
File "/anaconda3/lib/python3.6/site-packages/aiohttp/streams.py", line 575, in read
return await super().read()
File "/anaconda3/lib/python3.6/site-packages/aiohttp/streams.py", line 549, in read
raise EofStream
aiohttp.streams.EofStream
Your environment
MacOS Python 3.6.5 :: Anaconda custom (64-bit) aiohttp==3.3.2
Looks working as expected.
@webknjaz
Does working as expected mean the lib intentionally throw the exception instead of return a message with message type aiohttp.WSMsgType.closed ?
Can we at least catch the exception, hence the library does not break the whole app?
I mean, communicating failures as exceptions is a standard practice in Python world. See EAFP.
Do you mean that the context manager should process it? Well, probably yes. It would make sense to put it into __aexit__().
cc @asvetlov
Hello, seems related to this ticket:
Unhandled exception
Traceback (most recent call last):
File "/tmp/tmpg2n5yr0j/pypi__aiohttp_3_4_4/aiohttp/web_protocol.py", line 410, in start
await resp.prepare(request)
AttributeError: 'NoneType' object has no attribute 'prepare'
I get this exception on every reconnect attempt on a closed (closing?) connection
It's not. Please file another issue.
This was fixed in 3.3.0 via https://github.com/aio-libs/aiohttp/pull/2814