From ec943587fad92a50bc58b1ae90d436267f117fcc Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 3 Nov 2018 07:03:09 +0100 Subject: [PATCH] TunnelsManager: initialize observer on correct thread Because of the callback recursion, this function needs to always be called from the right thread Signed-off-by: Jason A. Donenfeld --- WireGuard/WireGuard/VPN/TunnelsManager.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift index 2aa415a..f38ca88 100644 --- a/WireGuard/WireGuard/VPN/TunnelsManager.swift +++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift @@ -399,13 +399,14 @@ class TunnelContainer: NSObject { } private func startObservingTunnelStatus() { - if (statusObservationToken != nil) { return } - let connection = tunnelProvider.connection - statusObservationToken = NotificationCenter.default.addObserver( + DispatchQueue.main.async { [weak self] in + guard let s = self else { return } + if (s.statusObservationToken != nil) { return } + let connection = s.tunnelProvider.connection + s.statusObservationToken = NotificationCenter.default.addObserver( forName: .NEVPNStatusDidChange, object: connection, - queue: nil) { [weak self] (_) in - guard let s = self else { return } + queue: nil) { (_) in if ((s.status == .restarting) && (connection.status == .disconnected || connection.status == .disconnecting)) { // Don't change s.status when disconnecting for a restart if (connection.status == .disconnected) { @@ -421,6 +422,7 @@ class TunnelContainer: NSObject { if (s.status == .inactive) { s.stopObservingTunnelStatus() } + } } }