I found that when I tried to call self.close()
for a class that inherited from discord.Bot:
class MyBot(discord.Bot):
async def on_ready(self):
while True:
try:
#<Do stuff here>
gc.collect()
break
gc.collect()
await asyncio.sleep(10)
await self.close()
#<After more code here>
MyBot.run(token_value)
I got the below error:
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x00000203F78E7970>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x00000203F78EF8E0>, 308164.671)]']
connector: <aiohttp.connector.TCPConnector object at 0x00000203F78E7760>
Fatal error on SSL transport
The above error also happened when I didn’t inherit discord.Bot
, but just directly used the instance name:
bot_instance = discord.Bot(intents=intents)
async def on_ready():
while True:
try:
#<Do stuff here>
gc.collect()
break
gc.collect()
await asyncio.sleep(10)
await bot_instance.close()
#<After more code here>
bot_instance.run(token_value)
I’m not quite sure what I’m doing wrong. If I’m not mistaken, using .close()
should allow for the connection to be closed gracefully, and for the process to be ended. Instead, what happens is that the process errors out at .close()
, and the process gets ended by the resulting exception (I tried this with adding print
statements immediately after .close()
. Any code that came after .close()
was not run at all).
Unlike the question in I keep getting the error ‘Unclosed client session’ in Discord.py, I am already using the run
method at the bottom of my file: bot_instance.run(token_value)