Skip to content

Commit

Permalink
Avoid logging graceful disconnect as error
Browse files Browse the repository at this point in the history
The chromecasts restart each night, and seem to
generally do this by doing a socket shutdown of
write leading to a EOF received by clients.

No need to log this as an error
  • Loading branch information
elupus committed Nov 13, 2024
1 parent 97446b7 commit 71c757b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pychromecast/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class ChromecastConnectionError(PyChromecastError):
"""When a connection error occurs within PyChromecast."""


class ChromecastConnectionClosed(PyChromecastError):
"""When a connection was closed by remote device."""


class PyChromecastStopped(PyChromecastError):
"""Raised when a command is invoked while the Chromecast's socket_client
is stopped.
Expand Down
12 changes: 11 additions & 1 deletion pychromecast/socket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .dial import get_host_from_service
from .error import (
ChromecastConnectionError,
ChromecastConnectionClosed,
ControllerNotRegistered,
NotConnected,
PyChromecastStopped,
Expand Down Expand Up @@ -619,6 +620,15 @@ def _run_once(self) -> int:
if self.stop.is_set():
return 1
raise
except ChromecastConnectionClosed as exc:
self._force_recon = True
self.logger.debug(
"[%s(%s):%s] %s",
self.fn or "",
self.host,
self.port,
exc,
)
except socket.error as exc:
self._force_recon = True
self.logger.error(
Expand Down Expand Up @@ -817,7 +827,7 @@ def _read_bytes_from_socket(self, msglen: int) -> bytes:
try:
chunk = self.socket.recv(min(msglen - bytes_recd, 2048))
if chunk == b"":
raise socket.error("socket connection broken")
raise ChromecastConnectionClosed("Connection was closed by remote")
chunks.append(chunk)
bytes_recd += len(chunk)
except TimeoutError:
Expand Down

0 comments on commit 71c757b

Please sign in to comment.