Skip to content

Commit 16f5009

Browse files
committed
Cancellation exception is not properly handled #17
1 parent dae5c6f commit 16f5009

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

acp/src/commonMain/kotlin/com/agentclientprotocol/client/Client.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,18 @@ public class Client(
193193

194194
private suspend fun createSession(sessionId: SessionId, sessionParameters: SessionCreationParameters, sessionResponse: AcpCreatedSessionResponse, factory: ClientOperationsFactory): ClientSession {
195195
val sessionDeferred = CompletableDeferred<ClientSessionImpl>()
196-
try {
196+
return runCatching {
197197
_sessions.update { it.put(sessionId, sessionDeferred) }
198198

199199
val operations = factory.createClientOperations(sessionId, sessionResponse)
200200

201201
val session = ClientSessionImpl(this, sessionId, sessionParameters, operations, sessionResponse, protocol)
202202
sessionDeferred.complete(session)
203-
return session
204-
}
205-
catch (e: Exception) {
206-
sessionDeferred.completeExceptionally(IllegalStateException("Failed to create session $sessionId", e))
203+
session
204+
}.getOrElse {
205+
sessionDeferred.completeExceptionally(IllegalStateException("Failed to create session $sessionId", it))
207206
_sessions.update { it.remove(sessionId) }
208-
throw e
207+
throw it
209208
}
210209
}
211210

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +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
1011
import io.github.oshai.kotlinlogging.KotlinLogging
1112
import kotlinx.atomicfu.*
1213
import kotlinx.collections.immutable.PersistentMap
@@ -163,17 +164,14 @@ public class Protocol(
163164

164165
// Start processing incoming messages
165166
scope.launch(CoroutineName("${Protocol::class.simpleName!!}.read-messages")) {
166-
try {
167+
catching {
167168
for (message in transport.asMessageChannel()) {
168-
try {
169-
handleIncomingMessage(message)
170-
} catch (e: Exception) {
171-
logger.error(e) { "Error processing incoming message: $message" }
169+
catching { handleIncomingMessage(message) }.onFailure { logger.error(it) {
170+
"Error processing incoming message: $message" }
172171
}
173172
}
174-
}
175-
catch (e: Exception) {
176-
logger.error(e) { "Error processing incoming messages" }
173+
}.onFailure {
174+
logger.error(it) { "Error processing incoming messages" }
177175
}
178176
}
179177
transport.start()
@@ -348,7 +346,7 @@ public class Protocol(
348346
}
349347

350348
private suspend fun handleIncomingMessage(message: JsonRpcMessage) {
351-
try {
349+
runCatching {
352350
when (message) {
353351
is JsonRpcNotification -> {
354352
handleNotification(message)
@@ -367,8 +365,8 @@ public class Protocol(
367365
handleResponse(message)
368366
}
369367
}
370-
} catch (e: Exception) {
371-
logger.error(e) { "Failed to parse message: $message" }
368+
}.onFailure {
369+
logger.error(it) { "Failed to parse message: $message" }
372370
}
373371
}
374372

@@ -427,10 +425,10 @@ public class Protocol(
427425
private suspend fun handleNotification(notification: JsonRpcNotification) {
428426
val handler = notificationHandlers.value[notification.method]
429427
if (handler != null) {
430-
try {
428+
runCatching {
431429
handler(notification)
432-
} catch (e: Exception) {
433-
logger.error(e) { "Error handling notification ${notification.method}" }
430+
}.onFailure {
431+
logger.error(it) { "Error handling notification ${notification.method}" }
434432
}
435433
} else {
436434
logger.debug { "No handler for notification: ${notification.method}" }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.agentclientprotocol.util
2+
3+
import kotlin.coroutines.cancellation.CancellationException
4+
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+
}

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
private val buildNumber: String? = System.getenv("GITHUB_RUN_NUMBER")
88
private val isReleasePublication = System.getenv("RELEASE_PUBLICATION")?.toBoolean() ?: false
99

10-
private val baseVersion = "0.6.0"
10+
private val baseVersion = "0.7.0"
1111

1212
allprojects {
1313
group = "com.agentclientprotocol"

0 commit comments

Comments
 (0)