diff --git a/src/Alchemy/WebSocketServer.cs b/src/Alchemy/WebSocketServer.cs index 3f9a61e..9dc7b1d 100644 --- a/src/Alchemy/WebSocketServer.cs +++ b/src/Alchemy/WebSocketServer.cs @@ -79,22 +79,17 @@ private static void HandleContextCleanupThread() { Thread.Sleep(100); - List currentConnections = new List(); - - lock (CurrentConnections) - { - currentConnections.AddRange(CurrentConnections); - } - - foreach (var connection in currentConnections) + for (int count = CurrentConnections.Count-1; count >= 0; count--) { if (cancellation.IsCancellationRequested) break; - + + var connection = CurrentConnections[count]; + if (!connection.Connected) { lock (CurrentConnections) { - CurrentConnections.Remove(connection); + CurrentConnections.RemoveAt(count); } lock (ContextMapping)