Skip to content

Commit

Permalink
fix listenany to return correct port number for porthint of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
amitmurthy committed May 21, 2017
1 parent 0394f47 commit 2272036
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
11 changes: 2 additions & 9 deletions base/distributed/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,8 @@ function start_worker(out::IO, cookie::AbstractString)
init_worker(cookie)
interface = IPv4(LPROC.bind_addr)
if LPROC.bind_port == 0
addr = Base.InetAddr(interface, 0)
sock = Base.TCPServer()
if bind(sock, addr) && Base.trylisten(sock) == 0
_addr, port = Base._sockname(sock, true)
LPROC.bind_port = port
else
close(sock)
error("no ports available")
end
(port, sock) = listenany(interface, UInt16(0))
LPROC.bind_port = port
else
sock = listen(interface, LPROC.bind_port)
end
Expand Down
4 changes: 4 additions & 0 deletions base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,10 @@ function listenany(host::IPAddr, default_port)
while true
sock = TCPServer()
if bind(sock, addr) && trylisten(sock) == 0
if default_port == 0
_addr, port = _sockname(sock, true)
return (port, sock)
end
return (addr.port, sock)
end
close(sock)
Expand Down
36 changes: 19 additions & 17 deletions test/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,29 @@ end
# test show() function for UDPSocket()
@test repr(UDPSocket()) == "UDPSocket(init)"

port = Channel(1)
defaultport = rand(2000:4000)
tsk = @async begin
p, s = listenany(defaultport)
put!(port, p)
sock = accept(s)
# test write call
write(sock,"Hello World\n")

# test "locked" println to a socket
@sync begin
for i in 1:100
@async println(sock, "a", 1)
for testport in [0, defaultport]
port = Channel(1)
tsk = @async begin
p, s = listenany(testport)
put!(port, p)
sock = accept(s)
# test write call
write(sock,"Hello World\n")

# test "locked" println to a socket
@sync begin
for i in 1:100
@async println(sock, "a", 1)
end
end
close(s)
close(sock)
end
close(s)
close(sock)
wait(port)
@test readstring(connect(fetch(port))) == "Hello World\n" * ("a1\n"^100)
wait(tsk)
end
wait(port)
@test readstring(connect(fetch(port))) == "Hello World\n" * ("a1\n"^100)
wait(tsk)

mktempdir() do tmpdir
socketname = is_windows() ? ("\\\\.\\pipe\\uv-test-" * randstring(6)) : joinpath(tmpdir, "socket")
Expand Down

0 comments on commit 2272036

Please sign in to comment.