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

Improves handling of VPN uninstall errors #3654

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Improves handling of VPN uninstall errors
  • Loading branch information
diegoreymendez committed Dec 9, 2024
commit 49360b08fe2f8e916237b5ad18c92cccf13dee2f
32 changes: 30 additions & 2 deletions LocalPackages/UDSHelper/Sources/UDSHelper/UDSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,33 @@ public actor UDSClient {

public typealias PayloadHandler = (Data) async throws -> Void

enum ConnectionError: Error {
enum ConnectionError: CustomNSError {
case cancelled
case failure(_ error: Error)

/// This means there's either no peer to connect to, or the connection timed out.
///
/// For our local UDS connection either is a clear problem.
///
case timeout(_ error: Error)

var errorCode: Int {
switch self {
case .cancelled: return 1
case .failure: return 2
case .timeout: return 3
}
}

var errorUserInfo: [String: Any] {
switch self {
case .cancelled:
return [:]
case .failure(let error),
.timeout(let error):
return [NSUnderlyingErrorKey: error as NSError]
}
}
}

private var internalConnection: NWConnection?
Expand Down Expand Up @@ -71,7 +95,9 @@ public actor UDSClient {
///
private func connect() async throws -> NWConnection {
let endpoint = NWEndpoint.unix(path: socketFileURL.path)
let parameters = NWParameters.tcp
let options = NWProtocolTCP.Options()
options.connectionTimeout = 5
let parameters = NWParameters(tls: nil, tcp: options)
let connection = NWConnection(to: endpoint, using: parameters)

connection.stateUpdateHandler = { state in
Expand All @@ -91,6 +117,8 @@ public actor UDSClient {
throw ConnectionError.cancelled
case .failed(let error):
throw ConnectionError.failure(error)
case .waiting(let error):
throw ConnectionError.timeout(error)
default:
try await Task.sleep(nanoseconds: 200 * MSEC_PER_SEC)
}
Expand Down
Loading