Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.

import asyncio
import logging
Expand Down Expand Up @@ -150,6 +150,7 @@ def start_text_gen_server(
verbose: bool = False,
num_replicas: int = 4,
hostname: Optional[str] = None,
sock: Optional[socket.socket] = None,
):
"""Start the text generation server."""
global _SERVER_PROCESSES
Expand All @@ -159,18 +160,29 @@ def start_text_gen_server(
logger.warning("Text gen server processes are already running.")
return

_SHARED_SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_SHARED_SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

if hasattr(socket, 'SO_REUSEPORT'):
try:
_SHARED_SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except OSError:
pass
# The caller may pass in a socket it has already bound ahead of time.
if sock is not None:
bound_port = sock.getsockname()[1]
if bound_port == 0:
raise ValueError(
"socket must be bound to a real port before being passed to start_text_gen_server"
)
_SHARED_SOCKET = sock
server_port = bound_port
_SHARED_SOCKET.setblocking(False)
else:
_SHARED_SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_SHARED_SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

if hasattr(socket, 'SO_REUSEPORT'):
try:
_SHARED_SOCKET.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except OSError:
pass
Comment on lines +177 to +181

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a small comment explaining what this is trying to do?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


bind_address = hostname if hostname is not None else "0.0.0.0"
_SHARED_SOCKET.bind((bind_address, server_port))
_SHARED_SOCKET.setblocking(False)
bind_address = hostname if hostname is not None else "0.0.0.0"
_SHARED_SOCKET.bind((bind_address, server_port))
_SHARED_SOCKET.setblocking(False)

_SHARED_SOCKET.set_inheritable(True)
fd = _SHARED_SOCKET.fileno()
Expand Down
Loading