Fix websocket exception handling on python2.7 #149
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
This library doesn't handle
socket.error
gracefully in Python 2.7. This becomes much more obvious when you use thepython-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 asocket.error
in Python 2.7 when the connection is refused (i.e. when the port is closed).socket.error
is anIOError
.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.xI'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.