Handle busy ports and stop orphaning mlx_lm.server
On a Mac with port 8000 already taken, the proxy failed to bind after mlx_lm.server had started, leaving an orphan each time launchd retried. - setup now stops our own services, then moves the API/dashboard to the next free ports (or errors if a port was given explicitly) - the MLX service binds its port before starting mlx_lm.server, always stops the child on exit, and cleans up leftovers from earlier crashes - drop the nonexistent huggingface_hub[cli] extra Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QaE1hpvyZTJixEbUUMj9Ca
This commit is contained in:
parent
d2ab08a5f5
commit
16f096714e
2 changed files with 100 additions and 12 deletions
|
|
@ -97,14 +97,24 @@ def cmd_setup(a):
|
|||
if input("Continue with setup? [Y/n] ").strip().lower() not in ("", "y", "yes"):
|
||||
raise SystemExit("aborted")
|
||||
|
||||
# Our own services would hold the ports on a re-run; stop them so anything still
|
||||
# listening belongs to another program. (--no-start leaves a running setup alone.)
|
||||
if not a.no_start:
|
||||
for name in service.NAMES:
|
||||
service.stop(name)
|
||||
# MLX needs two ports: the proxy, and mlx_lm.server on the next one.
|
||||
api_span = 2 if p["backend"] == "mlx" else 1
|
||||
api_port = choose_port("API", a.api_port, old.get("api_port", 8000), api_span, a.no_start, avoid=set())
|
||||
web_port = choose_port("dashboard", a.web_port, old.get("web_port", 8080), 1, a.no_start,
|
||||
avoid=set(range(api_port, api_port + api_span)))
|
||||
|
||||
say(f"\n== Installing {p['backend']} (this can take several minutes) ==")
|
||||
backend_info = {} if a.skip_backend else backend_module(p["backend"]).install(info, say)
|
||||
|
||||
cfg = {
|
||||
"version": 1, "kind": info["kind"], "backend": p["backend"],
|
||||
"hardware_name": hardware_name(info), "budget_gib": p["budget_gib"], "tp": p["tp"],
|
||||
"prefer": prefer, "api_port": a.api_port or old.get("api_port", 8000),
|
||||
"web_port": a.web_port or old.get("web_port", 8080),
|
||||
"prefer": prefer, "api_port": api_port, "web_port": web_port,
|
||||
"max_jobs": vllm_backend.max_jobs(info["ram_gib"], info["cpus"]),
|
||||
"models": p["models"], "excluded": p["excluded"], "active": active,
|
||||
p["backend"]: {**old.get(p["backend"], {}), **backend_info},
|
||||
|
|
@ -131,6 +141,55 @@ Done.
|
|||
Connect opencode: run `llmhost opencode-config` and follow what it prints.""")
|
||||
|
||||
|
||||
def port_free(port):
|
||||
# Check both: another program may hold only 127.0.0.1:port, and macOS can still let
|
||||
# 0.0.0.0:port bind while it does.
|
||||
for addr in ("0.0.0.0", "127.0.0.1"):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
s.bind((addr, port))
|
||||
except OSError:
|
||||
return False
|
||||
finally:
|
||||
s.close()
|
||||
return True
|
||||
|
||||
|
||||
def port_owner(port):
|
||||
"""Best-effort name of the program listening on a port."""
|
||||
try:
|
||||
out = subprocess.run(["lsof", "-nP", f"-iTCP:{port}", "-sTCP:LISTEN"],
|
||||
capture_output=True, text=True, timeout=5).stdout.splitlines()
|
||||
if len(out) > 1:
|
||||
f = out[1].split()
|
||||
return f"{f[0]} (pid {f[1]})"
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
pass
|
||||
return "another program"
|
||||
|
||||
|
||||
def choose_port(label, explicit, current, span, no_start, avoid):
|
||||
"""Use the requested/saved port if it's free; otherwise move to the next free one."""
|
||||
def ok(p):
|
||||
return all(port_free(q) and q not in avoid for q in range(p, p + span))
|
||||
if explicit:
|
||||
if not ok(explicit) and not no_start:
|
||||
raise SystemExit(f"{label} port {explicit} is in use by {port_owner(explicit)}.")
|
||||
return explicit
|
||||
if ok(current):
|
||||
return current
|
||||
busy = next(q for q in range(current, current + span) if not port_free(q) or q in avoid)
|
||||
owner = "llm-host's API" if busy in avoid else port_owner(busy)
|
||||
if no_start:
|
||||
say(f"Note: {label} port {busy} is in use by {owner}; it must be free before you start llm-host.")
|
||||
return current
|
||||
for p in range(current + span, current + 100):
|
||||
if ok(p):
|
||||
say(f"Note: {label} port {busy} is in use by {owner}; using port {p} instead.")
|
||||
return p
|
||||
raise SystemExit(f"No free port near {current} for the {label}; pass --{'api' if label == 'API' else 'web'}-port.")
|
||||
|
||||
|
||||
def default_host():
|
||||
h = socket.gethostname()
|
||||
return h if "." in h else h + ".local"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue