From b678005a2138627e61015c2f74ab30290ce03961 Mon Sep 17 00:00:00 2001 From: AraHaan Date: Thu, 30 Apr 2026 16:28:41 -0400 Subject: [PATCH 1/2] This fixes a potential WebSocketException crash that would print to console without getting logged via the logger when the user just so happens to lose internet (say router gets rebooted by ISP) right when Discord wants ``ResumeAsync`` to be called. This also fixes the issue at startup as well where the user infortunately gets hit by this as soon as they start their bot, but just before any attempt is made to connect to discord (there is a small window between ``Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)`` and ``NetCord.Gateway.GatewayClient.StartAsync(PresenceProperties presence, CancellationToken cancellationToken)`` prior to the call to ``StartAsync`` in WebSocketClient. The fix here is to instead log the exceptions via the logger. --- NetCord/Gateway/GatewayClient.cs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/NetCord/Gateway/GatewayClient.cs b/NetCord/Gateway/GatewayClient.cs index 4977541e..3ab7418c 100644 --- a/NetCord/Gateway/GatewayClient.cs +++ b/NetCord/Gateway/GatewayClient.cs @@ -5,6 +5,7 @@ using NetCord.Gateway.WebSockets; using NetCord.Logging; +using WebSocketException = System.Net.WebSockets.WebSocketException; using WebSocketCloseStatus = System.Net.WebSockets.WebSocketCloseStatus; namespace NetCord.Gateway; @@ -916,8 +917,18 @@ private ValueTask SendIdentifyAsync(ConnectionState connectionState, PresencePro /// public async ValueTask StartAsync(PresenceProperties? presence = null, CancellationToken cancellationToken = default) { - var connectionState = await StartAsync(new State(), cancellationToken).ConfigureAwait(false); - await SendIdentifyAsync(connectionState, presence, cancellationToken).ConfigureAwait(false); + try + { + var connectionState = await StartAsync(new State(), cancellationToken).ConfigureAwait(false); + await SendIdentifyAsync(connectionState, presence, cancellationToken).ConfigureAwait(false); + } + catch (WebSocketException ex) + { + Log(LogLevel.Error, null, ex, static (s, e) => + { + return $"An error occurred while starting the gateway client.{Environment.NewLine}{e}"; + }); + } } /// @@ -929,8 +940,18 @@ public async ValueTask StartAsync(PresenceProperties? presence = null, Cancellat /// public async ValueTask ResumeAsync(string sessionId, int sequenceNumber, CancellationToken cancellationToken = default) { - var connectionState = await StartAsync(new State(), cancellationToken).ConfigureAwait(false); - await TryResumeAsync(connectionState, SessionId = sessionId, SequenceNumber = sequenceNumber, cancellationToken).ConfigureAwait(false); + try + { + var connectionState = await StartAsync(new State(), cancellationToken).ConfigureAwait(false); + await TryResumeAsync(connectionState, SessionId = sessionId, SequenceNumber = sequenceNumber, cancellationToken).ConfigureAwait(false); + } + catch (WebSocketException ex) + { + Log(LogLevel.Error, null, ex, static (s, e) => + { + return $"An error occurred while resuming.{Environment.NewLine}{e}"; + }); + } } private protected override bool Reconnect(WebSocketCloseStatus? status, string? description) From f9712807329f70e1eef50d2773fae443fa43528e Mon Sep 17 00:00:00 2001 From: AraHaan Date: Sat, 2 May 2026 06:36:18 -0400 Subject: [PATCH 2/2] Rethrow the exception after writing it to the log. --- NetCord/Gateway/GatewayClient.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NetCord/Gateway/GatewayClient.cs b/NetCord/Gateway/GatewayClient.cs index 3ab7418c..203c4dbb 100644 --- a/NetCord/Gateway/GatewayClient.cs +++ b/NetCord/Gateway/GatewayClient.cs @@ -928,6 +928,7 @@ public async ValueTask StartAsync(PresenceProperties? presence = null, Cancellat { return $"An error occurred while starting the gateway client.{Environment.NewLine}{e}"; }); + throw; } } @@ -951,6 +952,7 @@ public async ValueTask ResumeAsync(string sessionId, int sequenceNumber, Cancell { return $"An error occurred while resuming.{Environment.NewLine}{e}"; }); + throw; } }