diff --git a/README.md b/README.md index 8fb313b..1816b36 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,10 @@ Pass setup options after `--`: /bin/bash -c "$(curl -fsSL https://git.neilbantoc.dev/pub/llm-host/raw/branch/main/install.sh)" -- --yes --default ornith-1.5-35b ``` +Ports: the API defaults to 8000 and the dashboard to 8080. On a first install a busy default moves to the +next free port; after that the ports are saved and never move on their own (clients are configured with them). +Change them with `llmhost setup --api-port --web-port `. On a Mac, MLX also uses the API port + 1. + Preview what a machine would get without installing anything (needs only Python 3.11+): ```bash diff --git a/llmhost/cli.py b/llmhost/cli.py index 6791229..4eaba89 100644 --- a/llmhost/cli.py +++ b/llmhost/cli.py @@ -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():