From e80a49e705c88c0329457570587b29e0846a909a Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Wed, 14 Sep 2022 13:11:26 -0700 Subject: [PATCH] [AltServer] Fixes potential race condition crash when managing connections --- Shared/Connections/ConnectionManager.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Shared/Connections/ConnectionManager.swift b/Shared/Connections/ConnectionManager.swift index 31dbf810e..7aacc9d2e 100644 --- a/Shared/Connections/ConnectionManager.swift +++ b/Shared/Connections/ConnectionManager.swift @@ -41,6 +41,7 @@ public class ConnectionManager public var isStarted = false private var connections = [Connection]() + private let connectionsLock = NSLock() public init(requestHandler: RequestHandlerType, connectionHandlers: [ConnectionHandler]) { @@ -88,6 +89,9 @@ private extension ConnectionManager { func prepare(_ connection: Connection) { + self.connectionsLock.lock() + defer { self.connectionsLock.unlock() } + guard !self.connections.contains(where: { $0 === connection }) else { return } self.connections.append(connection) @@ -96,6 +100,9 @@ private extension ConnectionManager func disconnect(_ connection: Connection) { + self.connectionsLock.lock() + defer { self.connectionsLock.unlock() } + guard let index = self.connections.firstIndex(where: { $0 === connection }) else { return } self.connections.remove(at: index) }