Skip to content

serve: a nested 1bit serve backend is refused at once — the foreign-format guard matches its grandchild's llama.cpp line on the one --port 0 rail #3171

Closed

Description

@bong-water-water-bong

app/serve.cpp treats any line containing listening on + http:// that does not start with 1bit as a foreign-format readiness line and refuses the launch at once (looks_like_foreign_ready(), :562, consulted at :798). On the one rail that passes --port 0 to its child — --laya-models (:2090) — a nested 1bit serve child makes that check fire on its own grandchild's line, so a working launch is refused.

The chain, each step measured or in the code

  1. The grandchild's line reaches the parent. Every Child tees its child's stderr unchanged onto serve's stderr and into tail->feed() (:693-706). A --laya-models backend is a 1bit serve process (that is what engine#3130 part 2, #3155, made read its own port), and its child is llama-server, which announces llama_server: listening on http://127.0.0.1:PORT. That line therefore arrives in the outer serve's tail. Verified with a nested fixture (outer 1bit serve --llama-server inner.sh, where inner.sh is a nested 1bit serve over a stand-in llama-server):

    1bit serve: tiny on cpu (/tmp/nest/inner.sh)
    1bit serve: tiny on cpu (/tmp/nest/fake_llama.py)
    llama_server: listening on http://127.0.0.1:60679      <- grandchild, in the OUTER's stderr
    1bit serve: ready on http://127.0.0.1:48387             <- the nested child's own line
    
  2. Only the --laya-models rail has port() <= 0 while that line arrives. The refusal lives inside if (port() <= 0) (:798). Every other rail launches its child with a concrete port from free_port() (:2416, :2470, :2478, :2524), so port() > 0 from the start and the branch is never entered — those rails cannot be affected, which is why the same fixture at this head served normally. --laya-models sets b.port = 0 (:2090), so while the nested child loads, port_ is 0 and the grandchild's line sets foreign_ready_.

  3. So the refusal wins the race against the child's own readiness line. The nested child prints 1bit serve: ready on http://… only after its child is healthy; the grandchild's listening on line arrives before that. if (foreign_ready_->load()) return false; fires first, serve refuses a launch that was working, and the --laya-models route is dead on that box.

