From bc6728ef6872a8982a0d1f104ae2affceb689493 Mon Sep 17 00:00:00 2001 From: Dylan Hurd Date: Tue, 24 Nov 2026 22:10:11 +0000 Subject: [PATCH] Stabilize code-mode panic and process metrics tests (#63767) ## Why Formatting a panic backtrace can outlast the process metrics RPC deadline. A timed-out RPC can also hold the collector permit before its worker starts, so checking the invocation count immediately can race with worker scheduling. ## What changed - Trigger the collector unwind without running the panic hook. Check the collection failure, panic, and busy errors explicitly, and wait for the blocked collector to start before checking invocation and connection counts. - Await the V8 runtime thread panic notifications directly in both tests, removing their one-second receive timeouts. GitOrigin-RevId: 9b2c36577e838c9d327592f9729d3a8a7868c56b --- .../tests/grpc/process_metrics_tests.rs | 23 +++++++++++++++---- codex-rs/code-mode-runtime/src/runtime/mod.rs | 9 ++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/codex-rs/code-mode-host/tests/grpc/process_metrics_tests.rs b/codex-rs/code-mode-host/tests/grpc/process_metrics_tests.rs index 7f4c16351a..3daa31e5bb 100644 --- a/codex-rs/code-mode-host/tests/grpc/process_metrics_tests.rs +++ b/codex-rs/code-mode-host/tests/grpc/process_metrics_tests.rs @@ -26,12 +26,16 @@ async fn metrics_failures_and_deadlines_preserve_sessions_and_keepalive() -> Res let collector_calls = calls.clone(); let (release, blocked) = std::sync::mpsc::channel::<()>(); let blocked = Mutex::new(blocked); + let (started_tx, mut started_rx) = tokio::sync::mpsc::unbounded_channel(); let host = codex_code_mode_host::GrpcCodeModeHost::with_process_metrics(Arc::new(move || { match collector_calls.fetch_add(1, Ordering::SeqCst) { 0 => Ok(snapshot.clone()), 1 => Err("metrics unavailable".to_string()), - 2 => panic!("collector panic"), + // Exercise the host's unwind handling without running the process panic hook: + // formatting a backtrace can itself outlast the metrics RPC deadline. + 2 => std::panic::resume_unwind(Box::new("collector panic")), _ => { + started_tx.send(()).unwrap(); blocked .lock() .unwrap() @@ -82,8 +86,15 @@ async fn metrics_failures_and_deadlines_preserve_sessions_and_keepalive() -> Res .map_err(anyhow::Error::msg)?, CodeModeProcessMetricsSample::Snapshot(expected) ); - for cell in 2..=4 { - assert!(provider.sample_process_metrics().await.is_err()); + for (cell, expected_error) in [ + (2, Some("process metrics collection failed")), + (3, Some("process metrics collector panicked")), + (4, None), + ] { + let error = provider.sample_process_metrics().await.unwrap_err(); + if let Some(expected_error) = expected_error { + assert!(error.contains(expected_error), "{error}"); + } let response = execute( &session, request("text(load('survives'));"), @@ -97,7 +108,11 @@ async fn metrics_failures_and_deadlines_preserve_sessions_and_keepalive() -> Res } // A canceled blocked RPC retains its collector quota: no unbounded worker // threads are added, and neither the sample nor overload opens another channel. - assert!(provider.sample_process_metrics().await.is_err()); + let busy = provider.sample_process_metrics().await.unwrap_err(); + assert!(busy.contains("collector is busy"), "{busy}"); + // Busy proves the server admitted the blocked call. It may have acquired the + // permit before its detached worker got scheduled, even after the RPC deadline. + started_rx.recv().await.expect("blocked collector started"); assert_eq!(calls.load(Ordering::SeqCst), 4); assert_eq!(connections.load(Ordering::SeqCst), 1); release.send(())?; diff --git a/codex-rs/code-mode-runtime/src/runtime/mod.rs b/codex-rs/code-mode-runtime/src/runtime/mod.rs index 14c820bf8c..48332c06b3 100644 --- a/codex-rs/code-mode-runtime/src/runtime/mod.rs +++ b/codex-rs/code-mode-runtime/src/runtime/mod.rs @@ -388,10 +388,7 @@ mod tests { ); assert_eq!( - tokio::time::timeout(Duration::from_secs(1), failure_rx.recv()) - .await - .expect("runtime failure timeout") - .expect("runtime failure"), + failure_rx.recv().await.expect("runtime failure"), "code-mode V8 runtime thread panicked" ); } @@ -406,9 +403,7 @@ mod tests { ); assert!(matches!( - tokio::time::timeout(Duration::from_secs(1), event_rx.recv()) - .await - .expect("runtime panic event timeout"), + event_rx.recv().await, Some(RuntimeEvent::ThreadPanicked) )); }