discord-net / Discord.Net

An unofficial .Net wrapper for the Discord API (https://discord.com/)
https://discordnet.dev
MIT License
3.33k stars 737 forks source link

Discord.Net doesn't automatically reconnect after network disconnect #960

Closed JustArchi closed 2 years ago

JustArchi commented 6 years ago
2018-02-22 14:06:21|INFO|ArchiBoT|OnLog() Discord | Discord.Net v2.0.0-beta (API v6)
(...)
2018-02-22 15:26:11|WARN|ArchiBoT|OnLog() Gateway |
2018-02-22 15:26:11|WARN|ArchiBoT|OnLog() System.Exception: Server missed last heartbeat
   at Discord.ConnectionManager.<>c__DisplayClass28_0.<<StartAsync>b__0>d.MoveNext()
2018-02-22 15:26:11|INFO|ArchiBoT|OnLog() Gateway | Disconnecting
2018-02-22 15:26:12|INFO|ArchiBoT|OnLog() Gateway | Disconnected
2018-02-22 15:26:13|INFO|ArchiBoT|OnLog() Gateway | Connecting
2018-02-22 15:27:58|INFO|ArchiBoT|OnLog() Gateway | Disconnecting
2018-02-22 15:27:58|INFO|ArchiBoT|OnLog() Gateway | Disconnected

Library properly attempted to reconnect after missing last heartbeat, but it seems like it failed with timeout after those ~45 seconds and decided to stop further attempts. According to https://github.com/RogueException/Discord.Net/commit/73ac9d7886aa48b9d809c56e51945056f3b67232 - similar issue should be already solved for 2.0.0-beta, but it seems that this issue is still recent in one form or another.

It should definitely keep on trying until it succeeds, unless this is somehow intended (in this case how we should handle it ourselves?)

Thank you in advance.

Citrom67 commented 4 years ago

I have the same issue for months now and I couldn't find a solution for it just yet

Untitled

SubZero0 commented 4 years ago

@Citrom67 your "issue" is a change made by Discord, update your Discord.Net version. I would recommend using the MyGet build that has a few more changes applied to help overall.

230Daniel commented 3 years ago

Edit: I resolved this issue, it turns out that I was requesting guild member downloads too frequently which killed the gateway connection. After disabling my "download those which aren't fully downloaded" loop, I haven't had a failed reconnect for 4 days.

Hi, I have a similar issue on one of the most recent dev versions (2.3.0-dev-20201126.1).

One or two times per day, my DiscordShardedClient logs this exception:

WebSocket connection was closed
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
Inner Exception: WebSocket connection was closed
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()

After this, the following is logged:

Disconnecting
Disconnected
Connecting
Failed to resume previous session
Connected

This is a big problem for me because it resets the user cache which must then be downloaded again. I have also noticed the bot going offline for a few minutes (with no console output) before the above exception is logged.

Is there any reason for the websocket connection being closed? I have safeguards against getting 403'd, and only rarely see ratelimit triggered messages. It's worth noting that I don't tend to see this issue on my small-scale bots.

Please let me know if I should be posting in a different issue or making a new issue. Thanks!

krypto5863 commented 3 years ago

I'm having the same issue as the person above with the latest stable release from nuget. Same exact error. Hope we get word on this soon.

230Daniel commented 3 years ago

I'm having the same issue as the person above with the latest stable release from nuget. Same exact error. Hope we get word on this soon.

I don't think the stable versions support resuming sessions at all @krypto5863. You should update to the dev versions which succeed in reconnecting most of the time. Add https://www.myget.org/F/discord-net/api/v3/index.json as a package source in the package manager and enable pre-releases.

cgoulart35 commented 3 years ago

I'm having the same issue as the person above with the latest stable release from nuget. Same exact error. Hope we get word on this soon.

I don't think the stable versions support resuming sessions at all @krypto5863. You should update to the dev versions which succeed in reconnecting most of the time. Add https://www.myget.org/F/discord-net/api/v3/index.json as a package source in the package manager and enable pre-releases.

@230Daniel has any progress been made on this issue? I am also receiving failures to reconnect (multiple everyday). I plan on trying to use dev version as you mentioned to see if that helps. Any word on stable release/fix?

230Daniel commented 3 years ago

Good news @cgoulart35, I did find a fix for this!

It turns out that making lots of gateway requests causes the WebSocket connection to drop. In my case, I had some code which tried to download users for every guild, one per second. With this code it dropped connection a few times per day, but after removing the code it's gone weeks without failing to restore the session once.

FireController1847 commented 3 years ago

I have a bot that's doing virtually nothing other than listening to events (join,leave,mod events) and responding with messages (I guess that's what all bots do though, ha), with little to no commands using the latest dev build and I am also recieving this issue:

11:16:17 Gateway     Disconnecting
11:16:17 Gateway     Disconnected
11:16:18 Gateway     Connecting
11:16:18 Gateway     Resumed previous session
11:16:18 Gateway     Connected
12:42:20 Gateway     System.Exception: WebSocket connection was closed
 ---> System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
 ---> System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
   at System.Net.WebSockets.ManagedWebSocket.ThrowIfEOFUnexpected(Boolean throwOnPrematureClosure)
   at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure)
   at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
   at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
   at Discord.Net.WebSockets.DefaultWebSocketClient.RunAsync(CancellationToken cancelToken)
   --- End of inner exception stack trace ---
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
12:42:20 Gateway     Disconnecting
12:42:20 Gateway     Disconnected
12:42:21 Gateway     Connecting
12:42:21 Gateway     Resumed previous session
12:42:21 Gateway     Connected
15:04:41 Gateway     Discord.WebSocket.GatewayReconnectException: Server missed last heartbeat
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
15:04:41 Gateway     Disconnecting
15:04:41 Gateway     Disconnected
15:04:42 Gateway     Connecting
15:06:22 Gateway     Disconnecting
15:06:22 Gateway     Disconnected

As you can see it handles errors completely fine and reconnects as it should, but it seems to be this "missed last heartbeat" that consistently makes the reconnect fail. I don't know if this is helpful at all, but hopefully it is.

I don't think this issue would be as frustrating if it either ended the process on the final disconnect or spit out some form of error, but it's simply disconnecting with nothing happening after that. Kind of like a soft-lock in a video game that doesn't allow the process to shut down but continues to be "online" so there's really nothing you can do to workaround the issue (that is, without forcing a full restart on every disconnect, which seems to be a terrible idea).

SubZero0 commented 3 years ago

I just merged two fixes to possible deadlocks, #1872 and #1873 They should deal with the valid reports that I read here.

These changes are effective in 2.4.1-dev and the latest 3.0.0-dev (so 3.0.0-dev-20210615.5) that are available in MyGet. Please use these before reporting any other deadlock.

As a note, the websocket connection being closed and reconnecting isn't a deadlock or an issue at all. If it reconnected, it's working.