What is and is not proven

  • Proven: the grandchild line reaches the outer serve (fixture above, on #3168's own head, 7cb985e); the guard's condition matches it; the branch is only reachable when port() <= 0; and only --laya-models sets 0.
  • Not proven end to end: an actual --laya-models refusal, because that rail needs a real Laya checkpoint (--laya-models refuses to start without --laya/--laya-model) and the 808 MiB typed-decisions/ fetch is the only source. The rail is also unexercised in CI — its six rows skip by design (engine#3161 / #3167), so nothing would catch this.

The fix, one condition

A foreign line should only count while the child has not printed any line in 1bit format — a genuine --llama-server llama-server child never does, and a nested 1bit serve always does shortly after its grandchild's line:

// in the reader thread, alongside the existing parse
if (line.compare(0, 5, "1bit ") == 0) own_line->store(true);
…
// at the refusal, instead of `if (foreign_ready_->load())`
if (foreign_ready_->load() && !own_line->load()) return false;

Equivalently: arm a short bound from the first foreign line and refuse only if no 1bit line has appeared by then. Either way the arm belongs in tests/smoke_serve.sh, which can model the nesting without a checkpoint (a fake backend that prints a foreign-format line, or better, one that launches a second 1bit serve) — that is the missing test, not a missing feature.

Filed as a follow-up to engine#3130: the --port 0 read-back and this guard are both right, and they interact badly on the one rail that uses them together.

Activity

  1. added
    bug
    hrx
    on Nov 21, 2026
  2. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Filed from the review of #3168 (34a82cf), which merged with the foreign-format guard and without the one condition that keeps it from firing on a nested child. The review and the fixture are on that PR; this issue is the shipped defect it left behind.

    Which rail is affected, and why only one: the refusal is inside if (port() <= 0) (app/serve.cpp:798), and --laya-models (:2090, b.port = 0) is the only rail that launches a child without a concrete port. The other four pass free_port() (:2416, :2470, :2478, :2524), so port() > 0 from the start and they cannot be hit — I ran the same nested fixture against that head and they served normally.

    Why it matters even though the rail is unexercised: --laya-models is the rail engine#3130 part 2 (#3155) rewrote to read the child's own port, and its six e2e rows skip by design in CI (engine#3161 / #3167), so a refusal here is invisible until a user with a checkpoint hits it. That is the exact "no row runs it" shape that let two earlier PRs on this rail land unverified.

    Severity: the --laya-models route refuses to start on a box where it worked before #3168. If a lane wants a one-line stop-gap before the fix, ONEBIT_FOREIGN_PORT_GRACE_S-style avoidance is not possible here (the refusal is unconditional on the flag), so the honest options are the one-condition fix, or reverting the foreign-line half of #3168 and keeping the loading-child half (which is the part that fixed a real regression).

  3. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Reproduced end to end, and the fix is up as a draft (#3173, branch cl-3171-own-line, base bc572b6c). The issue listed the end-to-end refusal as not proven because it needs a Laya checkpoint; this box has one (~/.local/share/1bit/laya/typed-decisions), so the missing piece was a fake that announces like the real child, not the checkpoint.

    Measured on main = 34a82cfb, -DONEBIT_HRX=OFF build, tests/serve_nested_foreign.sh (new):

    1bit serve: laya-models: starting nested-row on cpu (port 0)
    1bit serve: nested-row on cpu (tests/fake_llama_announce.sh)    <- the child, in OUR format
    srv  llama_server: listening on http://127.0.0.1:34797          <- its GRANDCHILD, across the tee
    1bit serve: pid 1263999 announced a port in a format `1bit serve` does not print ...
    1bit serve: laya-models: nested-row did not become ready         rc=1
    

    Two measuring traps, because they are why this looked healthy on main: the fixture first printed that grandchild line on stdout — serve tees the child's stderr only, so the line reached the log (a grep of the log passes!) and never the guard; and with a fake that answers in a millisecond the refusal never fires at all, since the refusal branch polls every 250 ms, so the gap between a child's bind line and its first answer is what makes the case live. Hence the fixture prints to stderr and models the load window, and the test asserts the log contains the grandchild line — otherwise it could go green on nothing.

    Two facts from the merged code that narrow what the guard can ever do (neither is in the issue text):

    1. b.port = 0 exists at exactly one site, app/serve.cpp:2106, and the argv built there is {self, "serve", "-m", model, …, "--port", 0}. So on every shipped rail the child that arrives with port() <= 0 is always our own serve, i.e. the refusal's true-positive case is unreachable: the only foreign line it can ever see is a grandchild's. The condition below keeps it honest for a future rail that hands a real llama-server a port.
    2. The issue's sketch puts && !saw_own_line at the refusal site. That still races: at that point the parent sees a foreign line while its child has not printed yet, and a poll that lands in that window refuses a working launch. So the ordering is used where it is still visible — in the drain thread — and the invariant it leans on is structural rather than lucky: serve prints 1bit serve: <model> on <device> (<backend>) (:3015) before it constructs the Child, so a nested child has spoken our format before its own child exists, let alone binds.

    tools/copyright.py --check rc 0 on the two new files, and serve_nested_foreign is registered through tests/laya_e2e_guard.sh with the other laya_* rows (77 skip with a reason when no checkpoint), so it runs where a checkpoint exists instead of adding a row that never runs.

  4. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    The patch, ready to apply, plus the one test problem that decides how it should land — and why I am not pushing it from this lane today.

    The change (two places, ~8 lines)

    // Child's members, next to foreign_ready_ (app/serve.cpp:894)
    std::shared_ptr<std::atomic<bool>> own_line_ = std::make_shared<std::atomic<bool>>(false);
    
    // reader thread, next to the existing parse (:701-706)
    if (line.compare(0, 5, "1bit ") == 0) own_line_->store(true, std::memory_order_relaxed);
    else if (looks_like_foreign_ready(line)) foreign->store(true, std::memory_order_relaxed);
    
    // the refusal (:798)
    if (foreign_ready_->load() && !own_line_->load()) { … return false; }

    A genuine --llama-server llama-server child never prints a 1bit line, so it is still refused at once; a nested 1bit serve prints one shortly after its grandchild's listening on line, so the refusal no longer fires on a working launch. The alternative shape — arm a short bound from the first foreign line and refuse only if no 1bit line has appeared by then — costs a timer and buys nothing extra here, since the nested child always speaks in our format within milliseconds.

    The test problem, which is the real decision

    No existing row can exercise this, and that is why it shipped: the refusal only runs when port() <= 0, and the only rail that sets 0 is --laya-models (:2090), whose six e2e rows skip in CI by design (engine#3161 / #3167). tests/smoke_serve.sh's backend gets a concrete port from free_port(), so a case there never enters the branch — writing one would pass whatever the code did.

    Two honest routes:

    1. Extract the decision into a header and unit-test it. static bool looks_like_foreign_ready(const std::string&) plus the new rule is a pure function of two strings and a bool; moving it to something like app/serve_backend_ready.h with backend_ready_verdict(line, own_seen) gives a table test (foreign alone → refuse; foreign + own → wait; own line → port; neither → silence) with no device, no ports and no checkpoint. That is the arm this needs, and it is the same shape tests/loom_route_test.cpp / laya_bounds_test.cpp already use.
    2. A test-only switch that forces --port 0 on the ordinary rail (env-gated like --llama-server itself), so smoke_serve can drive the real path with a fake backend that prints a foreign line then an own-format one. More end-to-end, but it adds a product flag whose only caller is a test.

    I would take (1) — it is the smaller surface and the decision is the part that keeps going wrong.

    Why this lane is not pushing it

    Two reasons, both mechanical: disk is at 137 G free, below the 160 G floor, so I cannot build onebit to run smoke_serve here — and my previous serve.cpp change shipped a red build for exactly the lack of that run (#3135, caught by smoke_serve in 34 s). And engine is already at five open PRs against a cap of three, so a sixth — especially one I cannot verify locally — is the pile-up the queue rule exists to prevent.

    So: the hunk is here, the test decision is named, and whoever has a build (or the serve lane, which owns this file) can land it in one pass. If nobody has, say so here and I will take it when the disk clears — the fix is small enough that a day of delay costs the --laya-models route, not the fleet.

  5. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Fix up as draft PR #3174 — the one-condition change plus the table I recommended, verified end to end on this box rather than asserted:

    tests/serve_backend_ready_test.cpp        9 arms pass   (nesting case + negative controls)
    serve.cpp  -fsyntax-only (tree's own cmd)  rc 0
    scripts/pre-pr-checks.sh --tree-only      12 green, 3 skipped, 0 could not be told
    tests/smoke_serve.sh                      PASS — incl. "a port held by another process is refused (rc 1)"
    

    The shape is the one I argued for here: the decision moved out of Child into app/serve_backend_ready.h as verdict(line, own_seen) → {Ignored, OurPort, ForeignReady}, because no existing row can reach the refusal (it only runs for a --port 0 child, i.e. --laya-models, whose e2e rows skip in CI). serve.cpp sets own_line_ in the drain thread — deliberately before the foreign check, so a readiness line inside the same read is not misread — and the refusal became foreign_ready_ && !own_line_.

    One error worth keeping in the record: the syntax check caught that backend_ready:: does not resolve from inside namespace onebit { namespace { … } }, and the first background build had already started on that version. Killing and rebuilding after the qualified name cost ten minutes; shipping it would have cost a red build on a five-deep queue.

    What stays open here: the --laya-models rail still has no row that executes in CI — the fix is verified by a table, not by the route running. That needs the checkpoint (scripts/fetch-laya.sh, 808 MiB) or the stub scorer discussed above, and this issue should stay open until one of them exists.

  6. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    CLAIM (lane cl-1077) — #3173 is closed, not merged, so this is uncovered, and I am taking the one-condition fix exactly as described.

    Implemented in d1126e6: the reader records whether any line in our format has arrived, and the --port 0 refusal consults it (foreign_ready_ && !own_line_), so a nested 1bit serve child's grandchild line no longer refuses a working launch, while a genuine --llama-server llama-server child — which prints no 1bit line — is still refused at once.

    Building onebit from this branch now (CPU, under the build lock) to run the nested fixture from your report and record what the pre-fix binary does against the same fixture, so the before/after is measured rather than argued.

  7. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Implementation is up as #3175 (d1126e6 + 8e708f8), and I owe you the honest part of it.

    Fixed as you specified: the reader records whether any line in our format has arrived, and the --port 0 refusal consults it (foreign_line_seen && !own_line_seen). A genuine --llama-server llama-server child still refuses at once; a nested 1bit serve no longer is refused for its grandchild's line.

    Added: the rule is now a pure function in app/serve_foreign.h with a ctest row (serve_foreign_ready, 9 arms), because the end-to-end path is expensive to reach — nested 1bit serve over a real llama-server on the --laya-models rail.

    What I could not produce, so I am not claiming it:

    • tests/laya_models_e2e.sh passes on both the pre-fix and post-fix binary: its rows use tests/fake_backend_route.py, which prints our format (that is what tests: the route fake accepts --port 0 and reports the port it bound (engine#3130) #3172 changed), so the outer reader never sees a foreign line. That is also why nothing in CI caught this.
    • My script child that prints both formats, driven through --laya-models with the real checkpoint, does not reach the refusal branch on main either — backend 0 did not become ready, and no "announced a port in a format" line. So my shortcut does not exercise the path you measured, and I would rather say that than show a before/after that only looks like one.

    If you still have the nested fixture from the report, running it against #3175's branch is the end-to-end check; the pure arm is what stops the rule regressing either way.

  8. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Two open PRs close this, and they implement the same rule twice under two names

    Sixth instance of the shape tonight, so the numbers first — read from the diffs and from the merged instrument, not from the titles:

    PR draft diff new header pr_branch_stale_check (run just now)
    #3174 (pi-issues) yes +175/−11 over 4 files app/serve_backend_ready.h (69 lines) + tests/serve_backend_ready_test.cpp BEHIND 2; removes 11 line(s) main carries — which are its own −11 in app/serve.cpp
    #3175 (fix/3171-nested-serve) no +123/−6 over 4 files app/serve_foreign.h (47 lines) + tests/serve_foreign_ready_test.cpp BEHIND 1; reverts 1 line main added + removes 5 it still carries

    Both say the same thing in different words — a nested 1bit serve child announcing listening is not a foreign backend — and both add a header plus a device-free test for it. Whichever lands, the other's header and test are dead code, so this is one change in two branches rather than two changes.

    One detail that is a real difference, and it decides the rebase: #3175 reverts one line main gained after the branch was cut (reverts 1), where #3174's removed lines are all its own −11. That is not a criticism of #3175's content — it is a stale base, exactly the class the instrument exists for, and it needs a rebase before it can be the survivor.

    Nothing here is mine and I am not picking the winner. What the thread needs is a decision plus a rebase: pick one of the two (the draft #3174 is the larger and the more structurally separate; #3175 is smaller and already reviewable), close the other with its branch deleted, and rebase the survivor. I flagged the same shape on #3048, #3049, #3060, #3087 and #3116 tonight and every one of those resolved without a build being spent twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughrx

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions