From 2b6f4d5b66d9775ad57b5a38993da8a8486f1dee Mon Sep 17 00:00:00 2001 From: Mylo Fawcett Date: Sat, 7 Sep 2024 02:09:11 +0200 Subject: [PATCH 1/2] Don't set local close code when remote closes the connection --- lib/src/gateway/shard_runner.dart | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/src/gateway/shard_runner.dart b/lib/src/gateway/shard_runner.dart index 756e08577..e14d67ec0 100644 --- a/lib/src/gateway/shard_runner.dart +++ b/lib/src/gateway/shard_runner.dart @@ -360,7 +360,16 @@ class ShardConnection extends Stream implements StreamSink { _currentRateLimitEnd.complete(); _currentRateLimitEnd = Completer(); }); - websocket.done.then((_) => close()); + websocket.done.then((_) { + _rateLimitResetTimer.cancel(); + if (!_currentRateLimitEnd.isCompleted) { + _currentRateLimitEnd + // Don't report an uncaught async error for the future. + ..future.ignore() + ..completeError(StateError('Connection is closed'), StackTrace.current); + } + _sentController.close(); + }); } static Future connect(String gatewayUri, ShardRunner runner) async { @@ -435,15 +444,9 @@ class ShardConnection extends Stream implements StreamSink { Future close([int code = 1000]) async { localCloseCode ??= code; - _rateLimitResetTimer.cancel(); - if (!_currentRateLimitEnd.isCompleted) { - _currentRateLimitEnd - // Install an error handler so the error is not counted as uncaught. - ..future.catchError((e) {}) - ..completeError(StateError('Connection is closed'), StackTrace.current); - } await websocket.close(code); - await _sentController.close(); + + return done; } @override From 204df18b2484b275fdf3c49afa7bd2a0fa3d8fe3 Mon Sep 17 00:00:00 2001 From: Mylo Fawcett Date: Fri, 14 Feb 2025 10:54:26 +0100 Subject: [PATCH 2/2] Try to resume more aggressively --- lib/src/gateway/shard_runner.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/gateway/shard_runner.dart b/lib/src/gateway/shard_runner.dart index e14d67ec0..a6900dcc9 100644 --- a/lib/src/gateway/shard_runner.dart +++ b/lib/src/gateway/shard_runner.dart @@ -214,7 +214,7 @@ class ShardRunner { // Check if we can resume based on close code if the connection was closed by Discord. if (connection!.localCloseCode == null) { // https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-close-event-codes - const resumableCodes = [null, 4000, 4001, 4002, 4003, 4005, 4008]; + const newSessionCodes = [4007, 4009]; const errorCodes = [4004, 4010, 4011, 4012, 4013, 4014]; if (errorCodes.contains(connection!.remoteCloseCode)) { @@ -222,7 +222,7 @@ class ShardRunner { return; } - canResume = resumableCodes.contains(connection!.remoteCloseCode); + canResume = !newSessionCodes.contains(connection!.remoteCloseCode); controller.add(ErrorReceived( error: 'Connection was closed with code ${connection!.remoteCloseCode}',