Skip to content

[BUG] RuntimeError after bot termination with aiohttp>=3.12.4 #1778

@mifuyutsuki

Description

@mifuyutsuki

Library Version

unstable @ 0d11f77

Describe the Bug

In interactions.py with aiohttp >= 3.12.4 (and specifically that version onwards), upon terminating the running bot with e.g. keyboard interrupt (Ctrl+C), the program will close, however a task error is emitted (see Traceback section).

The traceback indicates that interactions.py tried to reconnect through a websocket session that had been closed by program termination. I traced down the issue and discovered that aiohttp starting version 3.12.4 introduced a patch that fixes closing the client session (aio-libs/aiohttp#11077, which is based on aio-libs/aiohttp#3733). On the interactions.py side, I read resp.type inside interactions.api.gateway.websocket:WebsocketClient.receive() and found that during program termination:

# aiohttp < 3.12.4
DEBUG:interactions:Dispatching Event: disconnect
WSMessage: <WSMsgType.CLOSING: 256>
DEBUG:interactions:Stopping the bot.
INFO:interactions:Shard ID 0 | Stopping Shard

# aiohttp >= 3.12.4
DEBUG:interactions:Dispatching Event: disconnect
WSMessage: <WSMsgType.CLOSING: 256>
DEBUG:interactions:Stopping the bot.
WSMessage: <WSMsgType.CLOSED: 257>
INFO:interactions:Shard ID 0 | Stopping Shard
ERROR:asyncio:Task exception was never retrieved

The websocket client now emits WSMsgType.CLOSED on session close when it did not before, and interactions.py tried to reconnect due to how the library handles the message type.

Steps to Reproduce

  1. Run any interactions.py bot
  2. Wait for the bot to fully initialize (green activity)
  3. Terminate the bot

Expected Results

No errors are emitted. If run from the terminal, no message is emitted while the bot is running (assuming default log level).

Minimal Reproducible Code

from interactions import Client
bot = Client()
bot.start("token") # Ctrl+C when running

Traceback

Task exception was never retrieved
future: <Task finished name='Task-29' coro=<WebsocketClient.receive() done, defined at ...\interactions\api\gateway\websocket.py:167> exception=RuntimeError('Session is closed')>
Traceback (most recent call last):
  File "...\interactions\api\gateway\websocket.py", line 215, in receive
    await self.reconnect(resume=True)
  File "...\interactions\api\gateway\gateway.py", line 281, in reconnect
    await super().reconnect(resume=resume, code=code, url=url)
  File "...\interactions\api\gateway\websocket.py", line 260, in reconnect
    self.ws = await self.state.client.http.websocket_connect(url or self.ws_url)
  File "...\interactions\api\http\http_client.py", line 593, in websocket_connect
    return await self.__session.ws_connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\.venv\Lib\site-packages\aiohttp\client.py", line 1048, in _ws_connect
    resp = await self.request(
           ^^^^^^^^^^^^^^^^^^^
  File "...\.venv\Lib\site-packages\aiohttp\client.py", line 517, in _request
    raise RuntimeError("Session is closed")

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.
  • I have attempted to debug this myself, and I believe this issue is with the library

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions