214214mutable struct LocalProcess
215215 id:: Int
216216 bind_addr:: String
217+ bind_port_hint:: Int
217218 bind_port:: Int
218219 cookie:: String
219220 LocalProcess () = new (1 )
@@ -257,8 +258,7 @@ function start_worker(out::IO, cookie::AbstractString=readline(stdin); close_std
257258 init_worker (cookie)
258259 interface = IPv4 (LPROC. bind_addr)
259260 if LPROC. bind_port == 0
260- port_hint = 9000 + (getpid () % 1000 )
261- (port, sock) = listenany (interface, port_hint)
261+ (port, sock) = listenany (interface, LPROC. bind_port_hint)
262262 LPROC. bind_port = Int (port)
263263 else
264264 sock = listen (interface, LPROC. bind_port)
@@ -733,8 +733,29 @@ function launch_additional(np::Integer, cmd::Cmd)
733733 io_objs = Vector {Any} (undef, np)
734734 addresses = Vector {Any} (undef, np)
735735
736+ worker_cmd = Cmd (cmd)
737+ bind_idx = findfirst (== (" --bind-to" ), cmd)
738+ if ! isnothing (bind_idx)
739+ # The actual bind spec will be the next argument
740+ bind_idx += 1
741+
742+ bind_addr = worker_cmd[bind_idx]
743+ parts = split (bind_addr, ' :' )
744+ if length (parts) == 2
745+ port_str = parts[2 ]
746+
747+ # If the port is not specified as a port hint then we convert it
748+ # to a hint, otherwise the workers will try to bind to the same
749+ # port and error.
750+ if ! startswith (port_str, ' [' )
751+ new_bind_addr = " $(parts[1 ]) :[$(port_str) ]"
752+ worker_cmd. exec[bind_idx] = new_bind_addr
753+ end
754+ end
755+ end
756+
736757 for i in 1 : np
737- io = open (detach (cmd ), " r+" )
758+ io = open (detach (worker_cmd ), " r+" )
738759 write_cookie (io)
739760 io_objs[i] = io. out
740761 end
@@ -1318,17 +1339,24 @@ end
13181339# initialize the local proc network address / port
13191340function init_bind_addr ()
13201341 opts = JLOptions ()
1342+ bind_port_hint = 9000 + (getpid () % 1000 )
1343+ bind_port = 0
1344+
13211345 if opts. bindto != C_NULL
13221346 bind_to = split (unsafe_string (opts. bindto), " :" )
13231347 bind_addr = string (parse (IPAddr, bind_to[1 ]))
13241348 if length (bind_to) > 1
1325- bind_port = parse (Int,bind_to[2 ])
1326- else
1327- bind_port = 0
1349+ port_str = bind_to[2 ]
1350+ if startswith (port_str, ' [' )
1351+ if ! endswith (port_str, ' ]' )
1352+ error (" Malformed bind port string, please see the addprocs documentation for the formatting rules: $(port_str) " )
1353+ end
1354+ bind_port_hint = parse (Int, port_str[2 : end - 1 ])
1355+ else
1356+ bind_port = parse (Int, port_str)
1357+ end
13281358 end
13291359 else
1330- bind_port = 0
1331-
13321360 interfaces = _get_interfaces (IPv4)
13331361 if isempty (interfaces)
13341362 # Include IPv6 interfaces if there are no IPv4 ones
@@ -1355,6 +1383,7 @@ function init_bind_addr()
13551383 global LPROC
13561384 LPROC. bind_addr = bind_addr
13571385 LPROC. bind_port = bind_port
1386+ LPROC. bind_port_hint = bind_port_hint
13581387end
13591388
13601389using Random: randstring
0 commit comments