Skip to content

Commit c96d507

Browse files
authored
[backports-release-1.10]: Distributed: Worker: Bind to the first non-link-local IPv4 address (#58895)
This is a manual backport of JuliaLang/Distributed.jl#137 to Julia 1.10.x. Targets `backports-release-1.10` (#58889). This is a bugfix, and thus it is eligible to be backported.
1 parent 7a28ad5 commit c96d507

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

stdlib/Distributed/src/cluster.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,28 @@ function terminate_all_workers()
12621262
end
12631263
end
12641264

1265+
function choose_bind_addr()
1266+
# We prefer IPv4 over IPv6.
1267+
#
1268+
# We also prefer non-link-local over link-local.
1269+
# (This is because on HPC clusters, link-local addresses are usually not
1270+
# usable for communication between compute nodes.
1271+
#
1272+
# Therefore, our order of preference is:
1273+
# 1. Non-link-local IPv4
1274+
# 2. Non-link-local IPv6
1275+
# 3. Link-local IPv4
1276+
# 4. Link-local IPv6
1277+
addrs = getipaddrs()
1278+
i = something(
1279+
findfirst(ip -> !islinklocaladdr(ip) && ip isa IPv4, addrs), # first non-link-local IPv4
1280+
findfirst(ip -> !islinklocaladdr(ip) && ip isa IPv6, addrs), # first non-link-local IPv6
1281+
findfirst(ip -> ip isa IPv4, addrs), # first IPv4
1282+
findfirst(ip -> ip isa IPv6, addrs), # first IPv6
1283+
)
1284+
return addrs[i]
1285+
end
1286+
12651287
# initialize the local proc network address / port
12661288
function init_bind_addr()
12671289
opts = JLOptions()
@@ -1276,7 +1298,7 @@ function init_bind_addr()
12761298
else
12771299
bind_port = 0
12781300
try
1279-
bind_addr = string(getipaddr())
1301+
bind_addr = string(choose_bind_addr())
12801302
catch
12811303
# All networking is unavailable, initialize bind_addr to the loopback address
12821304
# Will cause an exception to be raised only when used.

0 commit comments

Comments
 (0)