Browse Source

fix: Connection deadlock when trying to Send and Disconnect (#1872)

tags/3.0.0
Paulo GitHub 3 years ago
parent
commit
97d90b9e8a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions
  1. +2
    -2
      src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
  2. +11
    -4
      src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs

+ 2
- 2
src/Discord.Net.WebSocket/DiscordSocketApiClient.cs View File

@@ -188,9 +188,9 @@ namespace Discord.API
catch { }

if (ex is GatewayReconnectException)
await WebSocketClient.DisconnectAsync(4000);
await WebSocketClient.DisconnectAsync(4000).ConfigureAwait(false);
else
await WebSocketClient.DisconnectAsync().ConfigureAwait(false);
await WebSocketClient.DisconnectAsync().ConfigureAwait(false);

ConnectionState = ConnectionState.Disconnected;
}


+ 11
- 4
src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs View File

@@ -108,11 +108,11 @@ namespace Discord.Net.WebSockets
}
private async Task DisconnectInternalAsync(int closeCode = 1000, bool isDisposing = false)
{
_isDisconnecting = true;

try { _disconnectTokenSource.Cancel(false); }
catch { }

_isDisconnecting = true;

if (_client != null)
{
if (!isDisposing)
@@ -166,7 +166,14 @@ namespace Discord.Net.WebSockets

public async Task SendAsync(byte[] data, int index, int count, bool isText)
{
await _lock.WaitAsync().ConfigureAwait(false);
try
{
await _lock.WaitAsync(_cancelToken).ConfigureAwait(false);
}
catch (TaskCanceledException)
{
return;
}
try
{
if (_client == null) return;
@@ -201,7 +208,7 @@ namespace Discord.Net.WebSockets
{
while (!cancelToken.IsCancellationRequested)
{
WebSocketReceiveResult socketResult = await _client.ReceiveAsync(buffer, CancellationToken.None).ConfigureAwait(false);
WebSocketReceiveResult socketResult = await _client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false);
byte[] result;
int resultCount;



Loading…
Cancel
Save