From 6946d4633568618bb7866521136d6c05c4070541 Mon Sep 17 00:00:00 2001 From: Andrew Rogers <87336074+rogers-andrew@users.noreply.github.com> Date: Wed, 2 Dec 2026 08:35:43 -0800 Subject: [PATCH] Make the stalled-browser relay test reach a stalled send The test sent one 32 MiB frame, but the relay reads the Gateway under tungstenite's default 16 MiB `max_frame_size`, so it refused the frame before forwarding it and no browser send ever stalled. On Linux the Gateway double's send then failed when the relay dropped the socket, the probe never fired, and the test failed after `RECV_TIMEOUT` at its first wait. Behind that, the browser read as soon as the probe fired, and a read makes room for a stalled send to finish inside `RELAY_IO_DEADLINE`; the 750 ms wait that c2e70f6e removed had been holding that read back. - `send_frames_until_the_relay_lets_go` sends up to `STALL_FRAMES` frames of `STALL_FRAME_BYTES`, each under the frame cap and together more than the sockets between the relay and the browser hold, then reports when the relay drops the Gateway socket, which it does only once it has given up on the browser. - The browser reads nothing until that report, and the test asserts that fewer than all the frames reach it, since frames the sockets took before the stall can arrive whole. - The test is now `a_stalled_browser_send_is_canceled_within_the_relay_deadline`: the relay gives up while the Gateway is still connected. Co-Authored-By: Claude Opus 5.5 --- .../server/tests/it/realtime_relay.rs | 47 ++++++++++++------- .../tests/it/realtime_relay/overload.rs | 33 +++++++++---- 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/crates/workshop/server/tests/it/realtime_relay.rs b/crates/workshop/server/tests/it/realtime_relay.rs index 55f8f663..67090b54 100644 --- a/crates/workshop/server/tests/it/realtime_relay.rs +++ b/crates/workshop/server/tests/it/realtime_relay.rs @@ -9,6 +9,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, MutexGuard}; use axum::Router; +use axum::body::Bytes; use axum::extract::State; use axum::extract::ws::{CloseFrame, Message, WebSocketUpgrade}; use axum::http::{HeaderMap, StatusCode, Uri, header}; @@ -254,35 +255,49 @@ async fn upstream_close(ws: WebSocketUpgrade) -> Response { }) } +/// The size of each frame the stalling Gateway sends. The relay reads the +/// Gateway under tungstenite's default 16 MiB `max_frame_size`, and refuses a +/// larger frame before it forwards anything, so no browser send would stall. +const STALL_FRAME_BYTES: usize = 8 * 1024 * 1024; + +/// The most frames the stalling Gateway sends: more bytes than the socket +/// buffers between the relay and the browser hold, so a browser that never +/// reads stalls one of the relay's sends. +const STALL_FRAMES: usize = 16; + #[derive(Clone, Default)] -struct StalledPeerProbe { - frame_sent: Arc, - frame_sent_event: Arc, +struct RelayReleaseProbe { + released: Arc, + released_event: Arc, } -impl StalledPeerProbe { - async fn wait_for_frame(&self) { - let notified = self.frame_sent_event.notified(); - if self.frame_sent.load(Ordering::Acquire) { +impl RelayReleaseProbe { + async fn wait_for_release(&self) { + let notified = self.released_event.notified(); + if self.released.load(Ordering::Acquire) { return; } notified.await; } } -async fn send_large_frame_then_disconnect( - State(probe): State, +/// Sends frames until the relay stops taking them, then reports when the +/// relay drops the Gateway socket, which it does only once it has given up on +/// the browser. +async fn send_frames_until_the_relay_lets_go( + State(probe): State, ws: WebSocketUpgrade, ) -> Response { ws.on_upgrade(move |mut socket| async move { - if socket - .send(Message::Binary(vec![0x5a; 32 * 1024 * 1024].into())) - .await - .is_ok() - { - probe.frame_sent.store(true, Ordering::Release); - probe.frame_sent_event.notify_one(); + let frame = Bytes::from(vec![0x5a; STALL_FRAME_BYTES]); + for _ in 0..STALL_FRAMES { + if socket.send(Message::Binary(frame.clone())).await.is_err() { + break; + } } + while let Some(Ok(_)) = socket.recv().await {} + probe.released.store(true, Ordering::Release); + probe.released_event.notify_one(); }) } diff --git a/crates/workshop/server/tests/it/realtime_relay/overload.rs b/crates/workshop/server/tests/it/realtime_relay/overload.rs index ce8f9058..1176bea1 100644 --- a/crates/workshop/server/tests/it/realtime_relay/overload.rs +++ b/crates/workshop/server/tests/it/realtime_relay/overload.rs @@ -3,11 +3,11 @@ use super::*; #[tokio::test] -async fn stalled_browser_cleanup_is_bounded_after_gateway_disconnect() { - let probe = StalledPeerProbe::default(); +async fn a_stalled_browser_send_is_canceled_within_the_relay_deadline() { + let probe = RelayReleaseProbe::default(); let gateway = spawn_gateway( Router::new() - .route("/v1/realtime", get(send_large_frame_then_disconnect)) + .route("/v1/realtime", get(send_frames_until_the_relay_lets_go)) .with_state(probe.clone()), ) .await; @@ -16,15 +16,28 @@ async fn stalled_browser_cleanup_is_bounded_after_gateway_disconnect() { tokio_tungstenite::connect_async(server.ws_url("/v1/realtime?intent=transcription")) .await .expect("the Workshop Realtime socket upgrades"); - tokio::time::timeout(RECV_TIMEOUT, probe.wait_for_frame()) + // The browser reads nothing until the relay lets go: a read would make + // room for the stalled send, which could then finish inside the deadline. + tokio::time::timeout(RECV_TIMEOUT, probe.wait_for_release()) .await - .expect("the Gateway fills the relay's browser send"); + .expect("the relay gives up on a browser that never reads"); - let first = tokio::time::timeout(RECV_TIMEOUT, socket.next()) - .await - .expect("bounded relay cleanup releases the stalled browser"); + // The frames the sockets took before the stall can arrive whole, so the + // test counts frames instead of checking the first one. + let mut frames = 0; + loop { + let next = tokio::time::timeout(RECV_TIMEOUT, socket.next()) + .await + .expect("the released browser socket ends"); + match next { + Some(Ok(ClientMessage::Binary(_))) => frames += 1, + Some(Ok(ClientMessage::Close(_)) | Err(_)) | None => break, + Some(Ok(_)) => {} + } + } assert!( - !matches!(first, Some(Ok(ClientMessage::Binary(_)))), - "the stalled send is canceled before peer reads can release it" + frames < STALL_FRAMES, + "the stalled send is canceled before peer reads can release it: \ + {frames} of {STALL_FRAMES} frames reached the browser" ); }