#RuntimeError: Session is closed

1 messages · Page 1 of 1 (latest)

hard pecan
#

I can't understand what the problem is and how to solve it. The bot itself works fine, but when I stop it, I get the error RuntimeError: Session is closed in the log

import logging

from interactions import Activity, ActivityType, Client, Intents, Status


class Bot(Client):
    def __init__(self, token: str) -> None:
        super().__init__(
            activity=Activity(
                type=ActivityType.COMPETING,
                name='Valorant❤️',
            ),
            status=Status.DO_NOT_DISTURB,
            intents=Intents.ALL,
            disable_dm_commands=True,
            logging_level=logging.INFO,
            basic_logging=True,
            token=token,
        )

    async def bot_launch(self) -> None:
        await self.astart()
import asyncio
import logging
from sys import stdout

from src.common import EXTENSIONS, env
from src.core import Bot
from src.utils import setup_extensions


async def main() -> None:
    bot = Bot(env.bot.token.get_secret_value())
    setup_extensions(bot, EXTENSIONS)
    await bot.bot_launch()


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, stream=stdout)
    try:
        asyncio.run(main())
    except (KeyboardInterrupt, SystemExit):
        logging.error('Bot stopped!')
zinc pagodaBOT
#

Hey! Once your issue is solved, press the button below to close this thread!

hard pecan
#
ERROR:root:Bot stopped!
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-52' coro=<WebsocketClient.receive() done, defined at /home/c0mrade/PycharmProjects/VANGUARD/.venv/lib/python3.12/site-packages/interactions/api/gateway/websocket.py:167> exception=RuntimeError('Session is closed')>
Traceback (most recent call last):
  File "/home/c0mrade/PycharmProjects/VANGUARD/.venv/lib/python3.12/site-packages/interactions/api/gateway/websocket.py", line 214, in receive
    await self.reconnect(resume=True)
  File "/home/c0mrade/PycharmProjects/VANGUARD/.venv/lib/python3.12/site-packages/interactions/api/gateway/gateway.py", line 281, in reconnect
    await super().reconnect(resume=resume, code=code, url=url)
  File "/home/c0mrade/PycharmProjects/VANGUARD/.venv/lib/python3.12/site-packages/interactions/api/gateway/websocket.py", line 259, in reconnect
    self.ws = await self.state.client.http.websocket_connect(url or self.ws_url)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/c0mrade/PycharmProjects/VANGUARD/.venv/lib/python3.12/site-packages/interactions/api/http/http_client.py", line 593, in websocket_connect
    return await self.__session.ws_connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/c0mrade/PycharmProjects/VANGUARD/.venv/lib/python3.12/site-packages/aiohttp/client.py", line 1067, in _ws_connect
    resp = await self.request(
           ^^^^^^^^^^^^^^^^^^^
  File "/home/c0mrade/PycharmProjects/VANGUARD/.venv/lib/python3.12/site-packages/aiohttp/client.py", line 527, in _request
    raise RuntimeError("Session is closed")
RuntimeError: Session is closed
hard pecan
#

Even after I made a few changes to bot_launch, the exception still appears after stopping the bot

    async def bot_launch(self) -> None:
        try:
            await self.astart()
        finally:
            await self.stop()
hard pecan
#

👀

cunning dove
hard pecan
#

Maybe this will help in fixing the bug

cunning dove
#

not sure how that happens, but a fix to the underlying error has been made in #1780 and should be available in stable next update

zinc pagodaBOT
# cunning dove not sure how that happens, but a fix to the underlying error has been made in #1...

• Pull Request Type: Bugfix
• Created <t:1755057881:R>

Description

#1778 mentions an interesting bug that came up because of a bug fix from aiohttp. This is because, when a session closes, aiohttp will now dispatch a close event rather than abruptly ending. Our code was not expecting this possibility, and so when handling a pre-established edge case, it errored out.

This PR fixes any errors that come from such a behavior.

Changes
  • Add a new closed attribute to HTTPClient.
  • Make WebsocketClient send an internal "no-op" op code if the websocket receives a CLOSED even and the HTTP client is closed.
    • Closing is already handled by other means later in the code. We have no need to implement actual behavior.
    • The internal op code may be controversial, but we should never get it under normal circumstances anyways.
  • Make the no-op op code make GatewayClient simply ignore it.
Related Issues

Closes #1778.

Test Scenarios

[CODEBLOCK]

Python Compatibility
  • ❌ I've ensured my code works on Python 3.10.x
  • ✅ I've ensured my code works on Python 3.11.x
Checklist
  • ✅ I've run the pre-commit code linter over all edited files
  • ✅ I've tested my changes on supported Python versions
  • ❌ I've added tests for my code, if applicable
  • ❌ I've updated / added documentation, where applicable