fix: propagate CancellationException instead of logging as error (#706)

fixes #242

## How Has This Been Tested?
new and existing tests pass

## Breaking Changes
none 

## Types of changes
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

## Checklist
- [x] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [x] My code follows the repository's style guidelines
- [x] New and existing tests pass locally
- [x] I have added appropriate error handling
- [x] I have added or updated documentation as needed
This commit is contained in:
Pavel Gorgulov 2026-04-15 14:59:01 +02:00 • committed by GitHub
commit e7b09d84f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 109 additions and 25 deletions

View file

@ -24,11 +24,12 @@ import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.SerializationException
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlin.time.Duration
@ -171,7 +172,8 @@ public class SseClientTransport(
}
override suspend fun closeResources() {
job?.cancelAndJoin()
withContext(NonCancellable) {
job?.cancel()
try {
if (::session.isInitialized) session.cancel()
if (::scope.isInitialized) scope.cancel()
@ -181,3 +183,4 @@ public class SseClientTransport(
}
}
}
}

View file

@ -21,6 +21,7 @@ import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelAndJoin
@ -31,6 +32,7 @@ import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import kotlinx.io.Buffer
import kotlinx.io.IOException
@ -241,9 +243,11 @@ public class StdioClientTransport @JvmOverloads public constructor(
}
override suspend fun closeResources() {
withContext(NonCancellable) {
scope.stopProcessing("Closed")
scope.coroutineContext[Job]?.join() // Wait for all coroutines to complete
}
}
private fun sendOutboundMessage(message: JSONRPCMessage, sink: Sink, mainScope: CoroutineScope) {
try {
@ -264,6 +268,8 @@ public class StdioClientTransport @JvmOverloads public constructor(
private suspend fun handleJSONRPCMessage(msg: JSONRPCMessage) {
try {
_onMessage.invoke(msg)
} catch (e: CancellationException) {
throw e
} catch (e: Throwable) {
logger.error(e) { "Error processing message." }
runCatching { _onError.invoke(e) }

View file

@ -277,12 +277,15 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
}
try {
handler(notification)
} catch (e: CancellationException) {
throw e
} catch (cause: Throwable) {
logger.error(cause) { "Error handling notification: ${notification.method}" }
onError(cause)
}
}
@Suppress("ThrowsCount")
private suspend fun onRequest(request: JSONRPCRequest) {
logger.trace { "Received request: ${request.method} (id: ${request.id})" }
@ -300,6 +303,8 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
),
),
)
} catch (e: CancellationException) {
throw e
} catch (cause: Throwable) {
logger.error(cause) { "Error sending method not found response" }
onError(cause)

View file

@ -16,6 +16,7 @@ import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlin.concurrent.atomics.AtomicBoolean
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlin.coroutines.cancellation.CancellationException
/** WebSocket subprotocol identifier for MCP connections. */
public const val MCP_SUBPROTOCOL: String = "mcp"
@ -44,6 +45,7 @@ public abstract class WebSocketMcpTransport : AbstractTransport() {
*/
protected abstract suspend fun initializeSession()
@Suppress("ThrowsCount")
override suspend fun start() {
logger.debug { "Starting websocket transport" }
@ -74,6 +76,8 @@ public abstract class WebSocketMcpTransport : AbstractTransport() {
try {
val message = McpJson.decodeFromString<JSONRPCMessage>(message.readText())
_onMessage.invoke(message)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
_onError.invoke(e)
throw e

View file

@ -1,11 +1,14 @@
package io.modelcontextprotocol.kotlin.sdk.shared
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.modelcontextprotocol.kotlin.sdk.types.CustomRequest
import io.modelcontextprotocol.kotlin.sdk.types.EmptyResult
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCNotification
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCRequest
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCResponse
import io.modelcontextprotocol.kotlin.sdk.types.McpJson
@ -24,6 +27,7 @@ import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlin.coroutines.cancellation.CancellationException
import kotlin.test.BeforeTest
import kotlin.test.Test
@ -126,6 +130,36 @@ class ProtocolTest {
inFlight.await()
}
@Test
fun `should propagate CancellationException from notification handler without calling onError`() = runTest {
protocol.connect(transport)
protocol.fallbackNotificationHandler = {
throw CancellationException("test cancellation")
}
shouldThrow<CancellationException> {
transport.deliver(JSONRPCNotification(method = "test/notification"))
}
protocol.errors shouldHaveSize 0
}
@Test
fun `should report non-cancellation exception from notification handler via onError`() = runTest {
protocol.connect(transport)
protocol.fallbackNotificationHandler = {
throw IllegalStateException("handler failed")
}
// Non-CE exceptions are caught and reported, not propagated
transport.deliver(JSONRPCNotification(method = "test/notification"))
protocol.errors shouldHaveSize 1
protocol.errors[0].message shouldBe "handler failed"
}
@Test
fun `should create params object when request params are null`() = runTest {
protocol.connect(transport)
@ -154,6 +188,12 @@ class ProtocolTest {
}
private class TestProtocol : Protocol(null) {
val errors = mutableListOf<Throwable>()
override fun onError(error: Throwable) {
errors.add(error)
}
override fun assertCapabilityForMethod(method: Method) {
// noop
}

View file

@ -84,6 +84,8 @@ public class SseServerTransport(private val endpoint: String, private val sessio
}
call.receiveText()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
call.respondText("Invalid message: ${e.message}", status = HttpStatusCode.BadRequest)
_onError.invoke(e)
@ -92,6 +94,8 @@ public class SseServerTransport(private val endpoint: String, private val sessio
try {
handleMessage(body)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
call.respondText("Error handling message $body: ${e.message}", status = HttpStatusCode.BadRequest)
return
@ -108,6 +112,8 @@ public class SseServerTransport(private val endpoint: String, private val sessio
try {
val parsedMessage = McpJson.decodeFromString<JSONRPCMessage>(message)
_onMessage.invoke(parsedMessage)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
_onError.invoke(e)
throw e

View file

@ -28,15 +28,18 @@ import io.modelcontextprotocol.kotlin.sdk.types.RPCError
import io.modelcontextprotocol.kotlin.sdk.types.RPCError.ErrorCode.REQUEST_TIMEOUT
import io.modelcontextprotocol.kotlin.sdk.types.RequestId
import io.modelcontextprotocol.kotlin.sdk.types.SUPPORTED_PROTOCOL_VERSIONS
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.job
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import kotlin.concurrent.atomics.AtomicBoolean
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlin.coroutines.cancellation.CancellationException
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.uuid.ExperimentalUuidApi
@ -283,6 +286,7 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
}
override suspend fun close() {
withContext(NonCancellable) {
streamMutex.withLock {
streamsMapping.values.forEach {
try {
@ -296,6 +300,7 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
invokeOnCloseCallback()
}
}
}
/**
* Handles an incoming HTTP request, whether GET, POST or DELETE
@ -420,6 +425,8 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
call.coroutineContext.job.invokeOnCompletion { streamsMapping.remove(streamId) }
messages.forEach { message -> _onMessage(message) }
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
call.reject(
HttpStatusCode.BadRequest,
@ -498,6 +505,7 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
val streamId = requestToStreamMapping[requestId] ?: return
val sessionContext = streamsMapping[streamId] ?: return
withContext(NonCancellable) {
try {
sessionContext.session?.close()
} catch (e: Exception) {
@ -506,6 +514,7 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
streamsMapping.remove(streamId)
}
}
}
@Suppress("TooGenericExceptionCaught")
private suspend fun replayEvents(store: EventStore, lastEventId: String, session: ServerSSESession) {
@ -554,6 +563,8 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
id = eventId,
data = McpJson.encodeToString(message),
)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
_onError(IllegalStateException("Failed to replay event: ${e.message}", e))
}
@ -565,6 +576,8 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
streamsMapping.remove(streamId)
throwable?.let { _onError(it) }
}
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
_onError(e)
}
@ -658,6 +671,8 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
private suspend fun flushSse(session: ServerSSESession?) {
try {
session?.send(data = "")
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
_onError(e)
}
@ -709,6 +724,9 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
val eventId = configuration.eventStore?.storeEvent(streamId, message)
try {
session?.send(event = "message", id = eventId, data = McpJson.encodeToString(message))
} catch (e: CancellationException) {
streamsMapping.remove(streamId)
throw e
} catch (_: Exception) {
streamsMapping.remove(streamId)
}
@ -733,6 +751,8 @@ public class StreamableHttpServerTransport(private val configuration: Configurat
retry = configuration.retryInterval?.inWholeMilliseconds,
data = "",
)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
_onError(e)
}