diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index 625e7a2..d064b6a 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -148,11 +148,19 @@ namespace CryptoExchange.Net.Sockets } var handledResponse = false; - foreach (var pendingRequest in pendingRequests.ToList()) + PendingRequest[] requests; + lock(pendingRequests) + { + requests = pendingRequests.ToArray(); + } + foreach (var pendingRequest in requests) { if (pendingRequest.Check(tokenData)) { - pendingRequests.Remove(pendingRequest); + lock (pendingRequests) + { + pendingRequests.Remove(pendingRequest); + } if (pendingRequest.Result == null) { continue; // A previous timeout. @@ -237,7 +245,10 @@ namespace CryptoExchange.Net.Sockets public virtual Task SendAndWait(T obj, TimeSpan timeout, Func handler) { var pending = new PendingRequest(handler, timeout); - pendingRequests.Add(pending); + lock (pendingRequests) + { + pendingRequests.Add(pending); + } Send(obj); return pending.Event.WaitOneAsync(timeout); }