From ced58bfd675264328040fc4f94cc2163c48358cf Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sun, 12 May 2024 15:07:56 -0400 Subject: [PATCH] fix: support IPv6-only network for cargo fix To coordinate diagnositcs between different cargo-as-rustc-proxies, `cargo fix` launches a diagnostc server on localhost. However, the TCP server was hard-coded with an IPv4 address 127.0.0.1, which didn't work with IPv6-only network. This commit fixes the issue by attempting to bind both IPv6 and IPv6 localhost addessses. --- src/cargo/util/diagnostic_server.rs | 3 ++- src/cargo/util/lockserver.rs | 4 +++- src/cargo/util/network/mod.rs | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/cargo/util/diagnostic_server.rs b/src/cargo/util/diagnostic_server.rs index 72eafa63c09..2cce962db92 100644 --- a/src/cargo/util/diagnostic_server.rs +++ b/src/cargo/util/diagnostic_server.rs @@ -16,6 +16,7 @@ use tracing::warn; use crate::core::Edition; use crate::util::errors::CargoResult; +use crate::util::network::LOCALHOST; use crate::util::GlobalContext; const DIAGNOSTICS_SERVER_VAR: &str = "__CARGO_FIX_DIAGNOSTICS_SERVER"; @@ -266,7 +267,7 @@ pub struct StartedServer { impl RustfixDiagnosticServer { pub fn new() -> Result { - let listener = TcpListener::bind("127.0.0.1:0") + let listener = TcpListener::bind(&LOCALHOST[..]) .with_context(|| "failed to bind TCP listener to manage locking")?; let addr = listener.local_addr()?; diff --git a/src/cargo/util/lockserver.rs b/src/cargo/util/lockserver.rs index 14911556a4e..3609a05fc8b 100644 --- a/src/cargo/util/lockserver.rs +++ b/src/cargo/util/lockserver.rs @@ -20,6 +20,8 @@ use std::thread::{self, JoinHandle}; use anyhow::{Context, Error}; +use crate::util::network::LOCALHOST; + pub struct LockServer { listener: TcpListener, addr: SocketAddr, @@ -44,7 +46,7 @@ struct ServerClient { impl LockServer { pub fn new() -> Result { - let listener = TcpListener::bind("127.0.0.1:0") + let listener = TcpListener::bind(&LOCALHOST[..]) .with_context(|| "failed to bind TCP listener to manage locking")?; let addr = listener.local_addr()?; Ok(LockServer { diff --git a/src/cargo/util/network/mod.rs b/src/cargo/util/network/mod.rs index 2bf78159510..a38fad19611 100644 --- a/src/cargo/util/network/mod.rs +++ b/src/cargo/util/network/mod.rs @@ -1,5 +1,10 @@ //! Utilities for networking. +use std::net::Ipv4Addr; +use std::net::Ipv6Addr; +use std::net::SocketAddr; +use std::net::SocketAddrV4; +use std::net::SocketAddrV6; use std::task::Poll; pub mod http; @@ -7,6 +12,12 @@ pub mod proxy; pub mod retry; pub mod sleep; +/// LOCALHOST constants for both IPv4 and IPv6. +pub const LOCALHOST: [SocketAddr; 2] = [ + SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0)), + SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::LOCALHOST, 0, 0, 0)), +]; + pub trait PollExt { fn expect(self, msg: &str) -> T; }