Skip to content

Commit

Permalink
Added a comment about the use of futures.CancelledError
Browse files Browse the repository at this point in the history
  • Loading branch information
pelson committed Feb 7, 2018
1 parent fda1112 commit fdc1108
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions homeassistant/components/websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
https://home-assistant.io/developers/websocket_api/
"""
import asyncio
from concurrent import futures
from contextlib import suppress
from concurrent.futures import CancelledError
from functools import partial
import json
import logging
Expand Down Expand Up @@ -121,6 +121,11 @@
TYPE_PING)
}, extra=vol.ALLOW_EXTRA)

# Define the possible errors that occur when connections are cancelled.
# Originally, this was just asyncio.CancelledError, but issue #9546 showed
# that futures.CancelledErrors can also occur in some situations.
CANCELLATION_ERRORS = (asyncio.CancelledError, futures.CancelledError)


def auth_ok_message():
"""Return an auth_ok message."""
Expand Down Expand Up @@ -232,7 +237,7 @@ def log_error(self, message1, message2=''):
def _writer(self):
"""Write outgoing messages."""
# Exceptions if Socket disconnected or cancelled by connection handler
with suppress(RuntimeError, asyncio.CancelledError, CancelledError):
with suppress(RuntimeError, *CANCELLATION_ERRORS):
while not self.wsock.closed:
message = yield from self.to_write.get()
if message is None:
Expand Down Expand Up @@ -364,7 +369,7 @@ def handle_hass_stop(event):
self.log_error(msg)
self._writer_task.cancel()

except (asyncio.CancelledError, CancelledError):
except CANCELLATION_ERRORS:
self.debug("Connection cancelled by server")

except asyncio.QueueFull:
Expand Down

0 comments on commit fdc1108

Please sign in to comment.