Skip to content

Commit

Permalink
Fix cancelation error on keyboard interruption
Browse files Browse the repository at this point in the history
  • Loading branch information
vadim-su authored Dec 29, 2023
1 parent e0bf6a4 commit a5d676a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions asyncord/gateway/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def start(self) -> None:
async with self._session.ws_connect(self.url) as ws:
self.ws = ws
if logger.isEnabledFor(logging.DEBUG):
logger.debug('Connected to gateway at %s', self.url)
logger.info('Connected to gateway at %s', self.url)
else:
logger.info('Connected to gateway')

Expand All @@ -96,19 +96,27 @@ async def start(self) -> None:
except errors.NecessaryReconnectError:
logger.info('Reconnect is necessary')

except asyncio.CancelledError:
logger.info('Gateway stopping')
await self.stop()
logger.info('Gateway stopped')
break

except Exception as err:
logger.exception('Unhandled exception in gateway loop: %s', err)
raise

else:
logger.exception('Could not connect to the gateway at %s', self.url)
raise RuntimeError('Could not connect to the gateway at %s', self.url)

async def stop(self) -> None:
"""Stop the gateway client."""
await self._heartbeat.stop()
self.is_started = False
if self.ws:
await self.ws.close()
self.ws = None
self.is_started = False

def add_handler[EVENT_T: GatewayEvent](self, event_handler: EventHandlerType[EVENT_T]) -> None:
"""Add an event handler.
Expand Down

0 comments on commit a5d676a

Please sign in to comment.