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
15 changes: 14 additions & 1 deletion crates/rmcp/src/transport/async_rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ 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 @@ -473,7 +478,15 @@ where

async fn close(&mut self) -> Result<(), Self::Error> {
let mut write = self.write.lock().await;
drop(write.take());
// A send whose future was dropped during its flush (the service loop abandons the progress
// write of a request the peer cancelled) leaves the rest of its frame buffered, in the
// codec or in the writer below it. Flush it before closing, so that the peer gets the
// whole line. This is best effort: bounded by CLOSE_FLUSH_TIMEOUT, so that a peer that
// 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;
}
Ok(())
}
}
Expand Down
Loading