fix: handle overflowing int max value and reset socket on failure

This commit is contained in:
Stevan Freeborn
2026-05-05 21:32:48 -05:00
parent b2a023f9c0
commit 4241258825
@@ -70,6 +70,8 @@ internal sealed class DiscordGatewayClient : IDiscordGatewayClient
await SetGatewayUrlAsync(gatewayUrl, cancellationToken); await SetGatewayUrlAsync(gatewayUrl, cancellationToken);
} }
_webSocket?.Dispose();
await SetWebSocketAsync(_webSocketFactory.Create(), cancellationToken); await SetWebSocketAsync(_webSocketFactory.Create(), cancellationToken);
await ConnectWithGatewayUrlAsync(cancellationToken); await ConnectWithGatewayUrlAsync(cancellationToken);
@@ -685,9 +687,10 @@ internal sealed class DiscordGatewayClient : IDiscordGatewayClient
await _webSocket.ConnectAsync(uri, cancellationToken); await _webSocket.ConnectAsync(uri, cancellationToken);
connected = true; connected = true;
} }
catch (Exception e) when (e is WebSocketException) catch (Exception e)
{ {
var currentDelay = (int)(delay.TotalMilliseconds * Math.Pow(2, attempts)); var calculatedDelay = delay.TotalMilliseconds * Math.Pow(2, attempts);
var currentDelay = (int)Math.Min(calculatedDelay, int.MaxValue);
_logger.LogWarning( _logger.LogWarning(
e, e,
@@ -700,6 +703,9 @@ internal sealed class DiscordGatewayClient : IDiscordGatewayClient
await Task.Delay(currentDelay, cancellationToken); await Task.Delay(currentDelay, cancellationToken);
attempts++; attempts++;
_webSocket.Dispose();
await SetWebSocketAsync(_webSocketFactory.Create(), cancellationToken);
} }
} }
} }