mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
## Why When the shared WebSocket writer stalls, a burst from one stream can fill the outgoing queue. If a handshake reply arrives while the queue is full, the relay can disconnect and interrupt existing streams. ## What changed Allow each stream to queue one data record while the writer flushes the current frame. Keep data and control frames in the same FIFO to preserve their order. If the queue is full when a handshake reply is ready, decline the new stream and keep the connection open so existing streams can drain. ## Testing Add a regression test that stalls the writer during a burst and verifies that a second stream's handshake reply arrives after at most two data records and that the burst drains in order. GitOrigin-RevId: f0e4225bccb63d021c09718e5c43df3f4c4a6745
105 lines
4.0 KiB
Rust
105 lines
4.0 KiB
Rust
//! Exercise heartbeat deadlines while a single physical write is stalled.
|
|
|
|
use std::convert::Infallible;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use pretty_assertions::assert_eq;
|
|
use tokio::sync::Notify;
|
|
use tokio::sync::mpsc;
|
|
use tokio_util::task::AbortOnDropHandle;
|
|
|
|
use super::*;
|
|
|
|
#[tokio::test(start_paused = true)]
|
|
async fn pong_during_write_clears_only_the_pong_deadline() {
|
|
for release_write in [true, false] {
|
|
let (outgoing_tx, outgoing_rx) = mpsc::channel(1);
|
|
let (pong_tx, pong_rx) = mpsc::channel(1);
|
|
let (observed_tx, mut observed_rx) = mpsc::unbounded_channel();
|
|
let release = Arc::new(Notify::new());
|
|
let completed = Arc::new(Notify::new());
|
|
let sink = futures::sink::unfold(
|
|
(observed_tx, Arc::clone(&release), Arc::clone(&completed)),
|
|
|(observed_tx, release, completed), message| async move {
|
|
let is_data = matches!(message, Message::Binary(_));
|
|
observed_tx.send(message).unwrap();
|
|
if is_data {
|
|
release.notified().await;
|
|
completed.notify_one();
|
|
}
|
|
Ok::<_, Infallible>((observed_tx, release, completed))
|
|
},
|
|
);
|
|
let task = AbortOnDropHandle::new(tokio::spawn(run(Box::pin(sink), outgoing_rx, pong_rx)));
|
|
assert!(matches!(observed_rx.recv().await, Some(Message::Ping(_))));
|
|
tokio::time::advance(WEBSOCKET_PONG_TIMEOUT * 3 / 4).await;
|
|
outgoing_tx
|
|
.send(OutgoingFrame::control(vec![42]))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
observed_rx.recv().await,
|
|
Some(Message::Binary(vec![42].into()))
|
|
);
|
|
pong_tx.send(()).await.unwrap();
|
|
|
|
// Let the old pong deadline expire while the write remains blocked.
|
|
tokio::time::advance(WEBSOCKET_PONG_TIMEOUT / 2).await;
|
|
tokio::task::yield_now().await;
|
|
assert!(!task.is_finished());
|
|
if release_write {
|
|
release.notify_one();
|
|
completed.notified().await;
|
|
drop(outgoing_tx);
|
|
drop(pong_tx);
|
|
assert_eq!(
|
|
task.await.unwrap(),
|
|
RendezvousDisconnectReason::LocalShutdown
|
|
);
|
|
} else {
|
|
// Receiving a pong must not extend the independent write budget.
|
|
tokio::time::advance(WEBSOCKET_PONG_TIMEOUT).await;
|
|
assert_eq!(task.await.unwrap(), RendezvousDisconnectReason::WriteError);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::test(start_paused = true)]
|
|
async fn missing_pong_times_out_during_a_blocked_write() {
|
|
let (outgoing_tx, outgoing_rx) = mpsc::channel(1);
|
|
let (_pong_tx, pong_rx) = mpsc::channel(1);
|
|
let (observed_tx, mut observed_rx) = mpsc::unbounded_channel();
|
|
let sink = futures::sink::unfold(observed_tx, |observed_tx, message| async move {
|
|
let is_data = matches!(message, Message::Binary(_));
|
|
observed_tx.send(message).unwrap();
|
|
if is_data {
|
|
std::future::pending::<()>().await;
|
|
}
|
|
Ok::<_, Infallible>(observed_tx)
|
|
});
|
|
let task = AbortOnDropHandle::new(tokio::spawn(run(Box::pin(sink), outgoing_rx, pong_rx)));
|
|
assert!(matches!(observed_rx.recv().await, Some(Message::Ping(_))));
|
|
tokio::time::advance(WEBSOCKET_PONG_TIMEOUT * 3 / 4).await;
|
|
outgoing_tx
|
|
.send(OutgoingFrame::control(vec![42]))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
observed_rx.recv().await,
|
|
Some(Message::Binary(vec![42].into()))
|
|
);
|
|
assert_eq!(task.await.unwrap(), RendezvousDisconnectReason::PongTimeout);
|
|
}
|
|
|
|
#[tokio::test(start_paused = true)]
|
|
async fn queued_pong_wins_over_an_expired_deadline() {
|
|
let (pong_tx, mut pong_rx) = mpsc::channel(1);
|
|
let mut watchdog = WebSocketPongWatchdog::new(Duration::from_secs(1));
|
|
watchdog.ping_sent(Instant::now());
|
|
tokio::time::advance(Duration::from_secs(1)).await;
|
|
pong_tx.send(()).await.unwrap();
|
|
assert_eq!(receive_pong(&mut watchdog, &mut pong_rx).await, Ok(()));
|
|
assert_eq!(watchdog.deadline(), None);
|
|
}
|