Skip to content

Commit

Permalink
[AltServer] Fixes potential race condition crash when managing connec…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
rileytestut committed Sep 14, 2022
1 parent 4282474 commit e80a49e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Shared/Connections/ConnectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ConnectionManager<RequestHandlerType: RequestHandler>
public var isStarted = false

private var connections = [Connection]()
private let connectionsLock = NSLock()

public init(requestHandler: RequestHandlerType, connectionHandlers: [ConnectionHandler])
{
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}
Expand Down

0 comments on commit e80a49e

Please sign in to comment.