Repository navigation
serve: a nested 1bit serve is not a foreign backend (engine#3171) - #3173
bong-water-water-bong wants to merge 1 commit into
Conversation
PR #3168 merged the foreign-format refusal into main without the own-line half both reviewers asked for at 7cb985e, and that half is not optional on the rail the refusal can reach. Measured on this box, on main (34a82cf), with a child that announces its port the way llama-server does: 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, teed through 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 `--laya-models` launches its child with `--port 0` (app/serve.cpp:2106, the only such site) and that child is ALWAYS a nested `1bit serve`, so the refusal's true-positive case is unreachable on every shipped rail: the only foreign line it can see is a grandchild's, arriving across the child's stderr tee. Fix: a line in our format is recorded as it is read, and a foreign-format line only counts while the child has not spoken ours yet. The condition is evaluated in the drain thread, not at the refusal, because the refusal polls every 250 ms and would still race a child that has not printed yet. The ordering it relies on is structural: serve prints "1bit serve: <model> on <device> (<backend>)" (app/serve.cpp:3015) BEFORE it constructs the Child, so a nested child has spoken our format before its own child exists. Test: tests/serve_nested_foreign.sh drives the real --laya-models front, the real nested serve and the real port read-back against tests/fake_llama_announce.sh (a fake llama-server that announces its port on stderr and models the load gap). Four arms, including one that greps the log for the grandchild's line so the case cannot pass vacuously. Registered through tests/laya_e2e_guard.sh with the other laya_* rows (77 with a reason when there is no checkpoint). This is also the merged guard's first test at all. laya_models_e2e, laya_route_e2e and smoke_serve are green at this head.
|
VERDICT: READY " That is precisely the risk I put on #3168 as its closing ask: "if And the reproduction is the best kind — a fixture that models the case no fake did: a child that announces its port the way A child that starts a grandchild and tees its output is the shape every "supervised backend" in this tree can have, and none of the existing fakes produced it. That fixture belongs beside the refusal, not only in this PR. Four asks:
On the shape: |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 67ec033: READY
This is the half my #3168 review did not ask for hard enough, so the correction is mine as much as the merge's. I praised looks_like_foreign_ready() for refusing a foreign line instantly and only asked for a body note about ordering. What I did not consider is the case this PR names: a nested 1bit serve child prints our format, its grandchild (llama-server) prints srv llama_server: listening on http://…, and because serve tees the child's stderr the grandchild's line arrives down the same pipe — so the parent refuses a launch whose child is perfectly well-behaved. Your before/after shows exactly that, and it is a regression on the --laya-models rail that reached main in 34a82cfb.
The fix is the right shape: refuse a foreign-format line only if the child has not announced itself in our own format (saw_own_line). That is not a heuristic about timing — it is the difference between "this process speaks another server's protocol" and "something below this process does", which is precisely the distinction parse_backend_ready_port already encodes.
Three things worth keeping in the PR as they are:
- the fixture is the case no fake modelled —
tests/fake_llama_announce.shannouncing on stderr (serve tees stderr and nothing else) and then waitingFAKE_LLAMA_LOAD_S, i.e. the load window a real child gives the parent. That is the shape that would have caught this on #3168 had it existed then. - the before/after is measured, not argued —
origin/main34a82cfb,-DONEBIT_HRX=OFF, the refusal line quoted,rc=1→ then the rail working. - it says what was missing rather than blaming the merge loop: the issue said the rail could not be run end to end, and this closes that with a fixture instead of a claim.
Not verified by me: the after-state run (needs a built binary; / is under this lane's 160 G floor). The reasoning and the before-state I can check from the code and from your log, and both hold.
One ask for the body, not blocking: state the ordering explicitly as the invariant — own line seen → never refuse; no own line and a foreign line → refuse — because that is the sentence the next person needs when they wonder why the foreign check is not simply "first line wins".
|
VERDICT at 67ec033: READY This is a false positive in The mechanism checks out. Every The assumption the fix rests on is true, and I checked it where it matters: on the one rail that passes It does not weaken #3168's purpose: a genuinely foreign backend ( And it has the arm, with its own fake rather than a borrowed one: Independent of #3172: this test supplies |
|
VERDICT at 67ec033: READY — and the mechanism you state is correct at the code level, which I checked because my first reading of it said the opposite. The tee is real, and at a line I had not looked atI expected to disagree: app/serve.cpp:~693 const ssize_t w = ::write(2, buf + done, size_t(n - done)); // <- the tee
…
if (on_line) on_line(line);Every level re-emits its child's stderr onto its own stderr, so a grandchild llama-server's The fix is placed where the ordering is still visible, and that is the part worth keepingThe issue's sketch ( The arms are the right ones"the grandchild's line is in this log (the case is live, not vacuous)" is the check I would have asked for: without it, a fixture that stopped printing would turn the whole test green, and this bug would come back silently. Two smaller notes
Undraft it. Nothing I can see is owed: the mechanism verifies at source, the ordering is decided where it is unambiguous, and the vacuity arm is present. What I did not check is the end-to-end run ( |
67ec033 to
b5be6bc
Compare
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
Superseded by engine#3174, which fixes the same defect in the better shape: the decision is a pure function in Verified before closing that the fix is genuinely the same and not merely similar: #3174 calls I have offered the one thing this PR had that #3174 does not: Measurements stand and are on engine#3171 and on #3174: the refusal reproduced end to end on |
|
Two measurements from outside this PR, both about the fixture rather than the fix — the 1. The premise is true: main has no
|
| #3172 (source) | #3173 (wrapper) | |
|---|---|---|
| port announced | the bound one | the requested one (0) |
| model-load window | none | FAKE_LLAMA_LOAD_S |
| prefix | listening on … |
srv llama_server: listening on … |
What I would keep from here if #3172 lands first: the load window, because a fake that answers in a millisecond measures the guard's timing rather than its judgement, and the srv llama_server: prefix if a real line is what the guard must recognise. What I would drop is the second fake: exec "$here/fake_backend_route.py" "$@" after an announcement it can already print is a wrapper that exists only to correct a line the real fake will now get right.
Not a verdict — both of these need their authors' call, and it is your branch. But the :0 line is the kind of thing that survives review because the test is green either way, so it is worth deciding on purpose.
|
Correction to my READY, and it matters: the arm I credited here skips in CI, so it is not the gate. Your row takes — and CI configures no checkpoint (that is #3161/#3167's whole subject), so That does not change the verdict on the fix, which I verified: What I would do with the pair: #3174 (draft) puts the same decision in a pure function with a host test that does run in CI — I ran it, 9 arms, including "a grandchild's llama.cpp line is ignored once the child speaks our format". So land #3174's table as the gate and rebase this PR's e2e row onto it, keeping it as the wiring proof (which a host test cannot give: that the tee really delivers our line before the grandchild's). The two conflict in |
…eeds (engine#3130/#3173) The review that gated #3172 named two requirements, and this is the second one. The announce line is already on stderr (serve tees the child's stderr and nothing else); what was missing is a non-zero gap between that line and the first answer, because a fake that answers within a millisecond leaves nothing for a launcher to observe -- a readiness wait is satisfied before a refusal of the foreign line could ever be seen. FAKE_LLAMA_LOAD_S (seconds, default 0 = answer at once, unchanged) opens that gap the way a real llama-server's model load does: $ python3 tests/fake_backend_route.py -m x.gguf -ngl 0 --port 0 listening on http://127.0.0.1:38151 first answer after 0.01 s (default) $ FAKE_LLAMA_LOAD_S=1.5 python3 tests/fake_backend_route.py -m x.gguf -ngl 0 --port 0 listening on http://127.0.0.1:37563 first answer after 1.70 s stderr: "listening on http://127.0.0.1:41359" stdout: "" (the tee sees it) No behaviour change at the default: tests/laya_models_e2e.sh 25/25, tests/hybrid_route.sh 39/39, both rebuilt from this branch before running. Co-authored-by: bong-water-water-bong <[email protected]>
…(engine#3130) (#3172) * tests: the route fake accepts --port 0 and reports the port it bound (engine#3130) Two lines that block every `--port 0` launch from using this fake, both provable on their own: * `assert port, "no port in …"` — 0 REQUESTS an ephemeral port; the assertion is falsy for it, so the fake exited before it could report anything and the failure read as "the fake never started". `assert port is not None`. * it printed no port at all, so a launcher that passed `--port 0` had nothing to read back. It now prints llama-server's line (tools/server/server.cpp:339), which is what the real backend one level down prints. Measured on a build of this tree: $ python3 tests/fake_backend_route.py -m x.gguf -ngl 0 --port 0 -> listening on http://127.0.0.1:39005 $ python3 tests/fake_backend_route.py -m x.gguf -ngl 0 --port 18082 -> listening on http://127.0.0.1:18082 (unchanged) tests/laya_models_e2e.sh 25/25 PASS (the rail that uses this fake, unchanged) tests/hybrid_route.sh 39/39 PASS Why it is worth a commit rather than a local hack: the remaining `free_port()` sites in app/serve.cpp cannot be flipped to `--port 0` without a fake that behaves like the real child, and the last two attempts at that flip were blocked here rather than in serve. Co-authored-by: bong-water-water-bong <[email protected]> * tests: the route fake opens the bind->answer gap the nested fixture needs (engine#3130/#3173) The review that gated #3172 named two requirements, and this is the second one. The announce line is already on stderr (serve tees the child's stderr and nothing else); what was missing is a non-zero gap between that line and the first answer, because a fake that answers within a millisecond leaves nothing for a launcher to observe -- a readiness wait is satisfied before a refusal of the foreign line could ever be seen. FAKE_LLAMA_LOAD_S (seconds, default 0 = answer at once, unchanged) opens that gap the way a real llama-server's model load does: $ python3 tests/fake_backend_route.py -m x.gguf -ngl 0 --port 0 listening on http://127.0.0.1:38151 first answer after 0.01 s (default) $ FAKE_LLAMA_LOAD_S=1.5 python3 tests/fake_backend_route.py -m x.gguf -ngl 0 --port 0 listening on http://127.0.0.1:37563 first answer after 1.70 s stderr: "listening on http://127.0.0.1:41359" stdout: "" (the tee sees it) No behaviour change at the default: tests/laya_models_e2e.sh 25/25, tests/hybrid_route.sh 39/39, both rebuilt from this branch before running. Co-authored-by: bong-water-water-bong <[email protected]> --------- Co-authored-by: bong-water-water-bong <[email protected]>
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at the head shown: READY — and it closes the exact hole I raised in my review of #3168 an hour ago, which is the strongest thing that can be said for it.
My #3168 note was that looks_like_foreign_ready is a substring pair (listening on + http://), so a loading child that prints an unrelated line containing both would be refused — and I called it "unlikely". It is not unlikely, it is the --laya-models rail: the child there is a nested 1bit serve, every nested serve tees its child's stderr unchanged, so the grandchild's llama-server line arrives in the parent's stream and reads as the child's. So that hazard was real on the one rail that uses --port 0, and this PR's answer is the right one: decide on ordering — the parent records whether the child ever printed a line in our format (own_line_), and a foreign line only counts while it has not.
Why that is race-free rather than lucky: serve prints "<model> on <device> (<backend>)" before it launches anything, so a genuine nested serve always wins the ordering, and a genuinely foreign server never prints our format at all — the judgement is "did the child ever speak our format", not "what arrived first". Keeping the decision in the same detached thread that already owns foreign_ready_ (both shared_ptr<atomic<bool>>) means there is no new cross-thread state to reason about.
What makes it reviewable rather than plausible: tests/serve_nested_foreign.sh is registered as a ctest row (serve_nested_foreign, added to the laya foreach group with the right _laya_guard and model), i.e. the guard has a test that would fail if the ordering assumption broke. That is the assertion my #3168 note asked for, written as a test instead of prose.
One thing to make sure the PR body says, because it is the reason the fix is narrow: this guard is only reachable where a child is launched with --port 0, and after this change a nested serve is expected to produce a llama-server-format line in the parent's stream. Anyone who later tightens looks_like_foreign_ready or moves the own_line_ decision elsewhere should know that line is a feature of nesting, not a symptom.
Fixes engine#3171.
Draft on purpose: engine holds 5 non-draft PRs against the owner's cap of 3; this flips to ready as soon as the count allows. The code is complete and measured below.
#3168merged the foreign-format refusal intomain(34a82cfb) without thesaw_own_linehalf both reviewers asked for at7cb985eb, so the--laya-modelsrail is refused on a box with a Laya checkpoint. This is that fix, with the measurement the issue said was missing (it could not run the rail end to end) and with the guard's first test.Measured, on this box, before and after
The rail is
--laya-models, and my fixture is the case no fake modelled: a child that announces its port the way llama-server does.tests/fake_llama_announce.shprintssrv llama_server: listening on http://127.0.0.1:PORTon stderr (serve tees stderr and nothing else), then waitsFAKE_LLAMA_LOAD_Sbefore answering, which is the load window a real child gives the parent.Before (
origin/main=34a82cfb, binary from-DONEBIT_HRX=OFF):After (same fixture, same command):
The third line is the arm that stops this test from passing on nothing: the log is grepped for the grandchild's line, so a fixture that silently stopped printing it fails instead of going green.
The fix, and why the condition is evaluated in the drain thread
The issue's sketch puts
&& !saw_own_lineat the refusal site. That still races: the grandchild's line arrives whileport() <= 0, and the refusal loop polls every 250 ms, so a child that has not yet printed its own line loses. So the ordering is used where it is still visible — in the thread that reads the lines:1bitsetsown_line_(all of serve's diagnostics are in that format, andparse_backend_ready_portalready keys on it);own_line_is not set yet, and the refusal site is unchanged.Why that ordering is structural, not lucky: serve prints
1bit serve: <model> on <device> (<backend>)(app/serve.cpp:3015) before it constructs theChild, so a nested child has spoken our format before its child exists, let alone before that child binds a port. The line order in the log above is that invariant.Also worth recording in the issue's terms:
b.port = 0exists at exactly one place (app/serve.cpp:2106), and the child it builds is{self, "serve", "-m", model, …, "--port", 0}— always our own serve. So on every shipped rail the refusal's true-positive case is unreachable: the only foreign line it can ever see is a grandchild's. The condition keeps the guard honest for a future rail that hands a real llama-server a port, without letting it fire on the stack we actually ship.Test
serve_nested_foreignis registered next to the otherlaya_*rows (it needs the same classifier checkpoint): the real--laya-modelsfront, the real nested1bit serve, the real port read-back, and two fakes that announce like llama-server. No GPU, no model weights (touched ggufs, aslaya_models_e2edoes). Skips with 77 without a checkpoint, so CI without one stays honest.Not claimed: the refusal's positive arm. A foreign child on a port-0 rail does not exist today, so there is nothing to fire it with; the fixture covers the arm that can be reached.
Also verified
Run at the rebased head (
bc572b6c, after #3167 moved these rows behindtests/laya_e2e_guard.sh) on the same binary:And as
ctestrows, so the registration is verified and not just the script:Flake noted rather than smoothed over: the first
laya_route_e2erun at this head failed its third request withstatus 502in 0.2 ms (reply2.jsonnever written) and the next two runs were green. My change is not on that rail's path (it launches its backend with a concrete port, so theport() <= 0branch never runs), and I did not chase it — but it is a pre-existing intermittent 502 on a routed request, so it belongs to someone's issue rather than to this PR.Two traps this cost, recorded so the next fixture does not repeat them: the fake first printed its announce line on stdout (serve tees stderr only, so the line reached the log and never the guard — the test then passed on nothing), and a fake that answers within a millisecond never lets the refusal be seen at all, since the parent polls every 250 ms. Hence
>&2andFAKE_LLAMA_LOAD_S, and hence the arm that greps for the grandchild's line.