Skip to content
Merged
Changes from 1 commit
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
32 changes: 21 additions & 11 deletions python/sglang/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,29 @@ def download_and_cache_file(url: str, filename: Optional[str] = None):

import fcntl

LOCKFILE = "/tmp/port_lock"
PORT_REGISTRY = "/tmp/port_registry.json"

def is_in_ci():
from sglang.test.test_utils import is_in_ci

return is_in_ci()


LOCKFILE = os.path.expanduser("~/.sglang_port_lock")
PORT_REGISTRY = os.path.expanduser("~/.sglang_port_registry.json")


def print_highlight(html_content: str):
html_content = str(html_content).replace("\n", "<br>")
display(HTML(f"<strong style='color: #00008B;'>{html_content}</strong>"))
if is_in_ci():
html_content = str(html_content).replace("\n", "<br>")
display(HTML(f"<strong style='color: #00008B;'>{html_content}</strong>"))
else:
print(html_content)


def init_port_registry():
"""Initialize the port registry file if it doesn't exist."""
if not os.path.exists(PORT_REGISTRY):
with open(PORT_REGISTRY, "w") as f:
with open(PORT_REGISTRY, "a") as f:
json.dump([], f)


Expand All @@ -330,34 +340,34 @@ def reserve_port(start=30000, end=40000):
Returns the allocated port.
"""
init_port_registry()
with open(LOCKFILE, "w") as lock:
with open(LOCKFILE, "a") as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
try:
with open(PORT_REGISTRY, "r") as f:
with open(PORT_REGISTRY, "a") as f:
used = json.load(f)
except Exception:
used = []
for port in range(start, end):
if port not in used:
used.append(port)
with open(PORT_REGISTRY, "w") as f:
with open(PORT_REGISTRY, "a") as f:
json.dump(used, f)
return port
raise RuntimeError("No free port available")


def release_port(port):
"""Release the reserved port by removing it from the registry."""
with open(LOCKFILE, "w") as lock:
with open(LOCKFILE, "a") as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
try:
with open(PORT_REGISTRY, "r") as f:
with open(PORT_REGISTRY, "a") as f:
used = json.load(f)
except Exception:
used = []
if port in used:
used.remove(port)
with open(PORT_REGISTRY, "w") as f:
with open(PORT_REGISTRY, "a") as f:
json.dump(used, f)


Expand Down