Skip to content

Commit 7669fd4

Browse files
committed
Fix a potential crash
1 parent 9ff8a1c commit 7669fd4

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Tool/Sources/XPCShared/XPCService.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class XPCService {
1717
let interface: NSXPCInterface
1818
let logger: Logger
1919
weak var delegate: XPCServiceDelegate?
20-
20+
2121
@XPCServiceActor
2222
private var isInvalidated = false
2323

@@ -101,6 +101,12 @@ private class InvalidatingConnection {
101101

102102
struct NoDataError: Error {}
103103

104+
struct EmptyResponseError: Error, LocalizedError {
105+
var errorDescription: String? {
106+
"The server is not returning a response. The app may be installed in the wrong directory."
107+
}
108+
}
109+
104110
struct AutoFinishContinuation<T> {
105111
var continuation: AsyncThrowingStream<T, Error>.Continuation
106112

@@ -129,7 +135,10 @@ func withXPCServiceConnected<T, P>(
129135
} as! P
130136
fn(service, .init(continuation: continuation))
131137
}
132-
return try await stream.first(where: { _ in true })!
138+
guard let result = try await stream.first(where: { _ in true }) else {
139+
throw EmptyResponseError()
140+
}
141+
return result
133142
}
134143

135144
@XPCServiceActor
@@ -144,9 +153,12 @@ public func testXPCListenerEndpoint(_ endpoint: NSXPCListenerEndpoint) async ->
144153
continuation.finish()
145154
}
146155
do {
147-
try await stream.first(where: { _ in true })!
156+
guard let result = try await stream.first(where: { _ in true }) else {
157+
throw EmptyResponseError()
158+
}
148159
return true
149160
} catch {
150161
return false
151162
}
152163
}
164+

0 commit comments

Comments
 (0)