Skip to content

Commit

Permalink
Don't set local close code when remote closes the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything committed Sep 9, 2024
1 parent 002cdfc commit dece582
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/src/gateway/shard_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,16 @@ class ShardConnection extends Stream<GatewayEvent> implements StreamSink<Send> {
_currentRateLimitEnd.complete();
_currentRateLimitEnd = Completer<void>();
});
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<ShardConnection> connect(String gatewayUri, ShardRunner runner) async {
Expand Down Expand Up @@ -435,15 +444,9 @@ class ShardConnection extends Stream<GatewayEvent> implements StreamSink<Send> {
Future<void> 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
Expand Down

0 comments on commit dece582

Please sign in to comment.