Skip to content

Commit e80a49e

Browse files
committed
[AltServer] Fixes potential race condition crash when managing connections
1 parent 4282474 commit e80a49e

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

Shared/Connections/ConnectionManager.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class ConnectionManager<RequestHandlerType: RequestHandler>
4141
public var isStarted = false
4242

4343
private var connections = [Connection]()
44+
private let connectionsLock = NSLock()
4445

4546
public init(requestHandler: RequestHandlerType, connectionHandlers: [ConnectionHandler])
4647
{
@@ -88,6 +89,9 @@ private extension ConnectionManager
8889
{
8990
func prepare(_ connection: Connection)
9091
{
92+
self.connectionsLock.lock()
93+
defer { self.connectionsLock.unlock() }
94+
9195
guard !self.connections.contains(where: { $0 === connection }) else { return }
9296
self.connections.append(connection)
9397

@@ -96,6 +100,9 @@ private extension ConnectionManager
96100

97101
func disconnect(_ connection: Connection)
98102
{
103+
self.connectionsLock.lock()
104+
defer { self.connectionsLock.unlock() }
105+
99106
guard let index = self.connections.firstIndex(where: { $0 === connection }) else { return }
100107
self.connections.remove(at: index)
101108
}

0 commit comments

Comments
 (0)