diff --git a/lib/src/gateway/shard_runner.dart b/lib/src/gateway/shard_runner.dart index 756e08577..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}', @@ -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