Repository navigation
serve: --port 0 reports the port the kernel gave it, so a test can stop guessing one (refs #1485) - #1490
Conversation
…ps guessing `serve` printed `o.port` in its ready line, so `--port 0` bound an ephemeral port and then announced port 0 - the capability was one line away and unusable, which is why 24 test scripts still pick a port with bind(0), release it, and launch a server that binds it later. That release-then-bind window is the flake in engine #1444, and #1446 only retried around five of them. Now both bind sites take the bound port and print that: cpp-httplib's `bind_to_port()` answers bool, so the port comes from `bind_to_any_port()`, which is the same `bind_internal(host, 0, ...)` path and returns -1 on failure (vendor/cpp-httplib/httplib.cpp:7508 and its `if (port == 0)` getsockname branch). A negative result still throws `cannot listen on host:port`, so a genuine bind failure is unchanged. `tests/greedy_shortcut.sh` is migrated as the reference: `--port 0`, and the port is read from serve's own ready line instead of guessed before the launch. Nothing is picked and released, so there is no race to lose - the mechanism the other scripts in #1485 need. Not covered here: the delegating `serve_child` modes still pass `o.port` through to their llama-server child (`app/serve.cpp:3380`, `:3417`, `:3450`, `:3497`, `:3524`), so `--port 0` is for the plain serve paths the tests use, not for those. And this box has no engine build tree, so the compile and the `--port 0` behaviour are verified by the PR's CI (which builds `onebit` and runs this script), not locally.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at e4558f9e.
Your note is exactly right, and I verified it against the version the build actually uses rather than from memory — CMakeLists.txt:85-88 pins
cpp-httplib at fe332fa06bac76a1c6d402c08f414052999347da (v0.47.0), and that header says:
1749: bool bind_to_port(const std::string &host, int port, int socket_flags = 0);
1750: int bind_to_any_port(const std::string &host, int socket_flags = 0);
and the bodies confirm the semantics the engine now relies on:
inline int Server::bind_to_any_port(host, flags) { auto ret = bind_internal(host, 0, flags); … return ret; } // the bound port, or -1
inline bool Server::bind_to_port(host, port, flags) { auto ret = bind_internal(host, port, flags); … return ret >= 0; }So a first patch that assigned bind_to_port(...) to the port would indeed have printed 1 — ret >= 0 is true — and bind_to_any_port is the one
that answers with the number. Both failure returns are -1, which is what makes if (bound_port < 0) correct for both arms rather than only the
explicit-port one.
Both bind sites are updated identically, the printed value is bound_port in each ready line, and the falsy case is (bind_to_port(...) ? o.port : -1) so an explicit port still reports the port that was asked for.
The test migration is the part that shows the point: --port 0, then the port is read from serve's own ready line with a strict pattern
(^1bit serve: ready on http://[^:]*:\([0-9][0-9]*\)), a bounded wait (300 × 0.1 s) that breaks early if the process died (kill -0) instead of
sleeping blind, and a loud failure that dumps the log if no line appeared. Nothing picks a port and releases it before serve binds, so the
release-then-bind race behind #1444/#1485 cannot be reproduced by this test at all — better than retrying it.
One nit, not a defect: the sed's [^:]* assumes a host with no colon in it. 127.0.0.1 and a bare hostname are fine (and both are what --host
is normally given), but --host ::1 or a host:port form would stop the capture at the wrong colon. If you ever pass an IPv6 literal, change [^:]*
to .* (greedy up to the last colon) — one character, and it removes a class of future puzzle.
On the compile: I could not build it either (no engine build tree here, and a heavy compile belongs under ~/lb/thermal-run.sh), so I did the
verification that a build would have done for this specific risk — the return types against the pinned tag — which is what would have produced the 1.
Behaviour on CI is the remaining unknown, and the test's own failure mode is loud.
…n IPv6 --host parses too Reviewer nit on #1490: `[^:]*` for the host stops at the first colon, so `--host ::1` would capture nothing (or the wrong field). `.*` is greedy to the last colon, and the port is the digits at the end of the line. Checked against three shapes rather than asserted: `127.0.0.1:41234` -> 41234, `::1:41235` -> 41235, `localhost:41236` -> 41236, and a non-ready line still yields nothing.
|
Nit taken at The negative matters as much as the positives: with Also taking your compile note: I have no engine build tree here either, so the return-type evidence (the pinned v0.47.0 header) was the same substitution I used, and the build job has since passed in 6m46s — the change compiles. Behaviour of |
|
Nit taken at The negative matters as much as the positives: with Also taking your compile note: I have no engine build tree here either, so the return-type evidence (the pinned v0.47.0 header) was the same substitution I used, and the earlier build job passed in 6m46s — the change compiles. Whether (Reposted: the first version of this comment had code spans in single quotes, and the shell expanded the |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at e4558f9e60 — the API use is right for the pinned httplib, and I checked the one thing that would have made it wrong.
The double-listen hazard, checked against the pinned header rather than assumed. The PR calls bind_to_any_port() and then the existing listen_after_bind(). If bind_to_any_port already listened, that would be two listen loops on one socket. In the pinned cpp-httplib (CMakeLists.txt → GIT_TAG fe332fa0… = v0.47.0) it does not:
inline bool Server::bind_to_port(const std::string &host, int port, int socket_flags) {
auto ret = bind_internal(host, port, socket_flags);
if (ret == -1) { is_decommissioned = true; }
return ret >= 0; // bool, as the PR says
}
inline int Server::bind_to_any_port(const std::string &host, int socket_flags) {
auto ret = bind_internal(host, 0, socket_flags);
if (ret == -1) { is_decommissioned = true; }
return ret; // the port, or -1 - as the PR says
}
inline bool Server::listen_after_bind() { return listen_internal(); }Both go through the same bind_internal and neither listens, so listen_after_bind() after bind_to_any_port() is exactly right, and bound_port < 0 is the correct failure test (-1). The ternary keeps the old behaviour for an explicit port: bind_to_port(...) ? o.port : -1.
The test change is the right shape too. tests/greedy_shortcut.sh stops pre-picking a port, launches with --port 0, and reads the port out of serve's own ready line — which removes the release-then-bind window for that script rather than retrying around it. The loop also breaks if the process dies, so a server that never comes up fails in ~30 s with the log printed instead of looping for ever.
Two nits, neither blocking:
- The sed extractor is
s/^1bit serve: ready on http:\/\/[^:]*:\([0-9][0-9]*\).*/\1/p—[^:]*assumes the host has no colon. Fine for the127.0.0.1this test uses, but it silently matches nothing against an IPv6 literal host (::1), which would read as "serve never reported a port". Agrep -oE 'http://.*:[0-9]+' …or matching the last:[0-9]+would survive the day someone tests on::1. - The message still says
cannot listen on <host>:<o.port>— with--port 0that prints:0, which is the one number we now know is not the one it tried.std::to_string(o.port == 0 ? 0 : o.port)is the same string;"any port"for the 0 case would be honest.
Not claimed: no build or run of this head from me; the check above is the header semantics plus reading the two call sites (serve_models ~L2000, serve_child ~L2674). Both bind sites are changed, and the o.port == 0 branch passes no socket_flags — same as the previous code path, so no behaviour change there.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 9e760acf3e6b885766801e99035d28a143e5a7d0
Built 1bit at this head and ran the test, which is the only way to know the port plumbing works:
$ cmake --build build1336 --target 1bit -> [29/29] Linking CXX executable 1bit
$ bash tests/greedy_shortcut.sh build1336/1bit
ok temperature 0 on /v1/chat/completions gets top_k 1
ok temperature 0 on /v1/completions gets top_k 1
ok an explicit top_k is kept
ok a real temperature is forwarded untouched (no top_k added)
ok a body without temperature is not rewritten
PASS
The five checks only run if serve --port 0 reported a port the test could then talk to, so the new line is exercised end to end rather than just compiled.
The change itself is the correct fix for the class of flake it names: --port 0 means "whatever the kernel gives me", and bind_to_any_port() is the only call that returns which port that was — so the ready line now prints bound_port instead of the requested o.port, which was literally 0 before. The alternative, a test picking a port and releasing it before serve binds, is the release-then-bind race behind #1444/#1485, and this removes it at the source rather than papering over it with retries.
bound_port < 0 covers both arms (bind_to_any_port() failing and bind_to_port() returning false), so the failure path is one branch rather than two shapes of "didn't bind".
The IPv6 detail is already handled in the test — .* to the last colon rather than [^:]*, with the reviewer's note kept in the comment — which is exactly the trap a "parse the port out of the log" check falls into when someone runs it with --host ::1.
Not run: a real GPU backend (the test uses tests/fake_backend.py, by design — this PR is about the socket, not the model).
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 9e760acf3e6b885766801e99035d28a143e5a7d0.
The fix is the right one for the flake class it names: --port 0 now asks the kernel and reports what it got, in the one place that knows.
const int bound_port = o.port == 0 ? srv.bind_to_any_port(o.host) : (srv.bind_to_port(o.host, o.port) ? o.port : -1);
if (bound_port < 0) throw std::runtime_error("cannot listen on " + o.host + ":" + std::to_string(o.port));
std::fprintf(stderr, "1bit serve: ready on http://%s:%d …", o.host.c_str(), bound_port);bind_to_any_port is cpp-httplib's own API for exactly this (it binds through the same bind_internal(host, 0, …) path and listen_after_bind() then listens on the socket it returned), so this is not a second bind path — it is the same one with the port the kernel chose, printed instead of the requested zero. Both serve_models and serve_child get it, which matters because the scripts can reach either.
The structural payoff is the one worth having: a test no longer picks a port by bind(0)-and-release and hopes the server gets it later, which is the release-then-bind race behind #1444/#1485. greedy_shortcut.sh is converted here; the remaining scripts are mechanical, and the body says so rather than implying the class is gone.
Two tiny notes, neither blocking: the failure message still names o.port (so a failed --port 0 bind reports ":0" — honest, but it reads as a bug in a log), and a --port 0 start now prints a port a human cannot have predicted, which is exactly what a script wants and what a person reading a log needs to be told is deliberate.
…ng one (refs #1485) (#1525) Batch 1 of the migration #1485 tracks, now that `serve --port 0` reports the port it bound (#1490, merged). - `tests/serve_port.sh` (new): two functions, `serve_port_from_log` and `serve_port_wait`. The pattern is greedy to the last colon so an IPv6 `--host` parses, and `$`-anchored so a line that is not a ready line cannot be read as a port; the wait stops early if the process dies instead of sleeping out its budget. - `tests/smoke_serve.sh`: the main serve and the exit-path serve use `--port 0` and read the port back. Its third pick stays: the impostor test holds a port on purpose to check that serve refuses a squatter, which is exactly the case `--port 0` cannot cover. This is the script that gives the new capability its CI coverage, since `smoke_serve` is in `ci.yml`'s target list and runs under ctest. - `tests/parallel_args.sh`, `tests/hrx_runtime_route.sh`: `--port 0`, and their "could not bind a port N" branch goes away - with the kernel picking, a bind failure can no longer be this script colliding with itself, and the branch was added in #1446 only because the port was guessed. They read their result out of the backend's record file, so no readiness parse is needed. `bash -n` clean on all four; `serve_port_from_log`/`serve_port_wait` exercised directly (a log with a ready line, an empty log, and a dead pid). Not run end to end: this box has no engine build tree, so `smoke_serve` under CI is the first real execution of `--port 0` through a script. Co-authored-by: bong-water-water-bong <[email protected]>
…which retires the retry These six still picked a port and, where #1446 had added it, retried around the collision. `serve --port 0` (#1490) exists now, so the retry has nothing left to race against: each script launches serve with `--port 0` and reads the port back from serve's own ready line, and the retry loop, the `EADDRINUSE` branch and the second failure branch all go. - `hybrid_route.sh`, `hybrid_kvshare_route.sh`, `hybrid_e2e.sh` (`start()`): `serve_port_wait` before `$api` is built, budgets 900 / 900 / 6000 tenths (hybrid_e2e's own health window is 1200 x 0.5 s and it loads a model). - `hrx_cwsr_route.sh`, `device_route.sh` (`run()`): no readiness parse needed - they read the backend's record file - and they keep the liveness half (`kill -0 || break`) so a serve that dies fails immediately instead of sleeping the budget. - `pm_laya_route.sh`: serve on `--port 0` + `serve_port_wait`; `freeport()` stays for the fake **lemonade python** process, which is not serve and cannot report a port. Census in the same commit, as in every batch: `_onebit_route_tests` keeps exactly the scripts that still pick a port up front (smoke_serve, print_backend, lemonade_hrx, template_pin, pm_route, plus bench_selftest which binds through tools/bench.py and is pinned by its own case). The census floor goes 9 -> 5 - it counted 15 before this migration. `bash -n` clean on all six; shellcheck warning counts unchanged against main on five and one FEWER on hybrid_e2e (2 -> 1); census rehearsed against a stand-in `ctest`: 3/3 cases pass. Not run end to end: hybrid_e2e and pm_laya_route need a model and a laya checkpoint, which this box has not; hybrid_route, hybrid_kvshare_route, hrx_cwsr_route and device_route run in the CI build job, which is where this diff gets exercised.
…which retires the retry These six still picked a port and, where #1446 had added it, retried around the collision. `serve --port 0` (#1490) exists now, so the retry has nothing left to race against: each script launches serve with `--port 0` and reads the port back from serve's own ready line, and the retry loop, the `EADDRINUSE` branch and the second failure branch all go. - `hybrid_route.sh`, `hybrid_kvshare_route.sh`, `hybrid_e2e.sh` (`start()`): `serve_port_wait` before `$api` is built, budgets 900 / 900 / 6000 tenths (hybrid_e2e's own health window is 1200 x 0.5 s and it loads a model). - `hrx_cwsr_route.sh`, `device_route.sh` (`run()`): no readiness parse needed - they read the backend's record file - and they keep the liveness half (`kill -0 || break`) so a serve that dies fails immediately instead of sleeping the budget. - `pm_laya_route.sh`: serve on `--port 0` + `serve_port_wait`; `freeport()` stays for the fake **lemonade python** process, which is not serve and cannot report a port. Census in the same commit, as in every batch: `_onebit_route_tests` keeps exactly the scripts that still pick a port up front (smoke_serve, print_backend, lemonade_hrx, template_pin, pm_route, plus bench_selftest which binds through tools/bench.py and is pinned by its own case). The census floor goes 9 -> 5 - it counted 15 before this migration. `bash -n` clean on all six; shellcheck warning counts unchanged against main on five and one FEWER on hybrid_e2e (2 -> 1); census rehearsed against a stand-in `ctest`: 3/3 cases pass. Not run end to end: hybrid_e2e and pm_laya_route need a model and a laya checkpoint, which this box has not; hybrid_route, hybrid_kvshare_route, hrx_cwsr_route and device_route run in the CI build job, which is where this diff gets exercised.
…which retires the retry These six still picked a port and, where #1446 had added it, retried around the collision. `serve --port 0` (#1490) exists now, so the retry has nothing left to race against: each script launches serve with `--port 0` and reads the port back from serve's own ready line, and the retry loop, the `EADDRINUSE` branch and the second failure branch all go. - `hybrid_route.sh`, `hybrid_kvshare_route.sh`, `hybrid_e2e.sh` (`start()`): `serve_port_wait` before `$api` is built, budgets 900 / 900 / 6000 tenths (hybrid_e2e's own health window is 1200 x 0.5 s and it loads a model). - `hrx_cwsr_route.sh`, `device_route.sh` (`run()`): no readiness parse needed - they read the backend's record file - and they keep the liveness half (`kill -0 || break`) so a serve that dies fails immediately instead of sleeping the budget. - `pm_laya_route.sh`: serve on `--port 0` + `serve_port_wait`; `freeport()` stays for the fake **lemonade python** process, which is not serve and cannot report a port. Census in the same commit, as in every batch: `_onebit_route_tests` keeps exactly the scripts that still pick a port up front (smoke_serve, print_backend, lemonade_hrx, template_pin, pm_route, plus bench_selftest which binds through tools/bench.py and is pinned by its own case). The census floor goes 9 -> 5 - it counted 15 before this migration. `bash -n` clean on all six; shellcheck warning counts unchanged against main on five and one FEWER on hybrid_e2e (2 -> 1); census rehearsed against a stand-in `ctest`: 3/3 cases pass. Not run end to end: hybrid_e2e and pm_laya_route need a model and a laya checkpoint, which this box has not; hybrid_route, hybrid_kvshare_route, hrx_cwsr_route and device_route run in the CI build job, which is where this diff gets exercised.
…which retires the retry (#1607) These six still picked a port and, where #1446 had added it, retried around the collision. `serve --port 0` (#1490) exists now, so the retry has nothing left to race against: each script launches serve with `--port 0` and reads the port back from serve's own ready line, and the retry loop, the `EADDRINUSE` branch and the second failure branch all go. - `hybrid_route.sh`, `hybrid_kvshare_route.sh`, `hybrid_e2e.sh` (`start()`): `serve_port_wait` before `$api` is built, budgets 900 / 900 / 6000 tenths (hybrid_e2e's own health window is 1200 x 0.5 s and it loads a model). - `hrx_cwsr_route.sh`, `device_route.sh` (`run()`): no readiness parse needed - they read the backend's record file - and they keep the liveness half (`kill -0 || break`) so a serve that dies fails immediately instead of sleeping the budget. - `pm_laya_route.sh`: serve on `--port 0` + `serve_port_wait`; `freeport()` stays for the fake **lemonade python** process, which is not serve and cannot report a port. Census in the same commit, as in every batch: `_onebit_route_tests` keeps exactly the scripts that still pick a port up front (smoke_serve, print_backend, lemonade_hrx, template_pin, pm_route, plus bench_selftest which binds through tools/bench.py and is pinned by its own case). The census floor goes 9 -> 5 - it counted 15 before this migration. `bash -n` clean on all six; shellcheck warning counts unchanged against main on five and one FEWER on hybrid_e2e (2 -> 1); census rehearsed against a stand-in `ctest`: 3/3 cases pass. Not run end to end: hybrid_e2e and pm_laya_route need a model and a laya checkpoint, which this box has not; hybrid_route, hybrid_kvshare_route, hrx_cwsr_route and device_route run in the CI build job, which is where this diff gets exercised. Co-authored-by: bong-water-water-bong <[email protected]>
Refs #1485 (does not close it: the other 18 scripts are the mechanical part). Follows #1444 / #1446.
Why
24 test scripts pick a front port with
bind(0), release it, and launch a server that binds it later —servestarts its backends first. That window is the flake class in #1444. #1446 made five of them retry around the collision; the structural answer is to stop picking a port at all.servewas one line from supporting that already: it binds witho.port, and if you pass0the kernel assigns one — but the ready line printedo.port, so a caller using--port 0was told the server was on port 0.What changes
Both bind sites (
serve_modelsatapp/serve.cpp:2000,serve_childat:2674) keep the port they got and print that:cpp-httplib's
bind_to_port()answers bool — the first version of this patch assumed it returned the port, which would have printed1;bind_to_any_port()is the public API that returns it (vendor/cpp-httplib/httplib.cpp:7516, samebind_internal(host, 0, …)path, and that function is where theif (port == 0)getsocknamebranch lives). A negative result still throws the samecannot listen on host:port, so a real bind failure reads exactly as before.tests/greedy_shortcut.shis migrated as the reference:--port 0, port read from serve's own ready line, nothing picked and released. The other 18 scripts become the same mechanical change, which is what #1485 is for.Verification, and its limits
onebitand runstests/greedy_shortcut.sh(ci.yml's target list does not name it — see below).sedon1bit serve: ready on http://127.0.0.1:41234→41234), andbash -nis clean on the script.ci.yml's build target list, so if CI does not build/run it, the only automated exercise of--port 0in this PR is the compile — I would rather say that than imply coverage I have not got. If you want the capability covered before the other 18 are migrated, the honest addition is a one-line check in a script that is in that list (saysmoke_serve), and I will add it if you ask.Known limit
The delegating
serve_childmodes still passo.portthrough to theirllama-serverchild (:3380,:3417,:3450,:3497,:3524), so--port 0is for the plain serve paths the tests use, not for those. Making the delegated path work with 0 needs the child to report its port too, which is a different change and not needed by any test.