Never move a saved port on update
`llmhost update` stopped the services and checked the ports before the old processes had released them (launchctl bootout returns early), so the API hopped to the next free port and broke configured clients. Setup now waits for our own ports to close, and treats saved ports as pinned: if another program holds one it errors instead of moving. Only a first install auto-picks a free port. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QaE1hpvyZTJixEbUUMj9Ca
This commit is contained in:
parent
249c693e6d
commit
7eb72d1d79
2 changed files with 43 additions and 22 deletions
|
|
@ -99,14 +99,22 @@ def cmd_setup(a):
|
|||
|
||||
# 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.)
|
||||
# MLX needs two ports: the proxy, and mlx_lm.server on the next one.
|
||||
api_span = 2 if p["backend"] == "mlx" else 1
|
||||
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())
|
||||
# Stopping returns before the old processes have let go of their ports (launchd
|
||||
# especially), so wait for our own ports to close before checking what's free.
|
||||
if old.get("api_port"):
|
||||
ours = list(range(old["api_port"], old["api_port"] + api_span)) + [old.get("web_port", 8080)]
|
||||
wait_ports_free(ours, timeout=30)
|
||||
# Ports saved by an earlier setup are pinned: clients are configured with them, so a
|
||||
# busy saved port is an error, never a reason to move. Only a first install auto-picks.
|
||||
api_port = choose_port("API", a.api_port, old.get("api_port", 8000), api_span, a.no_start,
|
||||
avoid=set(), pinned="api_port" in old)
|
||||
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)))
|
||||
avoid=set(range(api_port, api_port + api_span)), pinned="web_port" in old)
|
||||
|
||||
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)
|
||||
|
|
@ -168,26 +176,35 @@ def port_owner(port):
|
|||
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)
|
||||
def wait_ports_free(ports, timeout):
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline and not all(port_free(p) for p in ports):
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
def choose_port(label, explicit, current, span, no_start, avoid, pinned=False):
|
||||
"""Use the requested or saved port. Only an unpinned default moves to a free port."""
|
||||
def busy_in(p):
|
||||
return next((q for q in range(p, p + span) if q in avoid or not port_free(q)), None)
|
||||
|
||||
def owner(q):
|
||||
return "llm-host's API" if q in avoid else port_owner(q)
|
||||
flag = "--api-port" if label == "API" else "--web-port"
|
||||
want = explicit or current
|
||||
busy = busy_in(want)
|
||||
if busy is None:
|
||||
return want
|
||||
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.")
|
||||
say(f"Note: {label} port {busy} is in use by {owner(busy)}; it must be free before you start llm-host.")
|
||||
return want
|
||||
if explicit or pinned:
|
||||
why = "" if explicit else f" (llm-host is configured to use it; pass {flag} to change it)"
|
||||
raise SystemExit(f"{label} port {busy} is in use by {owner(busy)}{why}.")
|
||||
for p in range(want + span, want + 100):
|
||||
if busy_in(p) is None:
|
||||
say(f"Note: {label} port {busy} is in use by {owner(busy)}; 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.")
|
||||
raise SystemExit(f"No free port near {want} for the {label}; pass {flag}.")
|
||||
|
||||
|
||||
def default_host():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue