Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid deadlocking the Packet Tunnel after a reboot #7089

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ios/MullvadSettings/MigrationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public struct MigrationManager {
)
} catch .itemNotFound as KeychainError {
migrationCompleted(.nothing)
} catch let couldNotReadKeychainError as KeychainError
where couldNotReadKeychainError == .interactionNotAllowed {
migrationCompleted(.failure(couldNotReadKeychainError))
} catch {
resetStoreHandler(.failure(error))
}
Expand Down
48 changes: 28 additions & 20 deletions ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,36 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}

private func performSettingsMigration() {
migrationManager.migrateSettings(
store: SettingsManager.store,
migrationCompleted: { [unowned self] migrationResult in
switch migrationResult {
case .success:
providerLogger.debug("Successful migration from PacketTunnel")
case .nothing:
providerLogger.debug("Attempted migration from PacketTunnel, but found nothing to do")
case let .failure(error):
// `next` returns an Optional value, but this iterator is guaranteed to always have a next value
guard let delay = migrationFailureIterator.next() else { return }
providerLogger
.error(
"Failed migration from PacketTunnel: \(error), retrying in \(delay.timeInterval) seconds"
)
// Block the launch of the Packet Tunnel for as long as the settings migration fail.
// The process watchdog introduced by iOS 17 will kill this process after 60 seconds.
Thread.sleep(forTimeInterval: delay.timeInterval)
performSettingsMigration()
var hasNotMigrated = true
repeat {
migrationManager.migrateSettings(
store: SettingsManager.store,
migrationCompleted: { [unowned self] migrationResult in
switch migrationResult {
case .success:
providerLogger.debug("Successful migration from PacketTunnel")
hasNotMigrated = false
case .nothing:
hasNotMigrated = false
providerLogger.debug("Attempted migration from PacketTunnel, but found nothing to do")
case let .failure(error):
providerLogger
.error(
"Failed migration from PacketTunnel: \(error)"
)
}
}
)
if hasNotMigrated {
// `next` returns an Optional value, but this iterator is guaranteed to always have a next value
guard let delay = migrationFailureIterator.next() else { continue }

providerLogger.error("Retrying migration in \(delay.timeInterval) seconds")
// Block the launch of the Packet Tunnel for as long as the settings migration fail.
// The process watchdog introduced by iOS 17 will kill this process after 60 seconds.
Thread.sleep(forTimeInterval: delay.timeInterval)
}
)
} while hasNotMigrated
}

private func setUpTransportProvider(
Expand Down
Loading