From 0c66359397aed6b516d58ffe00f94d3b2e62ac44 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 20:29:53 -0600 Subject: [PATCH] fix(id): bound replication frame writes with a 30s timeout (#4726) write_envelope awaited write_frame with no deadline, so a peer that stopped reading pinned the stream, its world lease, and a hub session permit indefinitely. Frame writes now run under FRAME_WRITE_TIMEOUT (30s, matching the existing request-read bound) via write_frame_bounded; on expiry the stream errors with a distinct timed-out message and tears down, releasing the lease/permit. Ordinary write failures propagate unlabelled. Regression tests: a stalled (never-completing) write is cut off by the timeout with the distinguishable error; a prompt write and an ordinary write failure pass through untouched. --- pkgs/id/src/world_replication_net.rs | 60 ++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/pkgs/id/src/world_replication_net.rs b/pkgs/id/src/world_replication_net.rs index d69e1ab80a..e33de5b647 100644 --- a/pkgs/id/src/world_replication_net.rs +++ b/pkgs/id/src/world_replication_net.rs @@ -32,6 +32,12 @@ pub const REPLICATION_ALPN: &[u8] = b"/id-world-replication/1"; /// Bound long-lived replication streams one peer can hold on a connection. const MAX_STREAMS_PER_CONNECTION: usize = 8; +/// Bound for a single replication frame write (#4726), matching the 30 s +/// bound the request read already has. A peer that stops reading cannot +/// be allowed to pin the stream — and with it the world lease and the +/// hub session permit the stream holds — forever. +const FRAME_WRITE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30); + /// One request per QUIC stream. `world` defaults to the hub's default world. #[derive(Clone, Debug, Deserialize)] pub struct ReplicationRequest { @@ -302,12 +308,27 @@ async fn write_envelope( let signed = SignedReplicationEnvelope::sign(envelope, signing_key) .context("replication signing key does not match source")?; let bytes = signed.encode_frame().context("encode replication frame")?; - write_frame(send, &bytes) - .await - .context("write replication frame")?; + write_frame_bounded(write_frame(send, &bytes), FRAME_WRITE_TIMEOUT).await?; Ok(frame_digest(&bytes)) } +/// Run one frame write under a deadline (#4726). On expiry the returned +/// error says the write *timed out* — distinguishable from an ordinary +/// write failure, which propagates unchanged — and the caller tears the +/// stream down, releasing the world lease and hub session permit it held. +async fn write_frame_bounded(write: F, timeout: std::time::Duration) -> Result +where + F: std::future::Future>, +{ + match tokio::time::timeout(timeout, write).await { + Ok(result) => result, + Err(_elapsed) => bail!( + "replication frame write timed out after {}s (peer not reading)", + timeout.as_secs() + ), + } +} + #[cfg(test)] #[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod tests { @@ -319,6 +340,39 @@ mod tests { assert!(ensure_nonce(&[1; 16]).is_ok()); } + #[tokio::test] + async fn a_stalled_frame_write_is_cut_off_by_the_timeout() { + // A non-reading peer: the write never completes, so only the + // deadline can end it (#4726 regression test). + let stalled = write_frame_bounded( + std::future::pending::>(), + std::time::Duration::from_millis(10), + ) + .await; + let error = stalled.expect_err("a stalled write must be cut off"); + assert!( + error.to_string().contains("timed out"), + "timeout error must be distinguishable, got: {error}" + ); + } + + #[tokio::test] + async fn a_prompt_frame_write_passes_through_untouched() { + let written = write_frame_bounded(async { Ok(7_u32) }, std::time::Duration::from_secs(30)) + .await + .unwrap(); + assert_eq!(written, 7); + // An ordinary write failure is NOT re-labelled as a timeout. + let failed = write_frame_bounded::<_, ()>( + async { anyhow::bail!("stream reset by peer") }, + std::time::Duration::from_secs(30), + ) + .await + .expect_err("write failures propagate"); + assert!(failed.to_string().contains("stream reset by peer")); + assert!(!failed.to_string().contains("timed out")); + } + #[test] fn request_defaults_to_the_hub_world() { let request: ReplicationRequest =