Incapamentum / Exalted-Sage

Discord bot that does API requests to retrieve a collection of daily achievements for the next day and pushes an alert when any that is in a watchlist shows up.
3 stars 0 forks source link

BUG - `Discord.WebSocket.GatewayReconnectException: Server requested a reconnect at Discord.ConnectionManager.` #42

Closed Incapamentum closed 1 year ago

Incapamentum commented 1 year ago

This has been a persistent issue since the bot was first developed. It's possible the error is due to the service being "AFK" and Discord disconnecting the client (i.e. the bot), but then possibly the API is attempting to reconnect. I'm not sure too much about the specifics at the moment, but it would be nice to visit this issue. Consider this low-priority for the time being.

Posting the message:

22:08:26 Gateway     Discord.WebSocket.GatewayReconnectException: Server requested a reconnect
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
22:08:26 Gateway     Disconnecting
22:08:26 Gateway     Disconnected
22:08:27 Gateway     Connecting
Incapamentum commented 1 year ago

Possibly related to this issue: the bot timing out, disconnecting, then reconnecting may be causing multiple "threads" of execution to occur.

I currently am not able to confirm if this is the case. This is just a hunch. However, over a period of time, the bot ends up posting multiple times upon execution of a timed async task. This may be fixed with a simple flag procedure, which I'll go ahead and implement soon.

Details will be updated.

Incapamentum commented 1 year ago

This issue itself will not be assigned a branch as of yet. However, one was created to address the bot posting multiple times during a server reset (referred to as server-reset-async-flag-fix).

Incapamentum commented 1 year ago

It's possible I may have figured out the core issue of the problem, but I currently do not have a way to entirely verify if that's the case. This is moreso based on a hunch.

First and foremost: the OnServerReset() task was wrapped in a PeriodicAsync() task, which was THEN wrapped in a ReadyAsync() task that was hooked up to a Ready event.

According to Discord.NET documentation, this Ready event is fired when guild data has finished downloading. It is possible that this event gets fired over time anytime the bot disconnects/reconnects, leading to (possibly) multiple instances of the task running simultaneously.

I also did just now figure out that the client socket supports Connected and Disconnected events. As the issue was suspected to have been caused by disconnects/reconnects, it might end up making it a clean implementation for me to wrap the OnServerReset() within async tasks for both events. In other words: on connection, start the task, stop it on disconnect.

Incapamentum commented 1 year ago

It seems in my quest to resolve this (long-running) issue, I may have also unintentionally verified the above behavior.

It is definitely possible that the client is firing multiple Connected events, and I was able to see it when making use of Task completion guards through a TaskCompletionSource that is associated with the ReadyAsync() event handler that was coded up.

Will look into seeing on linking a branch to the issue and proceeding with a PR.