Skip to content

fix: handle CancellationException in WebSocketTransport#663

Open
devcrocod wants to merge 1 commit intomainfrom
devcrocod/websocket-transport-cancellation
Open

fix: handle CancellationException in WebSocketTransport#663
devcrocod wants to merge 1 commit intomainfrom
devcrocod/websocket-transport-cancellation

Conversation

@devcrocod
Copy link
Copy Markdown
Contributor

closes #17

How Has This Been Tested?

integration tests

Breaking Changes

none

Types of changes

  • 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

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Copilot AI review requested due to automatic review settings April 1, 2026 12:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses flaky WebSocket transport shutdown behavior by treating coroutine cancellation as a normal close signal instead of an error, and re-enables the previously ignored WebSocket integration tests with a safety timeout to prevent CI hangs.

Changes:

  • Suppress _onError callbacks when a WebSocket session completes with CancellationException, invoking the close callback instead.
  • Re-enable WebSocketTransportTest cases and migrate them to JUnit Jupiter annotations.
  • Add a class-level JUnit timeout to reduce CI flakiness/hangs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
kotlin-sdk-core/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/shared/WebSocketMcpTransport.kt Treats CancellationException as a normal shutdown path in the session completion handler.
integration-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/WebSocketTransportTest.kt Re-enables WebSocket transport integration tests and adds a JUnit timeout to stabilize CI execution.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@OptIn(InternalCoroutinesApi::class)
session.coroutineContext.job.invokeOnCompletion {
if (it != null) {
if (it != null && it !is CancellationException) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t the CancellationException be also rethrown?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, rethrowing here would be incorrect

@devcrocod devcrocod force-pushed the devcrocod/websocket-transport-cancellation branch from cbc9c57 to c4fa2df Compare April 10, 2026 16:27
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...rotocol/kotlin/sdk/shared/WebSocketMcpTransport.kt 0.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@devcrocod devcrocod requested a review from e5l April 10, 2026 16:44
@devcrocod devcrocod marked this pull request as ready for review April 10, 2026 16:44
Copilot AI review requested due to automatic review settings April 10, 2026 16:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 8 to 11
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
import io.modelcontextprotocol.kotlin.sdk.types.McpJson
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineName
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CancellationException is imported from kotlinx.coroutines here, but other commonMain code in this module uses kotlin.coroutines.cancellation.CancellationException (e.g., AbstractClientTransport). Consider switching to the stdlib import for consistency and to avoid confusion with java.util.concurrent.CancellationException on JVM.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate WebSocketTransportTest.should start then close cleanly failing only with GH action

4 participants