From efd51ba17ba9bd661e276cd6f255d1788f0b2811 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 12 Oct 2020 10:29:06 +0200 Subject: [PATCH] Check if the connection is open inside the lock --- aiokef/aiokef.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/aiokef/aiokef.py b/aiokef/aiokef.py index 3c54166..b7bca94 100644 --- a/aiokef/aiokef.py +++ b/aiokef/aiokef.py @@ -250,21 +250,21 @@ def is_connected(self) -> bool: return (self._reader, self._writer) != (None, None) async def open_connection(self) -> None: - if self.is_connected: - if self._writer.is_closing(): # type: ignore - _LOGGER.debug( - "%s: Connection closing but did not disconnect", self.host - ) - await self._disconnect() - else: - _LOGGER.debug("%s: Connection is still alive", self.host) - return retries = 0 while retries < _MAX_CONNECTION_RETRIES: - _LOGGER.debug("%s: Opening connection", self.host) - try: async with self._lock, timeout(_TIMEOUT): + if self.is_connected: + if self._writer.is_closing(): # type: ignore + _LOGGER.debug( + "%s: Connection closing but did not disconnect", + self.host, + ) + await self._disconnect(use_lock=False) + else: + _LOGGER.debug("%s: Connection is still alive", self.host) + return + _LOGGER.debug("%s: Opening connection", self.host) self._reader, self._writer = await asyncio.open_connection( self.host, self.port, family=socket.AF_INET ) @@ -274,6 +274,7 @@ async def open_connection(self) -> None: await asyncio.sleep(0.5) except BlockingIOError: # Connection incoming # XXX: I have never seen this. + _LOGGER.debug("%s: BlockingIOError", self.host) retries = 0 await asyncio.sleep(1) except (asyncio.TimeoutError, OSError) as e: # Host is down