From 6b281711c70d672d6bd0fe921e30ff18970cb931 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 9 Aug 2019 18:38:19 +0200 Subject: [PATCH] Ignore errors from outdated link writes Prevents async delegation after cleanup. --- .../Protocols/OpenVPN/OpenVPNSession.swift | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift index 6c32d3e..618e7f0 100644 --- a/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift +++ b/TunnelKit/Sources/Protocols/OpenVPN/OpenVPNSession.swift @@ -387,7 +387,7 @@ public class OpenVPNSession: Session { private func loopLink() { let loopedLink = link loopedLink?.setReadHandler(queue: queue) { [weak self] (newPackets, error) in - guard loopedLink === self?.link else { + guard self?.link === loopedLink else { log.warning("Ignoring read from outdated LINK") return } @@ -1008,7 +1008,12 @@ public class OpenVPNSession: Session { } // WARNING: runs in Network.framework queue + let writeLink = link link?.writePackets(rawList) { [weak self] (error) in + guard self?.link === writeLink else { + log.warning("Ignoring write from outdated LINK") + return + } if let error = error { self?.queue.sync { log.error("Failed LINK write during control flush: \(error)") @@ -1131,7 +1136,12 @@ public class OpenVPNSession: Session { // WARNING: runs in Network.framework queue controlChannel.addSentDataCount(encryptedPackets.flatCount) + let writeLink = link link?.writePackets(encryptedPackets) { [weak self] (error) in + guard self?.link === writeLink else { + log.warning("Ignoring write from outdated LINK") + return + } if let error = error { self?.queue.sync { log.error("Data: Failed LINK write during send data: \(error)") @@ -1172,7 +1182,12 @@ public class OpenVPNSession: Session { } // WARNING: runs in Network.framework queue + let writeLink = link link?.writePacket(raw) { [weak self] (error) in + guard self?.link === writeLink else { + log.warning("Ignoring write from outdated LINK") + return + } if let error = error { self?.queue.sync { log.error("Failed LINK write during send ack for packetId \(controlPacket.packetId): \(error)")