Skip to content

Commit

Permalink
fix: support IPv6-only network for cargo fix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weihanglo committed May 12, 2024
1 parent 2f17770 commit ced58bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/cargo/util/diagnostic_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -266,7 +267,7 @@ pub struct StartedServer {

impl RustfixDiagnosticServer {
pub fn new() -> Result<Self, Error> {
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()?;

Expand Down
4 changes: 3 additions & 1 deletion src/cargo/util/lockserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -44,7 +46,7 @@ struct ServerClient {

impl LockServer {
pub fn new() -> Result<LockServer, Error> {
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 {
Expand Down
11 changes: 11 additions & 0 deletions src/cargo/util/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
//! 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;
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<T> {
fn expect(self, msg: &str) -> T;
}
Expand Down

0 comments on commit ced58bf

Please sign in to comment.