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

Fix websocket exception handling on python2.7 #149

Merged
merged 1 commit into from Nov 29, 2019

Conversation

jabbawookiees
Copy link

@jabbawookiees jabbawookiees commented Nov 29, 2019

Problem

This library doesn't handle socket.error gracefully in Python 2.7. This becomes much more obvious when you use the python-socketio client.

If there's a socketio server running initially, the Python 2.7 client connects and does whatever it does. But if the socketio server dies for some reason, the thread that attempts to reconnect fails due to the unhandled exception because the server rejects connections, and reconnection doesn't work even when the socketio server goes back up.

Reason

The underlying websocket library used here throws a socket.error in Python 2.7 when the connection is refused (i.e. when the port is closed). socket.error is an IOError.

In Python 3.3 onwards, it throws a ConnectionError which it catches correctly.

The Fix

I caught IOError here to handle the cases similarly in Python 2.7 and Python 3.x

I'd be happy to add the following alternative to catch less errors, but the PR as is catches a wider net of error conditions that I may not have considered.

import socket

...

except (ConnectionError, socket.error):

@miguelgrinberg miguelgrinberg merged commit b714f81 into miguelgrinberg:master Nov 29, 2019
@miguelgrinberg
Copy link
Owner

Looks good. Thanks!

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.

None yet

2 participants