Skip to content

Commit

Permalink
Rework IP connections to provide more error details (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 1, 2024
1 parent 13b8283 commit f224904
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions aiohomekit/controller/ip/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def _connection_lost(self, exception: Exception) -> None:
"""
Called by a Protocol instance when eof_received happens.
"""
logger.debug("Connection %r lost.", self)
logger.debug("Connection lost to %r: %s", self, exception)
# Clear the transport and protocol right away
# as otherwise _start_connector will see them and
# think we are still connected.
Expand All @@ -587,6 +587,7 @@ async def _connect_once(self) -> None:

last_exception: Exception | None = None
sock: socket.socket | None = None
connected_host: str | None = None
interleave = 1
while addr_infos:
try:
Expand All @@ -597,12 +598,13 @@ async def _connect_once(self) -> None:
interleave=interleave,
loop=self._loop,
)
connected_host = sock.getpeername()[0]
break
except (OSError, asyncio.TimeoutError) as err:
last_exception = err
aiohappyeyeballs.pop_addr_infos_interleave(addr_infos, interleave)

if sock is None:
if sock is None or connected_host is None:
if isinstance(last_exception, asyncio.TimeoutError):
raise TimeoutError("Timeout") from last_exception
raise ConnectionError(str(last_exception)) from last_exception
Expand All @@ -613,7 +615,6 @@ async def _connect_once(self) -> None:
self.transport, self.protocol = await loop.create_connection(
lambda: InsecureHomeKitProtocol(self), sock=sock
)
connected_host = sock.getpeername()[0]
self.connected_host = connected_host
# The port is not included in the Host header for compatibility
# reasons. It may be safe to include it in the future if its
Expand Down

0 comments on commit f224904

Please sign in to comment.