discord-net / Discord.Net

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

[Bug]: Error when streaming audio for a long time #2762

Open josago97 opened 1 year ago

josago97 commented 1 year ago

Check The Docs

Verify Issue Source

Check your intents

Description

There is a problem with streaming audio when you are streaming for a long time. Using the audio streaming tutorial as an example, if you broadcast a radio station instead of streaming a file, a series of errors occur over the course of several hours, canceling and blocking a future stream. After transmission hours an error occurs The remote party closed the WebSocket connection without completing the close handshake. This error is followed by The server sent close 4006: "Session is no longer valid." After these errors, if you try to reconnect to the voice channel using the voiceChannel.ConnectAsync() method, it throws the System.TimeoutException: 'The operation has timed out' error, which prevents you from reconnecting until you restart the bot.

Version

3.12.0

Working Version

No response

Logs

# START BOT
[General/Info] 03:39:23 Discord     Discord.Net v3.12.0 (API v10)
[General/Info] 03:39:23 Gateway     Connecting
[General/Info] 03:39:24 Gateway     Connected
Ready
[General/Info] 03:39:25 Gateway     Ready

# PLAYING RADIO
[General/Info] 03:39:37 Audio #1    Connecting
[General/Warning] 03:39:37 Audio #1    Unknown OpCode (Hello)
[General/Warning] 03:39:37 Audio #1    Unknown OpCode (18)
[General/Warning] 03:39:37 Audio #1    Unknown OpCode (20)
[General/Info] 03:39:38 Audio #1    Connected
[General/Warning] 04:37:14 Gateway     Discord.WebSocket.GatewayReconnectException: Server requested a reconnect
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
[General/Info] 04:37:14 Gateway     Disconnecting
[General/Info] 04:37:14 Gateway     Disconnected
[General/Info] 04:37:15 Gateway     Connecting
[General/Info] 04:37:15 Gateway     Connected
[General/Info] 04:37:15 Gateway     Resumed previous session
[General/Warning] 06:43:55 Gateway     Discord.WebSocket.GatewayReconnectException: Server requested a reconnect
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
[General/Info] 06:43:55 Gateway     Disconnecting
[General/Info] 06:43:55 Gateway     Disconnected
[General/Info] 06:43:56 Gateway     Connecting
[General/Info] 06:43:56 Gateway     Connected
[General/Info] 06:43:56 Gateway     Resumed previous session
[General/Warning] 10:23:50 Gateway     Discord.WebSocket.GatewayReconnectException: Server requested a reconnect
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
[General/Info] 10:23:50 Gateway     Disconnecting
[General/Info] 10:23:50 Gateway     Disconnected
[General/Info] 10:23:51 Gateway     Connecting
[General/Info] 10:23:51 Gateway     Connected
[General/Info] 10:23:51 Gateway     Resumed previous session
[General/Warning] 12:13:12 Audio #1    Unknown OpCode (18)
[General/Warning] 12:13:12 Audio #1    Unknown OpCode (20)
[General/Warning] 12:22:59 Gateway     Discord.WebSocket.GatewayReconnectException: Server requested a reconnect
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
[General/Info] 12:22:59 Gateway     Disconnecting
[General/Info] 12:22:59 Gateway     Disconnected
[General/Info] 12:23:00 Gateway     Connecting
[General/Info] 12:23:01 Gateway     Connected
[General/Info] 12:23:01 Gateway     Resumed previous session
[General/Warning] 12:56:18 Audio #1    System.Exception: WebSocket connection was closed
 ---> 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.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TResult](Memory`1 payloadBuffer, CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask`1.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
--- End of stack trace from previous location ---
   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()
[General/Info] 12:56:18 Audio #1    Disconnecting
[General/Info] 12:56:18 Audio #1    Disconnected
[General/Info] 12:56:19 Audio #1    Connecting
[General/Warning] 12:56:19 Audio #1    Unknown OpCode (Hello)
[General/Error] 12:56:19 Audio #1    System.Exception: WebSocket session expired
 ---> Discord.Net.WebSocketClosedException: The server sent close 4006: "Session is no longer valid."
   at Discord.Net.WebSockets.DefaultWebSocketClient.RunAsync(CancellationToken cancelToken)
   --- End of inner exception stack trace ---
   at Discord.ConnectionManager.WaitAsync()
   at Discord.Audio.AudioClient.OnConnectingAsync()
   at Discord.ConnectionManager.ConnectAsync(CancellationTokenSource reconnectCancelToken)
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
[General/Info] 12:56:19 Audio #1    Disconnecting
[General/Info] 12:56:19 Audio #1    Disconnected
# END PLAY RADIO

[General/Warning] 14:17:48 Gateway     Discord.WebSocket.GatewayReconnectException: Server requested a reconnect
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
[General/Info] 14:17:48 Gateway     Disconnecting
[General/Info] 14:17:48 Gateway     Disconnected
[General/Info] 14:17:49 Gateway     Connecting
[General/Info] 14:17:49 Gateway     Connected
[General/Info] 14:17:49 Gateway     Resumed previous session
[General/Warning] 16:45:28 Gateway     Discord.WebSocket.GatewayReconnectException: Server requested a reconnect
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()
[General/Info] 16:45:28 Gateway     Disconnecting
[General/Info] 16:45:28 Gateway     Disconnected
[General/Info] 16:45:29 Gateway     Connecting
[General/Info] 16:45:29 Gateway     Resumed previous session
[General/Info] 16:45:29 Gateway     Connected
[General/Info] 16:48:41 Audio #2    Connecting
# TRYING CONNECT TO VOICE CHANNEL FAILED WITH ERROR:
System.TimeoutException: 'The operation has timed out.'
   at Discord.WebSocket.SocketGuild.<ConnectAudioAsync>d__334.MoveNext()
   at Discord.WebSocket.SocketGuild.<ConnectAudioAsync>d__334.MoveNext()
   at Discord.WebSocket.SocketVoiceChannel.<ConnectAsync>d__24.MoveNext()
   at DiscordBot.Logic.Audio.Bueno.Models.Player.<EnsureVoiceChannelAsync>d__38.MoveNext()

Sample

No response

Packages

.NET 6

Environment

Windows 10 Pro x64 22H2 19045.3324 .NET 6.0.21

DeclanFrampton commented 1 year ago

I am assuming this would close the PR #2765 correct? @josago97

If so, link this bug within the PR with closes tag and if/when PR is approved this will also be closed.

josago97 commented 1 year ago

No, that PR doesn't solve this problem. When I was trying to solve this problem I noticed that the discord heartbeat was not being used and that is why I made the PR. As I have been testing, I think this problem has to do with a bad connection closure, in such a way that when it reconnects it gives an error due to residue from the previous disconnection.