1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Small fix for socket possibly reconnecting while it should close

This commit is contained in:
Jkorf 2022-12-08 11:54:10 +01:00
parent 71072680a8
commit 8336d373f3

View File

@ -293,17 +293,17 @@ namespace CryptoExchange.Net.Sockets
public virtual async Task CloseAsync()
{
await _closeSem.WaitAsync().ConfigureAwait(false);
_stopRequested = true;
try
{
if (_closeTask != null && !_closeTask.IsCompleted)
if (_closeTask?.IsCompleted == false)
{
_log.Write(LogLevel.Debug, $"Socket {Id} CloseAsync() waiting for existing close task");
await _closeTask.ConfigureAwait(false);
return;
}
_stopRequested = true;
if (!IsOpen)
{
_log.Write(LogLevel.Debug, $"Socket {Id} CloseAsync() socket not open");
@ -430,6 +430,7 @@ namespace CryptoExchange.Net.Sockets
{
// Connection closed unexpectedly, .NET framework
OnError?.Invoke(ioe);
if (_closeTask?.IsCompleted != false)
_closeTask = CloseInternalAsync();
break;
}
@ -441,6 +442,7 @@ namespace CryptoExchange.Net.Sockets
// Because this is running in a separate task and not awaited until the socket gets closed
// any exception here will crash the send processing, but do so silently unless the socket get's stopped.
// Make sure we at least let the owner know there was an error
_log.Write(LogLevel.Warning, $"Socket {Id} Send loop stopped with exception");
OnError?.Invoke(e);
throw;
}
@ -486,6 +488,7 @@ namespace CryptoExchange.Net.Sockets
{
// Connection closed unexpectedly
OnError?.Invoke(wse);
if (_closeTask?.IsCompleted != false)
_closeTask = CloseInternalAsync();
break;
}
@ -494,6 +497,7 @@ namespace CryptoExchange.Net.Sockets
{
// Connection closed unexpectedly
_log.Write(LogLevel.Debug, $"Socket {Id} received `Close` message");
if (_closeTask?.IsCompleted != false)
_closeTask = CloseInternalAsync();
break;
}
@ -559,6 +563,7 @@ namespace CryptoExchange.Net.Sockets
// Because this is running in a separate task and not awaited until the socket gets closed
// any exception here will crash the receive processing, but do so silently unless the socket gets stopped.
// Make sure we at least let the owner know there was an error
_log.Write(LogLevel.Warning, $"Socket {Id} Receive loop stopped with exception");
OnError?.Invoke(e);
throw;
}