From bca937b4aeeebad1c5e9b2e497522bbc8af3a538 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Thu, 3 Jul 2025 14:27:57 -0400 Subject: [PATCH] Distributed: Worker: Bind to the first non-link-local IPv4 address This is a manual backport of https://github.com/JuliaLang/Distributed.jl/pull/137 to Julia 1.10.x --- stdlib/Distributed/src/cluster.jl | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/stdlib/Distributed/src/cluster.jl b/stdlib/Distributed/src/cluster.jl index 2444695f90afd..9467f8e798db1 100644 --- a/stdlib/Distributed/src/cluster.jl +++ b/stdlib/Distributed/src/cluster.jl @@ -1262,6 +1262,28 @@ function terminate_all_workers() end end +function choose_bind_addr() + # We prefer IPv4 over IPv6. + # + # We also prefer non-link-local over link-local. + # (This is because on HPC clusters, link-local addresses are usually not + # usable for communication between compute nodes. + # + # Therefore, our order of preference is: + # 1. Non-link-local IPv4 + # 2. Non-link-local IPv6 + # 3. Link-local IPv4 + # 4. Link-local IPv6 + addrs = getipaddrs() + i = something( + findfirst(ip -> !islinklocaladdr(ip) && ip isa IPv4, addrs), # first non-link-local IPv4 + findfirst(ip -> !islinklocaladdr(ip) && ip isa IPv6, addrs), # first non-link-local IPv6 + findfirst(ip -> ip isa IPv4, addrs), # first IPv4 + findfirst(ip -> ip isa IPv6, addrs), # first IPv6 + ) + return addrs[i] +end + # initialize the local proc network address / port function init_bind_addr() opts = JLOptions() @@ -1276,7 +1298,7 @@ function init_bind_addr() else bind_port = 0 try - bind_addr = string(getipaddr()) + bind_addr = string(choose_bind_addr()) catch # All networking is unavailable, initialize bind_addr to the loopback address # Will cause an exception to be raised only when used.