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

Deal gently with ConnectionClosedError fron websockets #762

Closed
wants to merge 6 commits into from

Conversation

euri10
Copy link
Member

@euri10 euri10 commented Aug 20, 2020

based on test nicely provided in #758
using pytest-reraise to avoid the bug shown in #759
fix #757

Would really appreciate some feedback on this, not sure I got this right

tests/protocols/test_websocket.py Outdated Show resolved Hide resolved

try:
await self.send(data)
except ConnectionClosedError as exc:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching the exception is certainly one way to deal with this. Just want to mention that the wsproto implementation checks for self.transport.is_closing():

https://github.com/encode/uvicorn/blob/master/uvicorn/protocols/websockets/wsproto_impl.py#L278

Maybe this is more agnostic to the underlying library. On the other hand it's circumventing the library and checks transport conditions directly. Unsure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I dont like it too, I didn't see something satisfying enough from the faq here too.
https://websockets.readthedocs.io/en/stable/faq.html#what-does-connectionclosederror-code-1006-mean


# Simulate a lost connection
# without receiving a close frame
self.send.__self__.connection_lost(None)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still wondering if there's a better way to access the underlying protocol here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you're right.
add to that the fact that once we call the protocol here, right after in the asgi_send mehtod at elif not self.closed_event.is_set(): we have that event not set and the State that is State.CLOSED which is dubious

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fail_connection ?

Copy link

@stefanw stefanw Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used connection_lost because it was used in test_early_disconnect in the http tests. It's the method called on protocol from transport.close(), so seems appropriate. I was just wondering if there's a nicer way to access it instead of going through the method's __self__.

@euri10 euri10 closed this Oct 6, 2020
@euri10 euri10 deleted the stefanw-server-lost-connection branch October 6, 2020 07:41
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

Successfully merging this pull request may close these issues.

ConnectionClosedError raised from websockets library
2 participants