#`RuntimeError: Session is closed` when using `bot.close()`

1 messages · Page 1 of 1 (latest)

grave burrow
#

Hi, for some reason I get this error when I stop my bot using bot.close(). I tried this on a clean bot too and it still persists. Using CTRL+C does not have this issue. Anyone have an idea what might cause this? Can you reproduce this? I'm on Python 3.12.4 and pycord 2.7.1.

bot = discord.Bot(intents=discord.Intents.all(), debug_guilds=[...])

@bot.command()
@discord.default_permissions(administrator=True)
async def stop(ctx: discord.ApplicationContext):
    """Stop the bot."""
    await ctx.respond("Stopping the bot...", ephemeral=True)
    await bot.close()

bot.run(os.getenv("BOT_TOKEN"))
Traceback (most recent call last):
  File "...\main.py", line 27, in <module>
    bot.run(os.getenv("BOT_TOKEN"))
  File "...\discord\client.py", line 881, in run
    return future.result()
           ^^^^^^^^^^^^^^^
  File "...\discord\client.py", line 860, in runner
    await self.start(*args, **kwargs)
  File "...\discord\client.py", line 824, in start
    await self.connect(reconnect=reconnect)
  File "...\discord\client.py", line 713, in connect
    self.ws = await asyncio.wait_for(coro, timeout=60.0)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\asyncio\tasks.py", line 520, in wait_for
    return await fut
           ^^^^^^^^^
  File "...\discord\gateway.py", line 346, in from_client
    gateway = gateway or await client.http.get_gateway()
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\discord\http.py", line 3234, in get_gateway
    data = await self.request(Route("GET", "/gateway"))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\discord\http.py", line 299, in request
    async with self.__session.request(
  File "...\aiohttp\client.py", line 1510, in __aenter__
    self._resp: _RetType = await self._coro
                           ^^^^^^^^^^^^^^^^
  File "...\aiohttp\client.py", line 529, in _request
    raise RuntimeError("Session is closed")
RuntimeError: Session is closed
#

RuntimeError: Session is closed when using bot.close()

wraith frost
#

try using sys.exit()

grave burrow
#

but I need to run some code after bot.run()...

#

like cleanup and stuff

#

using sys.exit() would just kill the bot, no?

#

like

bot = discord.Bot(intents=discord.Intents.all(), debug_guilds=[717770840221024422])

@bot.command()
@discord.default_permissions(administrator=True)
async def stop(ctx: discord.ApplicationContext):
    """Stop the bot."""
    await ctx.respond("Stopping the bot...", ephemeral=True)
    await bot.close()

dotenv.load_dotenv()
bot.run(os.getenv("BOT_TOKEN"))

bot.cleanup()
print("See you soon!")
wraith frost
#
try:
    bot.run(...)
except RuntimeError: # or some other error
    cleanup()
    raise
grave burrow
#

Yeah ok, sure I can catch it but that doesn't solve the underlying problem, does it? It should not throw this error in the first place. Can you reproduce the error?

#

I want to make sure it's not just me, before opening an Issue on github

wraith frost
#

When using bot.close, cleanup steps aren't executed

#

bot.run uses those

#

However something you could try is this pull request:

grave burrow
#

So just raising a KeyboardInterrupt is better than using bot.close() ig xD

wraith frost
#

We might change this in the future but we need to refactor the asyncio setup as shown above first

grave burrow
#

Alright, thanks for the help 🙂