diff --git a/codex-rs/rmcp-client/src/bin/test_streamable_http_server.rs b/codex-rs/rmcp-client/src/bin/test_streamable_http_server.rs index 821850d2a8..07e363c9ed 100644 --- a/codex-rs/rmcp-client/src/bin/test_streamable_http_server.rs +++ b/codex-rs/rmcp-client/src/bin/test_streamable_http_server.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use std::io::ErrorKind; use std::net::SocketAddr; use std::sync::Arc; +use std::time::Duration; use axum::Router; use axum::body::Body; @@ -40,6 +41,7 @@ use rmcp::transport::streamable_http_server::session::local::LocalSessionManager use serde::Deserialize; use serde_json::json; use tokio::task; +use tokio::time::sleep; #[derive(Clone)] struct TestToolServer { @@ -251,15 +253,28 @@ fn parse_bind_addr() -> Result> { #[tokio::main] async fn main() -> Result<(), Box> { let bind_addr = parse_bind_addr()?; - let listener = match tokio::net::TcpListener::bind(&bind_addr).await { - Ok(listener) => listener, - Err(err) if err.kind() == ErrorKind::PermissionDenied => { - eprintln!( - "failed to bind to {bind_addr}: {err}. make sure the process has network access" - ); - return Ok(()); + const MAX_BIND_RETRIES: u32 = 20; + const BIND_RETRY_DELAY: Duration = Duration::from_millis(50); + + let mut bind_retries = 0; + let listener = loop { + match tokio::net::TcpListener::bind(&bind_addr).await { + Ok(listener) => break listener, + Err(err) if err.kind() == ErrorKind::PermissionDenied => { + eprintln!( + "failed to bind to {bind_addr}: {err}. make sure the process has network access" + ); + return Ok(()); + } + Err(err) if err.kind() == ErrorKind::AddrInUse && bind_retries < MAX_BIND_RETRIES => { + bind_retries += 1; + eprintln!( + "bind retry {bind_retries}/{MAX_BIND_RETRIES} for {bind_addr} failed: {err}" + ); + sleep(BIND_RETRY_DELAY).await; + } + Err(err) => return Err(err.into()), } - Err(err) => return Err(err.into()), }; eprintln!("starting rmcp streamable http test server on http://{bind_addr}/mcp");