Skip to content

Commit ef68b4e

Browse files
author
devc0der
committed
fix: prevent app hang on launch by adding fallback in getExtensionStatus
- Add fallback to .disconnected status when no manager is found - Add error handling to return .disconnected on loadAllFromPreferences() failure - Prevents UI hang when getExtensionStatus() is called during app initialization - Ensures completion handler is always called, even on error - Improves app startup reliability
1 parent ce25926 commit ef68b4e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

NetbirdKit/NetworkExtensionAdapter.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,19 +564,32 @@ public class NetworkExtensionAdapter: ObservableObject {
564564
// This is especially important when pollExtensionStateUntilConnected() calls
565565
// checkExtensionState() multiple times per second, which would otherwise
566566
// spawn multiple concurrent Task instances calling loadAllFromPreferences()
567+
// Note: Task is created outside pollingQueue to avoid blocking the queue with async work
567568
pollingQueue.async { [weak self] in
568569
guard let self = self else { return }
569570

571+
// Create Task outside pollingQueue to avoid blocking the serial queue
572+
// The async work (loadAllFromPreferences) will run concurrently, but
573+
// we ensure only one call to getExtensionStatus is processed at a time
570574
Task {
571575
do {
572576
let managers = try await NETunnelProviderManager.loadAllFromPreferences()
573577
if let manager = managers.first(where: { $0.localizedDescription == self.extensionName }) {
574578
DispatchQueue.main.async {
575579
completion(manager.connection.status)
576580
}
581+
} else {
582+
// No manager found, return disconnected status
583+
DispatchQueue.main.async {
584+
completion(.disconnected)
585+
}
577586
}
578587
} catch {
579588
print("Error loading from preferences: \(error)")
589+
// Return disconnected status on error to prevent UI hang
590+
DispatchQueue.main.async {
591+
completion(.disconnected)
592+
}
580593
}
581594
}
582595
}

0 commit comments

Comments
 (0)