Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should not throw exception for EofStream Exception in WebSocket. #3223

Open
huangjosh opened this issue Aug 28, 2018 · 5 comments
Open

Should not throw exception for EofStream Exception in WebSocket. #3223

huangjosh opened this issue Aug 28, 2018 · 5 comments
Assignees

Comments

@huangjosh
Copy link

huangjosh commented Aug 28, 2018

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

@webknjaz
Copy link
Member

Looks working as expected.

@huangjosh
Copy link
Author

@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?

@webknjaz
Copy link
Member

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

@benitogf
Copy link
Contributor

benitogf commented Oct 2, 2018

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

@asvetlov
Copy link
Member

asvetlov commented Oct 2, 2018

It's not.
Please file another issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants