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 <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-11-03 07:03:09 +01:00
parent 6405b76941
commit ec943587fa
1 changed files with 7 additions and 5 deletions

View File

@ -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()
}
}
}
}