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

This commit is contained in:
Paulo
2021-06-15 13:32:17 -03:00
committed by GitHub
parent f47001a1c9
commit 97d90b9e8a
2 changed files with 13 additions and 6 deletions

View File

@@ -188,7 +188,7 @@ namespace Discord.API
catch { }
if (ex is GatewayReconnectException)
await WebSocketClient.DisconnectAsync(4000);
await WebSocketClient.DisconnectAsync(4000).ConfigureAwait(false);
else
await WebSocketClient.DisconnectAsync().ConfigureAwait(false);

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;