From d857968a658d4de6760d6f4e8323e564097cdcfc Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:40:27 +0800 Subject: [PATCH] fix(ci): wait for the MCP listener before restarting on its port --- crates/ide-mcp/tests/server.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/crates/ide-mcp/tests/server.rs b/crates/ide-mcp/tests/server.rs index ca2feadbab..f41559b495 100644 --- a/crates/ide-mcp/tests/server.rs +++ b/crates/ide-mcp/tests/server.rs @@ -881,14 +881,9 @@ async fn the_bridge_picks_the_server_again_when_the_servers_change() { async fn the_bridge_picks_the_server_again_after_a_restart_on_the_same_port() { let (_tmp, base, discovery) = layout(&["proj-a", "proj-b", "proj-c"]); let (proj_a, proj_b, proj_c) = (base.join("proj-a"), base.join("proj-b"), base.join("proj-c")); - let port = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap().local_addr().unwrap().port(); - let fixed = ServerOptions { port: Some(port), ..Default::default() }; - let runtime = tokio::runtime::Handle::current(); - let start_b = |registry: WithBackground| { - McpServer::start_with(Arc::new(registry), Some(discovery.clone()), &runtime, fixed.clone()).unwrap() - }; // Neither serves `proj-b` at `initialize`, which settles on A, the server started last. - let server_b = start_b(WithBackground { open: proj_c.clone(), background: Vec::new(), remote: None }); + let server_b = start_in(&discovery, WithBackground { open: proj_c.clone(), background: Vec::new(), remote: None }); + let port = server_b.port(); let server_a = start_in(&discovery, Registry { project: proj_a.clone() }); let mut client = Client::start(&discovery, &proj_b.join("src")); client.ask("initialize", json!({ "protocolVersion": "2025-06-18" })).await; @@ -896,8 +891,23 @@ async fn the_bridge_picks_the_server_again_after_a_restart_on_the_same_port() { assert_eq!(list["result"]["tools"][0]["name"], "everything", "{list}"); server_b.stop(); - let server_b = start_b(WithBackground::serving(&proj_c, &proj_b)); - assert_eq!(server_b.port(), port); + // stop signals the accept task; the listener is closed asynchronously. Wait for + // the old port to be released, as the IDE does before restarting its server. + tokio::time::timeout(std::time::Duration::from_secs(5), async { + while std::net::TcpListener::bind(("127.0.0.1", port)).is_err() { + tokio::time::sleep(std::time::Duration::from_millis(20)).await; + } + }) + .await + .expect("the stopped server should release its port"); + let server_b = McpServer::start_with( + Arc::new(WithBackground::serving(&proj_c, &proj_b)), + Some(discovery.clone()), + &tokio::runtime::Handle::current(), + ServerOptions { port: Some(port), ..Default::default() }, + ) + .unwrap(); + assert_eq!(server_b.port(), port, "{:?}", server_b.port_unavailable()); let list = client.ask("tools/list", json!({})).await; assert_eq!(list["result"]["tools"][0]["description"], format!("Names {}", proj_b.display()), "{list}"); client.close().await;