Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -409,61 +409,6 @@ def reserve_port(host, start=30000, end=40000):
|
||||
raise RuntimeError("No free port available.")
|
||||
|
||||
|
||||
def _prebind_listening_socket(host: str, port: int) -> socket.socket:
|
||||
"""
|
||||
Create and bind a server socket to reserve the port before model loading.
|
||||
|
||||
This prevents port conflicts that could occur if another process (e.g., a
|
||||
subprocess spawned during model loading) binds to the same port before the
|
||||
HTTP server starts. By binding early, we ensure the port is available and
|
||||
reserved for the HTTP server. The socket is intentionally not put into
|
||||
listening mode here to avoid accepting probe connections before uvicorn is
|
||||
ready to serve requests.
|
||||
|
||||
Args:
|
||||
host: The host address to bind to.
|
||||
port: The port number to bind to.
|
||||
|
||||
Returns:
|
||||
A bound socket object.
|
||||
|
||||
Raises:
|
||||
RuntimeError: If the port is already in use or permission is denied.
|
||||
"""
|
||||
import errno
|
||||
|
||||
from sglang.srt.utils.network import NetworkAddress
|
||||
|
||||
# Determine socket family based on address type
|
||||
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(na.to_bind_tuple())
|
||||
sock.set_inheritable(True)
|
||||
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 '{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
|
||||
elif e.errno == errno.EACCES:
|
||||
raise RuntimeError(
|
||||
f"Permission denied when trying to bind to port {port}. "
|
||||
f"Ports below 1024 require root/sudo privileges. "
|
||||
f"Consider using a port >= 1024 (e.g., --port 8000) or run with sudo."
|
||||
) from e
|
||||
else:
|
||||
raise RuntimeError(f"Failed to bind to {na}: {e}") from e
|
||||
|
||||
return sock
|
||||
|
||||
|
||||
def release_port(lock_socket):
|
||||
"""
|
||||
Release the reserved port by closing the lock socket.
|
||||
|
||||
Reference in New Issue
Block a user