Reverse future done checks so we know they are actually reachable (#708)

This commit is contained in:
J. Nick Koston 2023-11-25 08:49:54 -06:00 committed by GitHub
parent 3711d54ffa
commit d592f1e7ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -220,14 +220,13 @@ class APIConnection:
if self._debug_enabled: if self._debug_enabled:
_LOGGER.debug("Cleaning up connection to %s", self.log_name) _LOGGER.debug("Cleaning up connection to %s", self.log_name)
for fut in self._read_exception_futures: for fut in self._read_exception_futures:
if fut.done(): if not fut.done():
continue err = self._fatal_exception or APIConnectionError("Connection closed")
err = self._fatal_exception or APIConnectionError("Connection closed") new_exc = err
new_exc = err if not isinstance(err, APIConnectionError):
if not isinstance(err, APIConnectionError): new_exc = ReadFailedAPIError("Read failed")
new_exc = ReadFailedAPIError("Read failed") new_exc.__cause__ = err
new_exc.__cause__ = err fut.set_exception(new_exc)
fut.set_exception(new_exc)
self._read_exception_futures.clear() self._read_exception_futures.clear()
# If we are being called from do_connect we # If we are being called from do_connect we
# need to make sure we don't cancel the task # need to make sure we don't cancel the task
@ -676,9 +675,8 @@ class APIConnection:
def _handle_timeout(self, fut: asyncio.Future[None]) -> None: def _handle_timeout(self, fut: asyncio.Future[None]) -> None:
"""Handle a timeout.""" """Handle a timeout."""
if fut.done(): if not fut.done():
return fut.set_exception(asyncio_TimeoutError)
fut.set_exception(asyncio_TimeoutError)
def _handle_complex_message( def _handle_complex_message(
self, self,
@ -689,12 +687,11 @@ class APIConnection:
resp: message.Message, resp: message.Message,
) -> None: ) -> None:
"""Handle a message that is part of a response.""" """Handle a message that is part of a response."""
if fut.done(): if not fut.done():
return if do_append is None or do_append(resp):
if do_append is None or do_append(resp): responses.append(resp)
responses.append(resp) if do_stop is None or do_stop(resp):
if do_stop is None or do_stop(resp): fut.set_result(None)
fut.set_result(None)
async def send_messages_await_response_complex( # pylint: disable=too-many-locals async def send_messages_await_response_complex( # pylint: disable=too-many-locals
self, self,