Skip to content

Commit 3469755

Browse files
committed
Improve communication error handling
1 parent fa1416b commit 3469755

5 files changed

Lines changed: 33 additions & 6 deletions

File tree

CommunicationBridge/ServiceDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ actor EventHandler {
6363
) async {
6464
rescheduleExitTask()
6565
#if DEBUG
66+
if let endpoint, !(await testXPCListenerEndpoint(endpoint)) {
67+
self.endpoint = nil
68+
}
6669
reply(endpoint)
6770
#else
6871
if await launcher.isApplicationValid {

Core/Sources/HostApp/General.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct General: ReducerProtocol {
3030
return .run { send in
3131
await send(.setupLaunchAgentIfNeeded)
3232
}
33-
33+
3434
case .setupLaunchAgentIfNeeded:
3535
return .run { send in
3636
#if DEBUG
@@ -47,7 +47,7 @@ struct General: ReducerProtocol {
4747
#endif
4848
await send(.reloadStatus)
4949
}
50-
50+
5151
case .openExtensionManager:
5252
return .run { send in
5353
let service = try getService()
@@ -76,9 +76,15 @@ struct General: ReducerProtocol {
7676
))
7777
} else {
7878
toast("Launching service app.", .info)
79-
try await Task.sleep(nanoseconds: 3_000_000_000)
79+
try await Task.sleep(nanoseconds: 5_000_000_000)
8080
await send(.reloadStatus)
8181
}
82+
} catch let error as XPCCommunicationBridgeError {
83+
toast(
84+
"Failed to reach communication bridge. \(error.localizedDescription)",
85+
.error
86+
)
87+
await send(.failedReloading)
8288
} catch {
8389
toast(error.localizedDescription, .error)
8490
await send(.failedReloading)

Tool/Sources/XPCShared/XPCCommunicationBridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public enum XPCCommunicationBridgeError: Swift.Error, LocalizedError {
88
public var errorDescription: String? {
99
switch self {
1010
case .failedToCreateXPCConnection:
11-
return "Failed to create XPC connection"
11+
return "Failed to create XPC connection."
1212
case let .xpcServiceError(error):
1313
return "XPC Service error: \(error.localizedDescription)"
1414
}

Tool/Sources/XPCShared/XPCExtensionService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public enum XPCExtensionServiceError: Swift.Error, LocalizedError {
99
public var errorDescription: String? {
1010
switch self {
1111
case .failedToGetServiceEndpoint:
12-
return "Failed to get service endpoint"
12+
return "Waiting for service to connect to the communication bridge."
1313
case .failedToCreateXPCConnection:
14-
return "Failed to create XPC connection"
14+
return "Failed to create XPC connection."
1515
case let .xpcServiceError(error):
1616
return "XPC Service error: \(error.localizedDescription)"
1717
}

Tool/Sources/XPCShared/XPCService.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,21 @@ func withXPCServiceConnected<T, P>(
126126
return try await stream.first(where: { _ in true })!
127127
}
128128

129+
@XPCServiceActor
130+
public func testXPCListenerEndpoint(_ endpoint: NSXPCListenerEndpoint) async -> Bool {
131+
let connection = NSXPCConnection(listenerEndpoint: endpoint)
132+
defer { connection.invalidate() }
133+
let stream: AsyncThrowingStream<Void, Error> = AsyncThrowingStream { continuation in
134+
_ = connection.remoteObjectProxyWithErrorHandler {
135+
continuation.finish(throwing: $0)
136+
}
137+
continuation.yield(())
138+
continuation.finish()
139+
}
140+
do {
141+
try await stream.first(where: { _ in true })!
142+
return true
143+
} catch {
144+
return false
145+
}
146+
}

0 commit comments

Comments
 (0)