Skip to content

Commit 78fe17d

Browse files
committed
Fix completion handler for client
1 parent c973830 commit 78fe17d

File tree

1 file changed

+14
-5
lines changed
  • krpc/krpc-client/src/commonMain/kotlin/kotlinx/rpc/krpc/client

1 file changed

+14
-5
lines changed

krpc/krpc-client/src/commonMain/kotlin/kotlinx/rpc/krpc/client/KrpcClient.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,20 @@ public abstract class KrpcClient : RpcClient, KrpcEndpoint {
159159

160160
clientCancelled = true
161161

162-
if (!clientCancelledByServer) {
163-
sendCancellation(CancellationType.ENDPOINT, null, null, closeTransportAfterSending = true)
164-
}
162+
try {
163+
if (!clientCancelledByServer) {
164+
sendCancellation(CancellationType.ENDPOINT, null, null, closeTransportAfterSending = true)
165+
}
165166

166-
requestChannels.values.forEach { it.close(CancellationException("Client cancelled")) }
167-
requestChannels.clear()
167+
requestChannels.values.forEach {
168+
it.close(CancellationException("Client cancelled"))
169+
it.cancel()
170+
}
171+
} catch (_ : Exception) {
172+
// ignore, we are already cancelled
173+
} finally {
174+
requestChannels.clear()
175+
}
168176
}
169177

170178
CoroutineScope(context)
@@ -327,6 +335,7 @@ public abstract class KrpcClient : RpcClient, KrpcEndpoint {
327335
throw e
328336
} finally {
329337
channel.close()
338+
channel.cancel()
330339
requestChannels.remove(callId)
331340
connector.unsubscribeFromMessages(call.descriptor.fqName, callId)
332341
}

0 commit comments

Comments
 (0)