Skip to content

Commit d131cc7

Browse files
authored
find port (#3429)
* find port * safe
1 parent a4c43b4 commit d131cc7

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lmdeploy/pytorch/engine/executor/dist_utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
import torch.distributed as dist
77

88

9-
def find_available_port() -> bool:
9+
def find_available_port(start_port: int = 0) -> int:
1010
"""find available port."""
11-
port = 29500
12-
while True:
13-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
14-
if s.connect_ex(('localhost', port)) != 0:
15-
return port
16-
port += 1
11+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
12+
try:
13+
s.bind(('127.0.0.1', start_port))
14+
port = s.getsockname()[1]
15+
return port
16+
except socket.error as e:
17+
if start_port == 0:
18+
raise RuntimeError('Failed to find available port.') from e
19+
return find_available_port(0)
1720

1821

1922
def setup_master_addr(addr: str, port: str):

0 commit comments

Comments
 (0)