mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Stabilize PTY Python REPL test (#13883)
## What changed - The PTY Python REPL test now starts Python with a startup marker already embedded in argv. - The test waits for that marker in PTY output before making assertions. ## Why this fixes the flake - The old version tried to probe the live REPL almost immediately after spawn. - That races PTY initialization, Python startup, and prompt buffering, all of which vary across platforms and CI load. - By having the child process emit a known marker as part of its own startup path, the test gets a deterministic synchronization point that comes from the process under test rather than from guessed timing. ## Scope - Test-only change.
This commit is contained in:
1 parent
6052558a01
commit
5d9db0f995
1 file changed
+13
-22
@@ -130,35 +130,20 @@ async fn collect_output_until_exit(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn wait_for_python_repl_ready(
|
async fn wait_for_python_repl_ready(
|
||||||
writer: &tokio::sync::mpsc::Sender<Vec<u8>>,
|
|
||||||
output_rx: &mut tokio::sync::broadcast::Receiver<Vec<u8>>,
|
output_rx: &mut tokio::sync::broadcast::Receiver<Vec<u8>>,
|
||||||
timeout_ms: u64,
|
timeout_ms: u64,
|
||||||
newline: &str,
|
ready_marker: &str,
|
||||||
) -> anyhow::Result<Vec<u8>> {
|
) -> anyhow::Result<Vec<u8>> {
|
||||||
let mut collected = Vec::new();
|
let mut collected = Vec::new();
|
||||||
let marker = "__codex_pty_ready__";
|
|
||||||
let deadline = tokio::time::Instant::now() + tokio::time::Duration::from_millis(timeout_ms);
|
let deadline = tokio::time::Instant::now() + tokio::time::Duration::from_millis(timeout_ms);
|
||||||
let probe_window = tokio::time::Duration::from_millis(if cfg!(windows) { 750 } else { 250 });
|
|
||||||
|
|
||||||
while tokio::time::Instant::now() < deadline {
|
while tokio::time::Instant::now() < deadline {
|
||||||
writer
|
|
||||||
.send(format!("print('{marker}'){newline}").into_bytes())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let probe_deadline = tokio::time::Instant::now() + probe_window;
|
|
||||||
loop {
|
|
||||||
let now = tokio::time::Instant::now();
|
let now = tokio::time::Instant::now();
|
||||||
if now >= deadline || now >= probe_deadline {
|
let remaining = deadline.saturating_duration_since(now);
|
||||||
break;
|
|
||||||
}
|
|
||||||
let remaining = std::cmp::min(
|
|
||||||
deadline.saturating_duration_since(now),
|
|
||||||
probe_deadline.saturating_duration_since(now),
|
|
||||||
);
|
|
||||||
match tokio::time::timeout(remaining, output_rx.recv()).await {
|
match tokio::time::timeout(remaining, output_rx.recv()).await {
|
||||||
Ok(Ok(chunk)) => {
|
Ok(Ok(chunk)) => {
|
||||||
collected.extend_from_slice(&chunk);
|
collected.extend_from_slice(&chunk);
|
||||||
if String::from_utf8_lossy(&collected).contains(marker) {
|
if String::from_utf8_lossy(&collected).contains(ready_marker) {
|
||||||
return Ok(collected);
|
return Ok(collected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -172,10 +157,9 @@ async fn wait_for_python_repl_ready(
|
|||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
anyhow::bail!(
|
anyhow::bail!(
|
||||||
"timed out waiting for Python REPL readiness in PTY: {:?}",
|
"timed out waiting for Python REPL readiness marker {ready_marker:?} in PTY: {:?}",
|
||||||
String::from_utf8_lossy(&collected)
|
String::from_utf8_lossy(&collected)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -254,10 +238,17 @@ async fn pty_python_repl_emits_output_and_exits() -> anyhow::Result<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let ready_marker = "__codex_pty_ready__";
|
||||||
|
let args = vec![
|
||||||
|
"-i".to_string(),
|
||||||
|
"-q".to_string(),
|
||||||
|
"-c".to_string(),
|
||||||
|
format!("print('{ready_marker}')"),
|
||||||
|
];
|
||||||
let env_map: HashMap<String, String> = std::env::vars().collect();
|
let env_map: HashMap<String, String> = std::env::vars().collect();
|
||||||
let spawned = spawn_pty_process(
|
let spawned = spawn_pty_process(
|
||||||
&python,
|
&python,
|
||||||
&[],
|
&args,
|
||||||
Path::new("."),
|
Path::new("."),
|
||||||
&env_map,
|
&env_map,
|
||||||
&None,
|
&None,
|
||||||
@@ -269,7 +260,7 @@ async fn pty_python_repl_emits_output_and_exits() -> anyhow::Result<()> {
|
|||||||
let newline = if cfg!(windows) { "\r\n" } else { "\n" };
|
let newline = if cfg!(windows) { "\r\n" } else { "\n" };
|
||||||
let startup_timeout_ms = if cfg!(windows) { 10_000 } else { 5_000 };
|
let startup_timeout_ms = if cfg!(windows) { 10_000 } else { 5_000 };
|
||||||
let mut output =
|
let mut output =
|
||||||
wait_for_python_repl_ready(&writer, &mut output_rx, startup_timeout_ms, newline).await?;
|
wait_for_python_repl_ready(&mut output_rx, startup_timeout_ms, ready_marker).await?;
|
||||||
writer
|
writer
|
||||||
.send(format!("print('hello from pty'){newline}").into_bytes())
|
.send(format!("print('hello from pty'){newline}").into_bytes())
|
||||||
.await?;
|
.await?;
|
||||||
|
|||||||
Reference in new issue
Block a user