diff --git a/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift b/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift index abe0f98..b47e1aa 100644 --- a/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift +++ b/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift @@ -403,6 +403,12 @@ extension TunnelKitProvider: GenericSocketDelegate { if reasserting { log.debug("Disconnection is recoverable, tunnel will reconnect in \(reconnectionDelay) milliseconds...") tunnelQueue.schedule(after: .milliseconds(reconnectionDelay)) { + + // give up if reasserting cleared in the meantime + guard self.reasserting else { + return + } + self.connectTunnel(upgradedSocket: upgradedSocket, preferredAddress: socket.remoteAddress) } return @@ -456,9 +462,7 @@ extension TunnelKitProvider: SessionProxyDelegate { public func sessionDidStop(_: SessionProxy, shouldReconnect: Bool) { log.info("Session did stop") - if shouldReconnect { - reasserting = true - } + reasserting = shouldReconnect socket?.shutdown() } diff --git a/TunnelKit/Sources/Core/SessionProxy.swift b/TunnelKit/Sources/Core/SessionProxy.swift index 7df6ef2..7366381 100644 --- a/TunnelKit/Sources/Core/SessionProxy.swift +++ b/TunnelKit/Sources/Core/SessionProxy.swift @@ -979,7 +979,7 @@ public class SessionProxy { if let error = error { self?.queue.sync { log.error("Failed LINK write during control flush: \(error)") - self?.deferStop(.reconnect, SessionError.failedLinkWrite) + self?.deferStop(.shutdown, SessionError.failedLinkWrite) return } } @@ -1097,7 +1097,7 @@ public class SessionProxy { if let error = error { self?.queue.sync { log.error("Data: Failed LINK write during send data: \(error)") - self?.deferStop(.reconnect, SessionError.failedLinkWrite) + self?.deferStop(.shutdown, SessionError.failedLinkWrite) return } } @@ -1143,7 +1143,7 @@ public class SessionProxy { if let error = error { self?.queue.sync { log.error("Failed LINK write during send ack for packetId \(controlPacket.packetId): \(error)") - self?.deferStop(.reconnect, SessionError.failedLinkWrite) + self?.deferStop(.shutdown, SessionError.failedLinkWrite) return } }