From f20692544648356d4d044b017529a41e218597e6 Mon Sep 17 00:00:00 2001 From: Jamil Date: Tue, 14 Jan 2025 07:06:32 -0800 Subject: [PATCH] refactor(apple): `Adapter.start` doesn't need `async` (#7753) This function is called from `PacketTunnelProvider.startTunnel`, which already uses the `completionHandler` approach for returning to the caller when the tunnel start operation is completed. Thus `async / await` here is redundant and unnecessary. --- .../FirezoneNetworkExtension/Adapter.swift | 2 +- .../PacketTunnelProvider.swift | 46 +++++++++---------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/swift/apple/FirezoneNetworkExtension/Adapter.swift b/swift/apple/FirezoneNetworkExtension/Adapter.swift index e6575ca2c..ef5a01403 100644 --- a/swift/apple/FirezoneNetworkExtension/Adapter.swift +++ b/swift/apple/FirezoneNetworkExtension/Adapter.swift @@ -127,7 +127,7 @@ class Adapter { } /// Start the tunnel. - public func start() async throws { + public func start() throws { Log.log("Adapter.start") guard case .tunnelStopped = self.state else { throw AdapterError.invalidState(self.state) diff --git a/swift/apple/FirezoneNetworkExtension/PacketTunnelProvider.swift b/swift/apple/FirezoneNetworkExtension/PacketTunnelProvider.swift index 662f23040..464234758 100644 --- a/swift/apple/FirezoneNetworkExtension/PacketTunnelProvider.swift +++ b/swift/apple/FirezoneNetworkExtension/PacketTunnelProvider.swift @@ -38,13 +38,11 @@ class PacketTunnelProvider: NEPacketTunnelProvider { super.startTunnel(options: options, completionHandler: completionHandler) Log.log("\(#function)") - Task { + do { // If we don't have a token, we can't continue. guard let token = loadAndSaveToken(from: options) else { - completionHandler(PacketTunnelProviderError.tokenNotFoundInKeychain) - - return + throw PacketTunnelProviderError.tokenNotFoundInKeychain } // Try to save the token back to the Keychain but continue if we can't @@ -57,9 +55,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider { // Now we should have a token, so continue connecting guard let apiURL = protocolConfiguration.serverAddress else { - completionHandler( - PacketTunnelProviderError.savedProtocolConfigurationIsInvalid("serverAddress")) - return + throw PacketTunnelProviderError + .savedProtocolConfigurationIsInvalid("serverAddress") } // Reconfigure our Telemetry environment now that we know the API URL @@ -70,10 +67,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider { .providerConfiguration as? [String: String], let logFilter = providerConfiguration[VPNProfileManagerKeys.logFilter] else { - completionHandler( - PacketTunnelProviderError.savedProtocolConfigurationIsInvalid( - "providerConfiguration.logFilter")) - return + throw PacketTunnelProviderError + .savedProtocolConfigurationIsInvalid("providerConfiguration.logFilter") } // Hydrate telemetry account slug @@ -83,12 +78,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider { // connected. The system will try to restart us with a fresh config // once the user fixes the problem, but we'd rather not connect // without a slug. - completionHandler( - PacketTunnelProviderError.savedProtocolConfigurationIsInvalid( - "providerConfiguration.accountSlug" - ) - ) - return + throw PacketTunnelProviderError + .savedProtocolConfigurationIsInvalid("providerConfiguration.accountSlug") } Telemetry.accountSlug = accountSlug @@ -107,19 +98,24 @@ class PacketTunnelProvider: NEPacketTunnelProvider { internetResourceEnabled: internetResourceEnabled, packetTunnelProvider: self ) + + try adapter.start() + self.adapter = adapter - do { try await adapter.start() } - catch { - Log.error(error) - completionHandler(error) - - return - } - // Tell the system the tunnel is up, moving the tunnel manager status to // `connected`. completionHandler(nil) + + } catch let error as PacketTunnelProviderError { + + // These are expected, no need to log them + completionHandler(error) + + } catch { + + Log.error(error) + completionHandler(error) } }