[Utils] Add NetworkAddress abstraction for IPv6-safe address handling (#20306)
This commit is contained in:
@@ -432,31 +432,23 @@ def _prebind_listening_socket(host: str, port: int) -> socket.socket:
|
||||
"""
|
||||
import errno
|
||||
|
||||
from sglang.srt.utils.common import is_valid_ipv6_address
|
||||
from sglang.srt.utils.network import NetworkAddress
|
||||
|
||||
# Determine socket family based on address type
|
||||
if host and is_valid_ipv6_address(host):
|
||||
family = socket.AF_INET6
|
||||
else:
|
||||
family = socket.AF_INET
|
||||
|
||||
sock = socket.socket(family=family, type=socket.SOCK_STREAM)
|
||||
|
||||
# Set socket options to allow port reuse
|
||||
na = NetworkAddress(host or "0.0.0.0", port)
|
||||
sock = socket.socket(family=na.family, type=socket.SOCK_STREAM)
|
||||
# SO_REUSEADDR: Allows binding to a port in TIME_WAIT state
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
|
||||
try:
|
||||
sock.bind((host or "", port))
|
||||
sock.bind(na.to_bind_tuple())
|
||||
sock.set_inheritable(True)
|
||||
logger.info(
|
||||
f"Successfully reserved port {port} on host '{host or ('::' if family == socket.AF_INET6 else '0.0.0.0')}'"
|
||||
)
|
||||
logger.info(f"Successfully reserved port {port} on host '{na.host}'")
|
||||
except OSError as e:
|
||||
sock.close()
|
||||
if e.errno == errno.EADDRINUSE:
|
||||
raise RuntimeError(
|
||||
f"Port {port} is already in use on host '{host or ('::' if family == socket.AF_INET6 else '0.0.0.0')}'. "
|
||||
f"Port {port} is already in use on host '{na.host}'. "
|
||||
f"Please choose a different port with --port or stop the process using this port. "
|
||||
f"You can find the process using: lsof -i :{port} or netstat -tlnp | grep {port}"
|
||||
) from e
|
||||
@@ -467,9 +459,7 @@ def _prebind_listening_socket(host: str, port: int) -> socket.socket:
|
||||
f"Consider using a port >= 1024 (e.g., --port 8000) or run with sudo."
|
||||
) from e
|
||||
else:
|
||||
raise RuntimeError(
|
||||
f"Failed to bind to {host or ('::' if family == socket.AF_INET6 else '0.0.0.0')}:{port}: {e}"
|
||||
) from e
|
||||
raise RuntimeError(f"Failed to bind to {na}: {e}") from e
|
||||
|
||||
return sock
|
||||
|
||||
|
||||
Reference in New Issue
Block a user