Fix potential incorrect connection state resulting in null reference

This commit is contained in:
Dean Herbert 2022-11-18 13:45:46 +09:00
parent 1ff1738988
commit a5d22195f2
1 changed files with 6 additions and 2 deletions

View File

@ -145,7 +145,9 @@ private async Task handleErrorAndDelay(Exception exception, CancellationToken ca
private async Task onConnectionClosed(Exception? ex, CancellationToken cancellationToken)
{
isConnected.Value = false;
bool hasBeenCancelled = cancellationToken.IsCancellationRequested;
await disconnect(true);
if (ex != null)
await handleErrorAndDelay(ex, cancellationToken).ConfigureAwait(false);
@ -153,7 +155,7 @@ private async Task onConnectionClosed(Exception? ex, CancellationToken cancellat
Logger.Log($"{ClientName} disconnected", LoggingTarget.Network);
// make sure a disconnect wasn't triggered (and this is still the active connection).
if (!cancellationToken.IsCancellationRequested)
if (!hasBeenCancelled)
await Task.Run(connect, default).ConfigureAwait(false);
}
@ -174,7 +176,9 @@ private async Task disconnect(bool takeLock)
}
finally
{
isConnected.Value = false;
CurrentConnection = null;
if (takeLock)
connectionLock.Release();
}