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() public virtual async Task CloseAsync()
{ {
await _closeSem.WaitAsync().ConfigureAwait(false); await _closeSem.WaitAsync().ConfigureAwait(false);
_stopRequested = true;
try try
{ {
if (_closeTask != null && !_closeTask.IsCompleted) if (_closeTask?.IsCompleted == false)
{ {
_log.Write(LogLevel.Debug, $"Socket {Id} CloseAsync() waiting for existing close task"); _log.Write(LogLevel.Debug, $"Socket {Id} CloseAsync() waiting for existing close task");
await _closeTask.ConfigureAwait(false); await _closeTask.ConfigureAwait(false);
return; return;
} }
_stopRequested = true;
if (!IsOpen) if (!IsOpen)
{ {
_log.Write(LogLevel.Debug, $"Socket {Id} CloseAsync() socket not open"); _log.Write(LogLevel.Debug, $"Socket {Id} CloseAsync() socket not open");
@ -430,7 +430,8 @@ namespace CryptoExchange.Net.Sockets
{ {
// Connection closed unexpectedly, .NET framework // Connection closed unexpectedly, .NET framework
OnError?.Invoke(ioe); OnError?.Invoke(ioe);
_closeTask = CloseInternalAsync(); if (_closeTask?.IsCompleted != false)
_closeTask = CloseInternalAsync();
break; 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 // 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. // 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 // 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); OnError?.Invoke(e);
throw; throw;
} }
@ -486,7 +488,8 @@ namespace CryptoExchange.Net.Sockets
{ {
// Connection closed unexpectedly // Connection closed unexpectedly
OnError?.Invoke(wse); OnError?.Invoke(wse);
_closeTask = CloseInternalAsync(); if (_closeTask?.IsCompleted != false)
_closeTask = CloseInternalAsync();
break; break;
} }
@ -494,7 +497,8 @@ namespace CryptoExchange.Net.Sockets
{ {
// Connection closed unexpectedly // Connection closed unexpectedly
_log.Write(LogLevel.Debug, $"Socket {Id} received `Close` message"); _log.Write(LogLevel.Debug, $"Socket {Id} received `Close` message");
_closeTask = CloseInternalAsync(); if (_closeTask?.IsCompleted != false)
_closeTask = CloseInternalAsync();
break; 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 // 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. // 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 // 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); OnError?.Invoke(e);
throw; throw;
} }