Skip to content

Commit 2d4b290

Browse files
authored
Don't fail on cancellation (#1415)
Motivation: Cancellation should not throw if the RPC has already been cancelled. Modifications: - Don't fail a cancellation promise if the call has not yet been started or has already been cancelled. Result: Cancellation does not fail.
1 parent 0981d4d commit 2d4b290

File tree

3 files changed

+4
-6
lines changed

3 files changed

+4
-6
lines changed

Sources/GRPC/ClientCalls/Call.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,8 @@ extension Call {
308308

309309
switch self._state {
310310
case .idle:
311-
// This is weird: does it make sense to cancel before invoking it?
312-
let error = GRPCError.InvalidState("Call must be invoked before cancelling it")
313-
promise?.fail(error)
314-
self.channelPromise?.fail(error)
311+
promise?.succeed(())
312+
self.channelPromise?.fail(GRPCStatus(code: .cancelled))
315313

316314
case let .invoked(transport):
317315
transport.cancel(promise: promise)

Sources/GRPC/Interceptor/ClientTransport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ extension ClientTransport {
249249
self.channelPromise?.fail(error)
250250
promise?.succeed(())
251251
} else {
252-
promise?.fail(GRPCError.AlreadyComplete())
252+
promise?.succeed(())
253253
}
254254
}
255255
}

Tests/GRPCTests/ClientCallTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class ClientCallTests: GRPCTestCase {
183183

184184
func testCancelBeforeInvoke() throws {
185185
let get = self.get()
186-
assertThat(try get.cancel().wait(), .throws())
186+
XCTAssertNoThrow(try get.cancel().wait())
187187
}
188188

189189
func testCancelMidRPC() throws {

0 commit comments

Comments
 (0)