Skip to content

Commit

Permalink
Fix crash when stopping Connect before connection is established
Browse files Browse the repository at this point in the history
  • Loading branch information
tagavari committed Aug 30, 2022
1 parent 0c52e08 commit 4669b97
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions AirMessage/Connection/Connect/DataProxyConnect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,23 @@ class DataProxyConnect: DataProxy {

WebSocket.connect(to: components.url!, headers: headers, configuration: webSocketConfiguration, on: eventLoopGroup, onUpgrade: { [weak self] webSocket in
//Report open event
self?.processingQueue.async { [weak self] in
guard let self = self else { return }
let webSocketOK = self?.processingQueue.sync { [weak self] () -> Bool in
guard let self = self else { return false }

//Ignore if the connection has been cancelled in the meantime
guard self.isActive else { return false }

self.webSocket = webSocket
self.onWSConnect()

return true
}

//If we've decided we don't want this connection anymore, disconnect
//it and discard it silently
guard let webSocketOK = webSocketOK, webSocketOK else {
_ = webSocket.close()
return
}

//Set the listeners
Expand Down Expand Up @@ -149,16 +162,18 @@ class DataProxyConnect: DataProxy {
guard isActive else { return }

processingQueue.sync {
let socket = webSocket!

//Clear connected clients
connectionsLock.withWriteLock {
connectionsMap.removeAll()
}
NotificationNames.postUpdateConnectionCount(0)

//Disconnect
_ = socket.close()
//Socket can be nil between calls to startServer()
//and WebSocket.connect's result handler
if let socket = webSocket {
_ = socket.close()
}

delegate?.dataProxy(self, didStopWithState: .stopped, isRecoverable: false)
}

Expand Down

0 comments on commit 4669b97

Please sign in to comment.