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
126 lines
4.5 KiB
Rust
126 lines
4.5 KiB
Rust
//! Keep executor writes bounded while processing pongs independently of write backpressure.
|
|
|
|
use futures::Sink;
|
|
use futures::SinkExt;
|
|
use tokio::sync::mpsc;
|
|
use tokio::time::Instant;
|
|
use tokio_tungstenite::tungstenite::Message;
|
|
use tracing::warn;
|
|
|
|
use crate::connection::WEBSOCKET_KEEPALIVE_INTERVAL;
|
|
use crate::relay::RendezvousDisconnectReason;
|
|
use crate::websocket_pong_watchdog::WEBSOCKET_PONG_TIMEOUT;
|
|
use crate::websocket_pong_watchdog::WebSocketPongWatchdog;
|
|
|
|
/// Keep the stream's slot until its record reaches the socket writer.
|
|
/// Control frames share the FIFO to preserve handshake/data/reset ordering.
|
|
#[derive(Debug)]
|
|
pub(crate) struct OutgoingFrame {
|
|
pub(crate) encoded: Vec<u8>,
|
|
pub(crate) slot: Option<tokio::sync::OwnedSemaphorePermit>,
|
|
}
|
|
|
|
impl OutgoingFrame {
|
|
pub(super) fn control(encoded: Vec<u8>) -> Self {
|
|
Self {
|
|
encoded,
|
|
slot: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) async fn run<S, E>(
|
|
mut sink: S,
|
|
mut outgoing_rx: mpsc::Receiver<OutgoingFrame>,
|
|
mut pong_rx: mpsc::Receiver<()>,
|
|
) -> RendezvousDisconnectReason
|
|
where
|
|
S: Sink<Message, Error = E> + Unpin,
|
|
E: std::fmt::Display,
|
|
{
|
|
let mut keepalive = tokio::time::interval_at(
|
|
Instant::now() + WEBSOCKET_KEEPALIVE_INTERVAL,
|
|
WEBSOCKET_KEEPALIVE_INTERVAL,
|
|
);
|
|
keepalive.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
|
let mut watchdog = WebSocketPongWatchdog::new(WEBSOCKET_PONG_TIMEOUT);
|
|
loop {
|
|
let message = tokio::select! {
|
|
result = receive_pong(&mut watchdog, &mut pong_rx) => {
|
|
if let Err(reason) = result {
|
|
return reason;
|
|
}
|
|
continue;
|
|
}
|
|
_ = keepalive.tick(), if watchdog.deadline().is_none() => {
|
|
Message::Ping(Vec::new().into())
|
|
}
|
|
encoded = outgoing_rx.recv() => {
|
|
let Some(OutgoingFrame { encoded, slot }) = encoded else {
|
|
return RendezvousDisconnectReason::LocalShutdown;
|
|
};
|
|
drop(slot);
|
|
Message::Binary(encoded.into())
|
|
}
|
|
};
|
|
let is_keepalive_ping = matches!(message, Message::Ping(_));
|
|
// A pong clears only its own deadline, never the fixed write budget.
|
|
let write = tokio::time::timeout(WEBSOCKET_PONG_TIMEOUT, sink.send(message));
|
|
tokio::pin!(write);
|
|
loop {
|
|
tokio::select! {
|
|
// Keep the same send future alive when a pong arrives: cancelling
|
|
// and restarting a partially flushed send can duplicate a frame.
|
|
result = receive_pong(&mut watchdog, &mut pong_rx), if !is_keepalive_ping => {
|
|
if let Err(reason) = result {
|
|
return reason;
|
|
}
|
|
}
|
|
result = &mut write => {
|
|
match result {
|
|
Ok(Ok(())) => {
|
|
if is_keepalive_ping {
|
|
// Pongs received during the ping flush stay queued
|
|
// until the watchdog is armed here.
|
|
watchdog.ping_sent(Instant::now());
|
|
}
|
|
break;
|
|
}
|
|
Ok(Err(error)) => {
|
|
warn!("Noise multiplexed environment websocket write failed: {error}");
|
|
return RendezvousDisconnectReason::WriteError;
|
|
}
|
|
Err(_) => {
|
|
warn!("Noise multiplexed environment websocket write timed out");
|
|
return RendezvousDisconnectReason::WriteError;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn receive_pong(
|
|
watchdog: &mut WebSocketPongWatchdog,
|
|
pong_rx: &mut mpsc::Receiver<()>,
|
|
) -> Result<(), RendezvousDisconnectReason> {
|
|
let deadline = watchdog.deadline();
|
|
tokio::select! {
|
|
biased;
|
|
// Honor a pong already read by the reader even if the timer is also ready.
|
|
pong = pong_rx.recv() => {
|
|
pong.ok_or(RendezvousDisconnectReason::LocalShutdown)?;
|
|
watchdog.received_pong();
|
|
Ok(())
|
|
}
|
|
_ = tokio::time::sleep_until(deadline.unwrap_or_else(Instant::now)), if deadline.is_some() => {
|
|
Err(RendezvousDisconnectReason::PongTimeout)
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "relay_writer_tests.rs"]
|
|
mod tests;
|