From deacf613553334f35053a2092d4e062e4775544c Mon Sep 17 00:00:00 2001 From: Sam Symons Date: Thu, 21 Nov 2024 18:38:07 -0800 Subject: [PATCH] Add a 2 second delay before shutting down the tunnel when encountering an error (#1078) Required: Task/Issue URL: https://app.asana.com/0/1207603085593419/1208772807738114/f iOS PR: duckduckgo/iOS#3579 macOS PR: duckduckgo/macos-browser#3557 What kind of version bump will this require?: Patch Description: This PR adds a short delay when shutting down the tunnel. --- Sources/NetworkProtection/PacketTunnelProvider.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/NetworkProtection/PacketTunnelProvider.swift b/Sources/NetworkProtection/PacketTunnelProvider.swift index 0c50c8384..a197c8a9a 100644 --- a/Sources/NetworkProtection/PacketTunnelProvider.swift +++ b/Sources/NetworkProtection/PacketTunnelProvider.swift @@ -691,7 +691,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { // expired. In either case it should be enough to record the manual failures // for these prerequisited to avoid flooding our metrics. providerEvents.fire(.tunnelStartOnDemandWithoutAccessToken) - try await Task.sleep(interval: .seconds(15)) + try? await Task.sleep(interval: .seconds(15)) } else { // If the VPN was started manually without the basic prerequisites we always // want to know as this should not be possible. @@ -704,9 +704,13 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { // Check that the error is valid and able to be re-thrown to the OS before shutting the tunnel down if let wrappedError = wrapped(error: error) { + // Wait for the provider to complete its pixel request. providerEvents.fire(.malformedErrorDetected(error)) + try? await Task.sleep(interval: .seconds(2)) throw wrappedError } else { + // Wait for the provider to complete its pixel request. + try? await Task.sleep(interval: .seconds(2)) throw error } } @@ -725,7 +729,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { // We add a delay when the VPN is started by // on-demand and there's an error, to avoid frenetic ON/OFF // cycling. - try await Task.sleep(interval: .seconds(15)) + try? await Task.sleep(interval: .seconds(15)) } let errorDescription = (error as? LocalizedError)?.localizedDescription ?? String(describing: error) @@ -738,9 +742,13 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { // Check that the error is valid and able to be re-thrown to the OS before shutting the tunnel down if let wrappedError = wrapped(error: error) { + // Wait for the provider to complete its pixel request. providerEvents.fire(.malformedErrorDetected(error)) + try? await Task.sleep(interval: .seconds(2)) throw wrappedError } else { + // Wait for the provider to complete its pixel request. + try? await Task.sleep(interval: .seconds(2)) throw error } }