mirror of
https://github.com/openai/codex.git
synced 2026-11-18 07:00:27 +00:00
Read voice helper frames independently of pipe chunks (#42404)
## Why Pipe output chunks can split one protocol frame or contain several frames. A timed-out read must also retain any partial frame it already consumed so the next exchange can continue decoding it. ## What changed Add a stateful `MessageReader` that buffers partial input, preserves unread bytes from coalesced chunks, and validates frame bounds before reading the payload. Use it for voice helper handshake and shutdown responses. ## Testing Add async tests for fragmented and coalesced frames, cancelled reads, oversized headers, truncated and malformed input, and invalid pipe chunk sizes. GitOrigin-RevId: 274d9698e05f2de2147235b6e4e1bd38dbb7c23b
This commit is contained in:
1 parent
1281778e32
commit
deb1471166
4 files changed
+212
-23
No files matched your search
@@ -4,20 +4,17 @@ use std::collections::HashMap;
|
|||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::Context;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use anyhow::ensure;
|
use anyhow::ensure;
|
||||||
use codex_install_context::CodexPackageLayout;
|
use codex_install_context::CodexPackageLayout;
|
||||||
use codex_utils_pty::ProcessHandle;
|
use codex_utils_pty::ProcessHandle;
|
||||||
use codex_utils_pty::SpawnedProcess;
|
use codex_utils_pty::SpawnedProcess;
|
||||||
use tokio::sync::mpsc;
|
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
|
|
||||||
use crate::MAX_FRAME_BYTES;
|
|
||||||
use crate::Message;
|
use crate::Message;
|
||||||
use crate::decode_frame;
|
|
||||||
use crate::encode_frame;
|
use crate::encode_frame;
|
||||||
|
use crate::message_reader::MessageReader;
|
||||||
|
|
||||||
const DEADLINE: Duration = Duration::from_secs(/*secs*/ 5);
|
const DEADLINE: Duration = Duration::from_secs(/*secs*/ 5);
|
||||||
|
|
||||||
@@ -25,7 +22,7 @@ const DEADLINE: Duration = Duration::from_secs(/*secs*/ 5);
|
|||||||
/// A successful handshake establishes compatibility only, not an active audio session.
|
/// A successful handshake establishes compatibility only, not an active audio session.
|
||||||
pub struct VoiceHost {
|
pub struct VoiceHost {
|
||||||
process: ProcessHandle,
|
process: ProcessHandle,
|
||||||
output: mpsc::Receiver<Vec<u8>>,
|
output: MessageReader,
|
||||||
exit: oneshot::Receiver<i32>,
|
exit: oneshot::Receiver<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +57,7 @@ impl VoiceHost {
|
|||||||
drop(stderr_rx); // Drain and discard diagnostics rather than logging untyped child output.
|
drop(stderr_rx); // Drain and discard diagnostics rather than logging untyped child output.
|
||||||
let mut host = Self {
|
let mut host = Self {
|
||||||
process: session,
|
process: session,
|
||||||
output: stdout_rx,
|
output: MessageReader::new(stdout_rx),
|
||||||
exit: exit_rx,
|
exit: exit_rx,
|
||||||
};
|
};
|
||||||
host.exchange(
|
host.exchange(
|
||||||
@@ -92,23 +89,9 @@ impl VoiceHost {
|
|||||||
.send(encode_frame(&request)?)
|
.send(encode_frame(&request)?)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| anyhow::anyhow!("voice helper input closed"))?;
|
.map_err(|_| anyhow::anyhow!("voice helper input closed"))?;
|
||||||
let mut frame = Vec::new();
|
let response = self.output.next().await?;
|
||||||
loop {
|
ensure!(response == expected, "unexpected voice helper response");
|
||||||
let chunk = self
|
Ok(())
|
||||||
.output
|
|
||||||
.recv()
|
|
||||||
.await
|
|
||||||
.context("voice helper output closed")?;
|
|
||||||
ensure!(
|
|
||||||
frame.len() + chunk.len() <= MAX_FRAME_BYTES + 4,
|
|
||||||
"voice helper output exceeds limit"
|
|
||||||
);
|
|
||||||
frame.extend(chunk);
|
|
||||||
if let Some(response) = decode_frame(&frame)? {
|
|
||||||
ensure!(response == expected, "unexpected voice helper response");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
mod client;
|
mod client;
|
||||||
|
mod message_reader;
|
||||||
mod protocol;
|
mod protocol;
|
||||||
|
|
||||||
pub use client::VoiceHost;
|
pub use client::VoiceHost;
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
//! Read bounded helper frames independently of pipe chunks; cancellation retains partial input.
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use crate::Message;
|
||||||
|
use crate::decode_frame;
|
||||||
|
|
||||||
|
// Matches the maximum read in codex_utils_pty's pipe output reader. Retain at most
|
||||||
|
// one such chunk and one bounded partial frame, never a queue of decoded messages.
|
||||||
|
const MAX_CHUNK_BYTES: usize = 8_192;
|
||||||
|
|
||||||
|
pub(crate) struct MessageReader {
|
||||||
|
output: mpsc::Receiver<Vec<u8>>,
|
||||||
|
pending: std::vec::IntoIter<u8>,
|
||||||
|
frame: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MessageReader {
|
||||||
|
pub(crate) fn new(output: mpsc::Receiver<Vec<u8>>) -> Self {
|
||||||
|
Self {
|
||||||
|
output,
|
||||||
|
pending: Vec::new().into_iter(),
|
||||||
|
frame: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn next(&mut self) -> io::Result<Message> {
|
||||||
|
loop {
|
||||||
|
let target = if let [a, b, c, d, ..] = self.frame.as_slice() {
|
||||||
|
// Validate the header before consuming or allocating its payload.
|
||||||
|
decode_frame(&self.frame)?;
|
||||||
|
u32::from_be_bytes([*a, *b, *c, *d]) as usize + 4
|
||||||
|
} else {
|
||||||
|
4
|
||||||
|
};
|
||||||
|
while self.frame.len() < target {
|
||||||
|
if self.pending.len() == 0 {
|
||||||
|
let chunk = self.output.recv().await.ok_or_else(|| {
|
||||||
|
io::Error::new(io::ErrorKind::UnexpectedEof, "voice helper output closed")
|
||||||
|
})?;
|
||||||
|
if chunk.is_empty() || chunk.len() > MAX_CHUNK_BYTES {
|
||||||
|
return Err(io::Error::other("invalid voice helper output chunk"));
|
||||||
|
}
|
||||||
|
self.pending = chunk.into_iter();
|
||||||
|
}
|
||||||
|
self.frame
|
||||||
|
.extend(self.pending.by_ref().take(target - self.frame.len()));
|
||||||
|
}
|
||||||
|
if let Some(message) = decode_frame(&self.frame)? {
|
||||||
|
self.frame.clear();
|
||||||
|
return Ok(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "message_reader_tests.rs"]
|
||||||
|
mod tests;
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
//! Exercise real frame decoding across pipe boundaries, cancellation, and invalid input.
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
use std::io;
|
||||||
|
use std::task::Context;
|
||||||
|
use std::task::Poll;
|
||||||
|
use std::task::Waker;
|
||||||
|
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use super::MAX_CHUNK_BYTES;
|
||||||
|
use super::MessageReader;
|
||||||
|
use crate::MAX_FRAME_BYTES;
|
||||||
|
use crate::Message;
|
||||||
|
use crate::encode_frame;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn reads_fragmented_and_coalesced_messages() -> io::Result<()> {
|
||||||
|
let expected = [
|
||||||
|
Message::Hello {
|
||||||
|
protocol: 1,
|
||||||
|
build_commit: "test-build".into(),
|
||||||
|
},
|
||||||
|
Message::Ready {},
|
||||||
|
Message::Close {},
|
||||||
|
Message::Closed {},
|
||||||
|
];
|
||||||
|
let frames = expected
|
||||||
|
.iter()
|
||||||
|
.map(encode_frame)
|
||||||
|
.collect::<io::Result<Vec<_>>>()?
|
||||||
|
.concat();
|
||||||
|
// Every split exercises both a partial frame and multiple messages in a chunk.
|
||||||
|
for split in 1..frames.len() {
|
||||||
|
let (sender, receiver) = mpsc::channel(/*buffer*/ 2);
|
||||||
|
sender.try_send(frames[..split].to_vec()).unwrap();
|
||||||
|
sender.try_send(frames[split..].to_vec()).unwrap();
|
||||||
|
drop(sender);
|
||||||
|
let mut reader = MessageReader::new(receiver);
|
||||||
|
let mut actual = Vec::new();
|
||||||
|
for _ in &expected {
|
||||||
|
actual.push(reader.next().await?);
|
||||||
|
}
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
assert_eq!(
|
||||||
|
reader.next().await.unwrap_err().kind(),
|
||||||
|
io::ErrorKind::UnexpectedEof
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn cancelled_read_preserves_each_partial_header_and_payload() -> io::Result<()> {
|
||||||
|
let expected = Message::Hello {
|
||||||
|
protocol: 1,
|
||||||
|
build_commit: "test-build".into(),
|
||||||
|
};
|
||||||
|
let frame = encode_frame(&expected)?;
|
||||||
|
for split in 1..frame.len() {
|
||||||
|
let (sender, receiver) = mpsc::channel(/*buffer*/ 1);
|
||||||
|
sender.try_send(frame[..split].to_vec()).unwrap();
|
||||||
|
let mut reader = MessageReader::new(receiver);
|
||||||
|
{
|
||||||
|
let mut read = std::pin::pin!(reader.next());
|
||||||
|
assert!(matches!(
|
||||||
|
read.as_mut().poll(&mut Context::from_waker(Waker::noop())),
|
||||||
|
Poll::Pending
|
||||||
|
));
|
||||||
|
}
|
||||||
|
sender.try_send(frame[split..].to_vec()).unwrap();
|
||||||
|
assert_eq!(reader.next().await?, expected);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rejects_oversized_headers_without_waiting_for_payload() {
|
||||||
|
for length in [MAX_FRAME_BYTES as u32 + 1, u32::MAX] {
|
||||||
|
let (sender, receiver) = mpsc::channel(/*buffer*/ 1);
|
||||||
|
sender.try_send(length.to_be_bytes().to_vec()).unwrap();
|
||||||
|
let mut reader = MessageReader::new(receiver);
|
||||||
|
let mut read = std::pin::pin!(reader.next());
|
||||||
|
assert!(matches!(
|
||||||
|
read.as_mut().poll(&mut Context::from_waker(Waker::noop())),
|
||||||
|
Poll::Ready(Err(_))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rejects_truncation_at_every_byte_boundary() -> io::Result<()> {
|
||||||
|
let frame = encode_frame(&Message::Ready {})?;
|
||||||
|
for end in 0..frame.len() {
|
||||||
|
let (sender, receiver) = mpsc::channel(/*buffer*/ 1);
|
||||||
|
if end > 0 {
|
||||||
|
sender.try_send(frame[..end].to_vec()).unwrap();
|
||||||
|
}
|
||||||
|
drop(sender);
|
||||||
|
assert_eq!(
|
||||||
|
MessageReader::new(receiver)
|
||||||
|
.next()
|
||||||
|
.await
|
||||||
|
.unwrap_err()
|
||||||
|
.kind(),
|
||||||
|
io::ErrorKind::UnexpectedEof
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rejects_malformed_input_without_echoing_payload() {
|
||||||
|
let secret = b"sensitive-invalid-json";
|
||||||
|
let mut invalid = (secret.len() as u32).to_be_bytes().to_vec();
|
||||||
|
invalid.extend(secret);
|
||||||
|
let (sender, receiver) = mpsc::channel(/*buffer*/ 1);
|
||||||
|
sender.try_send(invalid).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
MessageReader::new(receiver)
|
||||||
|
.next()
|
||||||
|
.await
|
||||||
|
.unwrap_err()
|
||||||
|
.to_string(),
|
||||||
|
"invalid voice frame"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rejects_chunks_outside_the_pipe_read_bound() {
|
||||||
|
for chunk in [vec![], vec![0; MAX_CHUNK_BYTES + 1]] {
|
||||||
|
let (sender, receiver) = mpsc::channel(/*buffer*/ 1);
|
||||||
|
sender.try_send(chunk).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
MessageReader::new(receiver)
|
||||||
|
.next()
|
||||||
|
.await
|
||||||
|
.unwrap_err()
|
||||||
|
.to_string(),
|
||||||
|
"invalid voice helper output chunk"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user