diff --git a/irclog.py b/irclog.py index 0b29280..dcded50 100644 --- a/irclog.py +++ b/irclog.py @@ -674,12 +674,17 @@ class IRCClient: connectionClosedEvent.clear() try: self.logger.debug('Creating IRC connection') - self._transport, self._protocol = await loop.create_connection( + t = asyncio.create_task(loop.create_connection( protocol_factory = lambda: IRCClientProtocol(self.messageQueue, connectionClosedEvent, loop, self.config, self.channels), host = self.config['irc']['host'], port = self.config['irc']['port'], ssl = self._get_ssl_context(), - ) + )) + done, _ = await asyncio.wait({t, asyncio.create_task(sigintEvent.wait())}, return_when = asyncio.FIRST_COMPLETED, timeout = 30) + if t not in done: + t.cancel() + await t # Raises the CancelledError + self._transport, self._protocol = t.result() self.logger.debug('Starting send queue processing') asyncio.create_task(self._protocol.send_queue()) # Quits automatically on connectionClosedEvent self.logger.debug('Waiting for connection closure or SIGINT') @@ -690,8 +695,8 @@ class IRCClient: if not connectionClosedEvent.is_set(): self.logger.debug('Quitting connection') await self._protocol.quit() - except (ConnectionRefusedError, ssl.SSLError, asyncio.TimeoutError) as e: - self.logger.error(str(e)) + except (ConnectionRefusedError, ssl.SSLError, asyncio.TimeoutError, asyncio.CancelledError) as e: + self.logger.error(f'{type(e).__module__}.{type(e).__name__}: {e!s}') await asyncio.wait({asyncio.create_task(sigintEvent.wait())}, timeout = 5) if sigintEvent.is_set(): self.logger.debug('Got SIGINT, putting EOF and breaking')