Skip to content
Merged
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
60 changes: 57 additions & 3 deletions pkgs/id/src/world_replication_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<F, T>(write: F, timeout: std::time::Duration) -> Result<T>
where
F: std::future::Future<Output = Result<T>>,
{
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 {
Expand All @@ -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::<Result<()>>(),
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 =
Expand Down