Skip to content

Commit 79318b6

Browse files
authored
Improve catch and cancellation code in some places (#20)
1 parent d0b7989 commit 79318b6

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

acp/api/acp.api

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,3 @@ public final class com/agentclientprotocol/transport/TransportKt {
330330
public static final fun asMessageChannel (Lcom/agentclientprotocol/transport/Transport;)Lkotlinx/coroutines/channels/Channel;
331331
}
332332

333-
public final class com/agentclientprotocol/util/CoroutinesUtilKt {
334-
public static final fun catching (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
335-
}
336-

acp/src/commonMain/kotlin/com/agentclientprotocol/protocol/Protocol.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.agentclientprotocol.model.CancelRequestNotification
77
import com.agentclientprotocol.rpc.*
88
import com.agentclientprotocol.transport.Transport
99
import com.agentclientprotocol.transport.asMessageChannel
10-
import com.agentclientprotocol.util.catching
10+
import com.agentclientprotocol.util.checkCancelled
1111
import io.github.oshai.kotlinlogging.KotlinLogging
1212
import kotlinx.atomicfu.*
1313
import kotlinx.collections.immutable.PersistentMap
@@ -164,13 +164,11 @@ public class Protocol(
164164

165165
// Start processing incoming messages
166166
scope.launch(CoroutineName("${Protocol::class.simpleName!!}.read-messages")) {
167-
catching {
167+
runCatching {
168168
for (message in transport.asMessageChannel()) {
169-
catching { handleIncomingMessage(message) }.onFailure { logger.error(it) {
170-
"Error processing incoming message: $message" }
171-
}
169+
handleIncomingMessage(message)
172170
}
173-
}.onFailure {
171+
}.checkCancelled().onFailure {
174172
logger.error(it) { "Error processing incoming messages" }
175173
}
176174
}
@@ -365,8 +363,8 @@ public class Protocol(
365363
handleResponse(message)
366364
}
367365
}
368-
}.onFailure {
369-
logger.error(it) { "Failed to parse message: $message" }
366+
}.checkCancelled().onFailure {
367+
logger.error(it) { "Exception while processing incoming message: $message" }
370368
}
371369
}
372370

@@ -427,8 +425,12 @@ public class Protocol(
427425
if (handler != null) {
428426
runCatching {
429427
handler(notification)
430-
}.onFailure {
431-
logger.error(it) { "Error handling notification ${notification.method}" }
428+
}.onFailure { t ->
429+
if (t is CancellationException) {
430+
logger.trace(t) { "Notification handler for '${notification.method}' cancelled" }
431+
} else {
432+
logger.error(t) { "Error handling notification ${notification.method}" }
433+
}
432434
}
433435
} else {
434436
logger.debug { "No handler for notification: ${notification.method}" }

acp/src/commonMain/kotlin/com/agentclientprotocol/transport/StdioTransport.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.agentclientprotocol.rpc.ACPJson
44
import com.agentclientprotocol.rpc.JsonRpcMessage
55
import com.agentclientprotocol.rpc.decodeJsonRpcMessage
66
import com.agentclientprotocol.transport.Transport.State
7+
import com.agentclientprotocol.util.checkCancelled
78
import io.github.oshai.kotlinlogging.KotlinLogging
89
import kotlinx.coroutines.*
910
import kotlinx.coroutines.channels.Channel

acp/src/commonMain/kotlin/com/agentclientprotocol/util/CoroutinesUtil.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package com.agentclientprotocol.util
22

33
import kotlin.coroutines.cancellation.CancellationException
44

5-
/** Use instead of [runCatching] for KT-55480 fix. */
6-
public suspend fun <R> catching(body: suspend () -> R): Result<R> =
7-
try {
8-
Result.success(body())
9-
} catch (c: CancellationException) {
10-
throw c
11-
} catch (t: Throwable) {
12-
Result.failure(t)
13-
}
5+
/**
6+
* Rethrows [CancellationException] if the [Result] execution was cancelled. Use on [runCatching] to avoid swallowing of [CancellationException]
7+
*/
8+
internal fun <T> Result<T>.checkCancelled(): Result<T> {
9+
val throwable = exceptionOrNull()
10+
if (throwable is CancellationException) throw throwable
11+
return this
12+
}

0 commit comments

Comments
 (0)