When using AutoShardedClient (and by inheritance AutoShardedBot) manually closing the client using AutoShardedClient.close() closes the connection to Discord, but never actually causes AutoShardedClient.run() to return, preventing the program from terminating.
This is caused by an issue in shard.py inside AutoShardedClient.connect() (shard.py, line 401) where the while-loop never actually terminates due to no more events being added to the internal queue, causing it to forever wait on the next item.
When AutoShardedClient.close() is called, and all shards are closed, no further events are added to AutoShardedClient.__queue, causing the loop to wait indefinitely for the next item from the queue.
A possible solution that I have found, although I don't know how good it is, is to add the following code to the end of the current AutoShardedClient.close() method, after the call to await self.http.close():
self.__queue.put(EventItem(None, None, None))
The event item is received from the queue, its type does not match any of EventType and thus directly leads into the next iteration of the loop, where the check in while not self.is_closed() causes the loop to exit, and AutoShardedBot.connect() to return.
Reproduction Steps
The following code is enough to reproduce the bug:
import discord
token = "YOUR TOKEN GOES HERE"
client = discord.AutoShardedClient()
@client.event
async def on_ready():
print("calling client.close()")
await client.close()
print("client.close() was called")
print("calling client.run()")
client.run(token)
print("this is never printed")
This prints the following, showcasing that the final print() after client.run(token) is never called.
calling client.run()
calling client.close()
client.close() was called
Expected Results
AutoShardedClient.run() should return after a successful call to AutoShardedClient.close().
Actual Results
AutoShardedClient.run() blocks indefinitely, causing the program to never finish.
Checklist
[ x ] I have searched the open issues for duplicates.
[ x ] I have shown the entire traceback, if possible.
[ x ] I have removed my token from display, if visible.
Summary
When using
AutoShardedClient
(and by inheritanceAutoShardedBot
) manually closing the client usingAutoShardedClient.close()
closes the connection to Discord, but never actually causesAutoShardedClient.run()
to return, preventing the program from terminating.This is caused by an issue in
shard.py
insideAutoShardedClient.connect()
(shard.py, line 401) where the while-loop never actually terminates due to no more events being added to the internal queue, causing it to forever wait on the next item.When
AutoShardedClient.close()
is called, and all shards are closed, no further events are added toAutoShardedClient.__queue
, causing the loop to wait indefinitely for the next item from the queue.A possible solution that I have found, although I don't know how good it is, is to add the following code to the end of the current
AutoShardedClient.close()
method, after the call toawait self.http.close()
:The event item is received from the queue, its type does not match any of
EventType
and thus directly leads into the next iteration of the loop, where the check inwhile not self.is_closed()
causes the loop to exit, andAutoShardedBot.connect()
to return.Reproduction Steps
The following code is enough to reproduce the bug:
This prints the following, showcasing that the final
print()
afterclient.run(token)
is never called.Expected Results
AutoShardedClient.run()
should return after a successful call toAutoShardedClient.close()
.Actual Results
AutoShardedClient.run()
blocks indefinitely, causing the program to never finish.Checklist
System Information