diff --git a/CryptoExchange.Net/Sockets/CryptoExchangeWebSocketClient.cs b/CryptoExchange.Net/Sockets/CryptoExchangeWebSocketClient.cs index dddaa2f..a80854c 100644 --- a/CryptoExchange.Net/Sockets/CryptoExchangeWebSocketClient.cs +++ b/CryptoExchange.Net/Sockets/CryptoExchangeWebSocketClient.cs @@ -250,7 +250,7 @@ namespace CryptoExchange.Net.Sockets /// public virtual async Task ReconnectAsync() { - if (_processState != ProcessState.Processing) + if (_processState != ProcessState.Processing && IsOpen) return; _log.Write(LogLevel.Debug, $"Socket {Id} reconnect requested"); diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index 512368c..46d597c 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -385,7 +385,7 @@ namespace CryptoExchange.Net.Sockets if (!subscriptions.Contains(subscription)) return; - subscriptions.Remove(subscription); + subscription.Closed = true; } if (Status == SocketStatus.Closing || Status == SocketStatus.Closed || Status == SocketStatus.Disposed) @@ -407,7 +407,7 @@ namespace CryptoExchange.Net.Sockets return; } - shouldCloseConnection = subscriptions.All(r => !r.UserSubscription); + shouldCloseConnection = subscriptions.All(r => !r.UserSubscription || r.Closed); if (shouldCloseConnection) Status = SocketStatus.Closing; } @@ -417,6 +417,9 @@ namespace CryptoExchange.Net.Sockets log.Write(LogLevel.Debug, $"Socket {SocketId} closing as there are no more subscriptions"); await CloseAsync().ConfigureAwait(false); } + + lock (subscriptionLock) + subscriptions.Remove(subscription); } /// diff --git a/CryptoExchange.Net/Sockets/SocketSubscription.cs b/CryptoExchange.Net/Sockets/SocketSubscription.cs index 544c517..62e7b87 100644 --- a/CryptoExchange.Net/Sockets/SocketSubscription.cs +++ b/CryptoExchange.Net/Sockets/SocketSubscription.cs @@ -48,6 +48,11 @@ namespace CryptoExchange.Net.Sockets /// public bool Authenticated { get; set; } + /// + /// Whether we're closing this subscription and a socket connection shouldn't be kept open for it + /// + public bool Closed { get; set; } + /// /// Cancellation token registration, should be disposed when subscription is closed. Used for closing the subscription with /// a provided cancelation token