Skip to content

Commit 1ca8e49

Browse files
authored
fix: abort keep-alive task when transport is removed (#82)
1 parent 5f9a966 commit 1ca8e49

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

crates/rust-mcp-sdk/src/mcp_runtimes/server_runtime.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,10 @@ impl ServerRuntime {
392392
let transport = self.transport_by_stream(stream_id).await?;
393393

394394
let (disconnect_tx, mut disconnect_rx) = oneshot::channel::<()>();
395-
let _ = transport.keep_alive(ping_interval, disconnect_tx).await;
395+
let abort_alive_task = transport
396+
.keep_alive(ping_interval, disconnect_tx)
397+
.await?
398+
.abort_handle();
396399

397400
// in case there is a payload, we consume it by transport to get processed
398401
if let Some(payload) = payload {
@@ -429,11 +432,13 @@ impl ServerRuntime {
429432
}
430433
// close the stream after all messages are sent, unless it is a standalone stream
431434
if !stream_id.eq(DEFAULT_STREAM_ID){
435+
abort_alive_task.abort();
432436
return Ok(());
433437
}
434438
}
435439
_ = &mut disconnect_rx => {
436440
self.remove_transport(stream_id).await?;
441+
abort_alive_task.abort();
437442
// Disconnection detected by keep-alive task
438443
return Err(SdkError::connection_closed().into());
439444

0 commit comments

Comments
 (0)