Skip to content

Commit b09b75b

Browse files
authored
Distributed: add missing check on return code (JuliaLang/julia#34998)
1 parent 46bde5f commit b09b75b

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/managers.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ function connect_w2w(pid::Int, config::WorkerConfig)
511511
(s,s)
512512
end
513513

514-
const client_port = Ref{Cushort}(0)
514+
const client_port = Ref{UInt16}(0)
515515

516516
function socket_reuse_port(iptype)
517517
if ccall(:jl_has_so_reuseport, Int32, ()) == 1
@@ -523,6 +523,8 @@ function socket_reuse_port(iptype)
523523
bind_early && bind_client_port(sock, iptype)
524524
rc = ccall(:jl_tcp_reuseport, Int32, (Ptr{Cvoid},), sock.handle)
525525
if rc < 0
526+
close(sock)
527+
526528
# This is an issue only on systems with lots of client connections, hence delay the warning
527529
nworkers() > 128 && @warn "Error trying to reuse client port number, falling back to regular socket" maxlog=1
528530

@@ -538,9 +540,10 @@ end
538540

539541
function bind_client_port(sock::TCPSocket, iptype)
540542
bind_host = iptype(0)
541-
Sockets.bind(sock, bind_host, client_port[])
542-
_addr, port = getsockname(sock)
543-
client_port[] = port
543+
if Sockets.bind(sock, bind_host, client_port[])
544+
_addr, port = getsockname(sock)
545+
client_port[] = port
546+
end
544547
return sock
545548
end
546549

test/managers.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ using Distributed: parse_machine, bind_client_port, SSHManager, LocalManager
1515
@test_throws ArgumentError parse_machine("127.0.0.1:0")
1616
@test_throws ArgumentError parse_machine("127.0.0.1:65536")
1717

18-
sock = bind_client_port(TCPSocket(), typeof(IPv4(0)))
19-
addr, port = getsockname(sock)
20-
@test addr == ip"0.0.0.0"
21-
22-
sock = bind_client_port(TCPSocket(), typeof(IPv6(0)))
23-
addr, port = getsockname(sock)
24-
@test addr == ip"::"
18+
for ip in (IPv4(0), IPv6(0))
19+
sock = TCPSocket()
20+
@test bind_client_port(sock, typeof(ip)) === sock
21+
addr, port = getsockname(sock)
22+
@test addr === ip
23+
@test port::UInt16 === Distributed.client_port[] != 0
24+
end
2525

2626
@test occursin(r"^SSHManager\(machines=.*\)$",
2727
sprint((t,x) -> show(t, "text/plain", x), SSHManager("127.0.0.1")))

0 commit comments

Comments
 (0)