Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/rmcp/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ use crate::service::{RxJsonRpcMessage, ServiceRole, TxJsonRpcMessage};

pub mod sink_stream;

/// How long a transport's `close` waits for the rest of a message that a dropped `send` left
/// buffered (best effort: a peer that is not reading within this time still gets a partial
/// message). It also delays closing the output, e.g. a child process's stdin, by up to this long
/// when bytes are left.
pub(crate) const CLOSE_FLUSH_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(500);

/// Flushes `sink` before the transport closes, bounded by [`CLOSE_FLUSH_TIMEOUT`]. A send whose
/// future was dropped during its flush (the service loop abandons the progress write of a request
/// the peer cancelled) may leave the message buffered in the sink; this finishes it, so that the
/// peer does not get a truncated or missing message. A write error or the timeout is ignored. The
/// bound covers the flush only: the caller holds the sink, so a send future that is still alive and
/// holds the sink's lock delays `close` until it ends or is dropped.
pub(crate) async fn flush_before_close<T, S>(sink: &mut S)
where
S: futures::Sink<T> + Unpin,
{
use futures::SinkExt;
let _ = tokio::time::timeout(CLOSE_FLUSH_TIMEOUT, SinkExt::<T>::flush(sink)).await;
}

#[cfg(feature = "transport-async-rw")]
pub mod async_rw;

Expand Down
7 changes: 1 addition & 6 deletions crates/rmcp/src/transport/async_rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ where

pub type TransportWriter<Role, W> = FramedWrite<W, JsonRpcMessageCodec<TxJsonRpcMessage<Role>>>;

/// How long [`AsyncRwTransport`]'s `close` waits for the rest of an abandoned frame to be written
/// (best effort: a peer that is not reading within this time still gets a partial line). It also
/// delays closing the output, e.g. a child process's stdin, by up to this long when bytes are left.
const CLOSE_FLUSH_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(500);

/// Default cap on the size of a single incoming line (one JSON-RPC message) for
/// [`AsyncRwTransport`].
///
Expand Down Expand Up @@ -801,7 +796,7 @@ where
// stopped reading cannot hold the shutdown, and a write error or the timeout still leaves
// the line partial.
if let Some(mut framed) = write.take() {
let _ = tokio::time::timeout(CLOSE_FLUSH_TIMEOUT, framed.flush()).await;
super::flush_before_close::<TxJsonRpcMessage<Role>, _>(&mut framed).await;
}
Ok(())
}
Expand Down
9 changes: 9 additions & 0 deletions crates/rmcp/src/transport/sink_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ where
}

async fn close(&mut self) -> Result<(), Self::Error> {
// `send` is `SinkExt::send`, which leaves its message buffered in the sink if its future
// is dropped before the flush completes (the service loop abandons the progress write of
// a request the peer cancelled). Flush it before closing, so that the peer does not get a
// truncated or missing message. Best effort: once the lock is held, the flush is bounded
// by CLOSE_FLUSH_TIMEOUT, as in `AsyncRwTransport::close` (a send future that is still
// alive holds the lock until it ends or is dropped). The sink itself is not closed, as
// before.
let mut write = self.sink.lock().await;
super::flush_before_close::<TxJsonRpcMessage<Role>, _>(&mut *write).await;
Ok(())
}
}
Expand Down
Loading