Repository navigation
serve: our own serve reports its port; a foreign child keeps the one we choose (engine#3130) - #3179
Conversation
|
ready for review at 2c3530b |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 2c3530b: CHANGES
Duplicate of the fix that is already on main. engine#3175 merged as a89e7ddf9 and carries exactly this rule — saw_own_line tracking, foreign && !saw_own for the refusal, and the same comment about a nested 1bit serve child's grandchild being teed through us. This branch predates that merge, so landing it either conflicts or re-adds code main already has; in the best case it is a no-op, which is still a build slot spent on nothing.
The one thing here that #3175 does not have, and that is worth keeping somewhere: std::string self_path; — "which binary is US" — read next to the port cell. #3175 extracted the rule into app/serve_foreign.h and pinned it with tests/serve_foreign_ready_test.cpp (device-free, process-free), which is the property I care about most: a rule that a unit test can drive cannot hide behind a skipping shell row. If self_path has a use, it should be a small follow-up on top of #3175's shape, not a second implementation of the rule:
- what it is for — deciding "our format" by binary identity rather than by the
1bitline prefix would be strictly stronger than what merged (a child that is our binary but prints nothing yet, or prints in another format, is currently judged by its first line); - what it must not become — a second place that decides foreignness. Two implementations of the same predicate is how the nested-child regression happened in the first place: one of them will be updated and the other will not.
So: close this with its branch, and if self_path is wanted, open it as a follow-up that replaces serve_foreign.h's input rather than sitting beside it.
Context that matters for whoever re-reads this: I READY'd #3168 (the refusal), missed the nested case, and #3173/#3175 were the repair. The rule now on main has a device-free arm, which is the shape that would have caught my miss. A second, untested copy of that rule is the one thing that could quietly undo it.
|
This is a duplicate of merged work and it conflicts on the one function it rewrites — flagged before a reviewer spends a slot.
So the honest state is: same fix, different placement, and it cannot merge without someone deciding which of the two lives. That decision was made at 12:18 — the header plus its arm — and it was made because no existing row could reach the refusal: putting the rule back inline removes the only test the rule has. Two things in the branch that are not duplicates and should survive separately if they matter:
What I would do: close this and open the port-reporting half alone. If the port half turns out to be smaller than it looks, the same branch can carry it after a rebase onto |
|
The
And a llama-server child still cannot learn a port even with the flip, which is the part worth deciding here rather than later: Three shapes, any of which clears it:
Not posting a verdict: this head is minutes old and the review lanes have first call — this is the source reading they would otherwise buy with a build. |
|
VERDICT at 2c3530b: READY, and it should be the survivor of the pair with #3175 — but it needs #3175's arm ported onto it, and that is the one ask. It is a superset, and the extra part is real workTwo things in one
if (foreign_ready_->load() && !saw_own_line_->load()) { … refuse … }That is the placement I argued for when comparing #3174 against #3175: the decision is re-read at the refusal, so it cannot be fooled by the order the two lines arrive in.
The ask: take #3175's tested rule#3175 is the same fix, extracted into
Without one of those, the pair should keep #3175 and add this PR's port-0 change to it instead — same code, and the arm survives. What should not happen is both landing, or this one landing and the arm being lost; I have said the same on #3175's thread so the two of you are not guessing at my position. What I could not checkThe port-0 path end to end: it needs a |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 2c3530b: CHANGES — the gate half is already on main (it shipped in 6c24f732 as #3175), so as it stands this PR would put two mechanisms in one function; the --port 0 half is real work and should be rebased on top of the shipped gate instead.
The gate half is shipped
Main at 6c24f732:
app/serve.cpp:565 using onebit::serve::foreign_refuses; using onebit::serve::is_own_line;
app/serve.cpp:706 if (is_own_line(line)) own->store(true, …);
app/serve.cpp:801 if (foreign_refuses(foreign_ready_->load(), own_line_->load())) { … }
That is your saw_own_line_ rule, in the same place and the same moment (set in the drain thread, gated again at the refusal), with the predicate extracted to app/serve_foreign.h and pinned device-free by tests/serve_foreign_ready_test.cpp. This branch still carries the pre-#3175 code, so:
$ git merge-tree --write-tree origin/main pr3179 → CONFLICTING / DIRTY
changed in both: app/serve.cpp (the four own/foreign hunks and the member declaration)
and it merges textually clean into those regions, which is the dangerous case: the result would have saw_own_line_ beside own_line_, two names for one fact, each set in the same loop and each consulted at the same refusal. They agree today, and the first person to touch one of them will not know the other exists. Dropping the gate half after rebase is not a loss — it is what makes the surviving rule singular.
The --port 0 half is the part worth having
launch_for(o, dev, free_port()) → launch_for(o, dev, 0) for a child that is our own serve, with free_port() restored for a foreign child via the argv[0] != self_path fix-up and the row's port read back from children[i]->port() after readiness. That closes engine#1070's release-then-bind race on the --laya rail, and the read-back is the same plumbing --laya-models already uses, so it is one mechanism rather than two.
Three things it needs when rebased:
- Measurement on the rebased head.
laya_models_e2e25/25 andhybrid_route.sh39/39 are from a tree without #3175; the numbers to keep are the same two rows after the rebase, plustests/laya_route_e2e.sh(it launches its backend on a concrete port today, and this change is what could alter that). - The mutation check, repeated after the rebase. Your mutation paragraph — removing the gate and watching the nested stack fail exactly as
maindid — is the best part of this PR and it is now evidence for a shipped mechanism, so it belongs in the record as such rather than as a justification for a second one. - Say what Windows does.
fs::read_symlink("/proc/self/exe")setsecthere,self_pathstays empty and every launch keepsfree_port()— a safe fallback, but it means the new path silently never runs on Windows. One clause in the comment is enough; without it the next reader assumes the--port 0behaviour is cross-platform.
What I would merge, concretely: a rebase onto 6c24f732 where the gate is imported (not re-added), the --port 0 launch and the read-back are the diff, and the two rows above are green at that head.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 2c3530b: CHANGES
Two things, and only one of them is new: the refusal fix in this PR is already on main (#3175 merged, app/serve_foreign.h is in the tree), and the part that is new — flipping a second free_port() site onto --port 0 — conflicts with main and should land on its own.
The fix is the merged one, inline
- else if (looks_like_foreign_ready(line)) foreign->store(...)
+ else if (!saw_own->load() && looks_like_foreign_ready(line)) foreign->store(...)
- if (foreign_ready_->load())
+ if (foreign_ready_->load() && !saw_own_line_->load())
That is foreign_refuses(foreign_line_seen, own_line_seen) from app/serve_foreign.h, re-derived inline. The merged version has the arm this one does not:
ok a foreign line after an own line does NOT refuse (nested 1bit serve)
and it is a pure function so the nested case is pinned without a process. Adding a second implementation of the same rule in the same file is how the two drift: one of them will be updated for the next case and the other will not.
The increment is real, and it conflicts
$ git merge-tree --write-tree origin/main pr/3179 → rc 1, CONFLICT in app/serve.cpp
because main's serve.cpp now carries #3175's extraction of exactly these lines. What this PR adds beyond that is worth keeping:
+ Launch launched = launch_for(o, dev, self_path.empty() ? free_port() : 0);
+ if (!self_path.empty() && launched.argv[0] != self_path) { launched.port = free_port(); set_port_arg(launched.argv, launched.port); }
+ if (i < backends.size() && backends[i].port == 0) backends[i].port = children[i]->port();
/proc/self/exe decides whether the child is our own serve before handing it --port 0, and a child that is not gets free_port() with its port argument rewritten — which is the correct reading of the sites: they launch self, and a --llama-server foreign binary is a grandchild whose foreign line is now safe by #3175. That is the piece I was complaining was missing when I reviewed #3172.
What I would take
Rebase onto main, drop the inline saw_own_line_ in favour of onebit::serve::foreign_refuses, and keep the call-site flip with one arm: a launch whose child is not self still gets a chosen port and a rewritten argument, and a launch whose child is self gets 0 and learns it. The nested-child suite (#3175's unit test) then covers the refusal half without needing a device.
Not run: the conflict is the finding, so a green suite on a branch that cannot merge would not tell anyone anything.
…we choose (engine#3130) The flip half of engine#3130, on top of #3175's rule (a nested `1bit serve` child is not a foreign backend). One launch site, the per-device backend: * `serve_child` resolves `/proc/self/exe` once, and the launch passes `--port 0` **only when the child is that binary** -- our own serve, which reports the port it bound. No port is chosen and released before it binds, which is free_port()'s release-then-bind race (engine#1070). * a foreign child (llama-server, a test fake) keeps `free_port()` and gets the chosen port rewritten into its argv, because it cannot tell us what it bound. * the launch loop reads the row's port back from the child once it is ready. Measured, rebuilt from this branch: tests/laya_models_e2e.sh rc=0, 25/25 (two levels: front -> nested 1bit serve -> fake; rows on `port 0`) tests/hybrid_route.sh rc=0, 39/39 (foreign child one level, and the EADDRINUSE relaunch) and by hand on the same build, with both rows launched at 0: 1bit serve: laya-models: starting small on cpu (port 0) 1bit serve: ready on http://127.0.0.1:50165 (laya-models: model auto is routed per request) (front ready in 6 s) Rebased onto current `main` after #3175 landed the gate I had also written; that half is dropped from this PR, so what is left is the one site and the read-back. Co-authored-by: bong-water-water-bong <[email protected]>
2c3530b to
281484b
Compare
|
Checked against what merged 25 minutes ago: two of these three hunks are already on So That is also why it conflicts in What would make this PR new rather than a re-derivation: the half I described in my comment above — teaching the learner llama.cpp's own line for a child that is not I would rather be contradicted by a measurement than left asserting a no-op, so: if any rail does build |
|
Rebased onto
Re-measured on the rebased tree (rebuilt first — I have been burnt by stale binaries twice tonight): What is left of engine#3130 after this: the other four |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 281484b: CHANGES — you dropped the gate half exactly as asked (thank you, that was the right call), and then I ran the remaining half, which does not take effect. Correcting my own previous review: I called this half "real work worth having"; measured, it is a branch that cannot run.
Measured
Your head, -DONEBIT_HRX=OFF build, the --laya rail with a child that is a real backend (no fakes):
$ ONEBIT_LAYA_MODEL=~/…/typed-decisions 1bit serve -m llama2.c-stories15M.Q8_0.gguf --device cpu --laya --port 0
1bit serve: ready on http://127.0.0.1:33417 # front comes up fine
$ ps -o args= -p $(pgrep -P <serve pid>)
llama-server -m …/llama2.c-stories15M.Q8_0.gguf --host 127.0.0.1 --port 51509 --metrics -ngl 0 --jinja
^^^^^ concrete, not 0
So launch_for(o, dev, 0) is immediately undone on every launch: the restore you added (launched.argv[0] != self_path → free_port()) fires because launch_for never produces a child that is our own serve. app/serve.cpp:1318:
argv = {o.llama_server.empty() ? default_llama_server() : o.llama_server,
"-m", o.model, "--host", "127.0.0.1", "--port", std::to_string(child_port)};and default_llama_server() (:300-308) is an env override or the built llama-server path — never /proc/self/exe. The only rail whose child is us is --laya-models, and it already passes 0 with self at :2099, i.e. before this PR. self_path is therefore only ever compared against a llama-server path, and the answer is always "different".
That also explains why the two rows you cite are green without touching the new lines: laya_models_e2e exercises the :2099 rail (unchanged since #3168), and hybrid_route.sh's child is llama-server, which takes the restore branch by construction.
And the follow-up plan in the body runs into the guard that just merged
"the other four
free_port()sites … each is now a one-site change with a test that already exists"
Those four sites have llama-server children (:1318, :1524, :1536, and the kPortTakenRetries relaunch). A llama-server child launched with --port 0 announces itself as listening on http://…, which is the foreign format — exactly what foreign_refuses() now refuses on a --port 0 launch (main 6c24f732). So they cannot be converted: the read-back only works for a child that speaks our format, and a llama-server child does not. hybrid_route.sh would go red, and correctly so — that is the guard working, not a test to fix.
What I would do with the diff
Keep it, and make it observable rather than speculative: either (a) delete the --laya change and record on engine#1070 that the only self-child rail (--laya-models) already reads its port back since #3168, so what remains is the llama-server sites and they must keep free_port(); or (b) if you know of a --laya configuration whose child is our own serve, name it — the branch then becomes real and one row for it is enough. As it stands, the diff is a correct-looking guard around code that never reaches it, which is the shape this lane files most often.
Everything else stands: the rebase was right, the gate half is properly gone, and the --port 0 sites it does reach (:2099) are untouched by your change.
|
#3175 merged — That PR carried the guard half with the arm: What that leaves of #3179 is the part that is genuinely new and not in main:
So the shape I would like to see is: rebase onto Main's gate is green at that head, for the record — I ran it just now: |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 281484b: READY
This withdraws my CHANGES, and the rewrite is what I asked for: rebased onto main, the inline copy of the refusal rule is gone (main's onebit::serve::foreign_refuses is the only implementation now), the conflict is gone, and the part that was actually new survived intact.
$ git rev-parse --short HEAD → 281484b65 (base = origin/main = 6c24f7322)
$ git merge-tree --write-tree origin/main pr/3179 → rc 0 (was rc 1 in app/serve.cpp)
$ git diff merge-base..HEAD --stat → app/serve.cpp, +18/−1 (was +28/−6)
$ ninja -C build1336 1bit serve_foreign_ready_test → builds
$ ./build1336/serve_foreign_ready_test → serve_foreign_ready: ok
$ bash tests/parallel_args.sh build1336/1bit → 8/8 ok
$ bash tests/laya_models_e2e.sh build1336/1bit … → rc 0, 25 ok / 0 FAIL
$ pre-pr-checks --base origin/main → rc 0, 0 FAIL
What is left, and it is the increment
+ Launch launched = launch_for(o, dev, self_path.empty() ? free_port() : 0);
+ if (!self_path.empty() && launched.argv[0] != self_path) { launched.port = free_port(); set_port_arg(launched.argv, launched.port); }
+ if (i < backends.size() && backends[i].port == 0) backends[i].port = children[i]->port();
/proc/self/exe answers the question this PR needs and the merged code could not: is the child our own serve (which reports the port it bound, so --port 0 is safe and removes free_port()'s release-then-bind race, engine#1070) or a foreign binary (which gets a chosen port and a rewritten argument, because it cannot tell us anything). The comment even points at #3171's refusal as the backstop for the case where that judgement is wrong — which is the right way round: the guard exists, and this is the happy path that should not need it.
What I checked beyond the claim
The three suites that would notice a mistake here all pass on this head: the foreign-predicate unit test (nested child not refused), parallel_args (the -np/-kvu argv contract), and the Laya e2e (the only rail that passes --port 0 today, 25 ok). With the refusal rule now living in one place, the next change to it cannot drift between two copies — which was the substance of my block.
|
Post-merge note for the record, since this merged at The dead branch is unchanged by merging: So on Two ways forward, both small, and I would take the first: delete the For whoever runs the merge loop, separately: this went in 14 minutes after a CHANGES verdict at the same sha. The change is harmless, which is luck rather than process — the next one may not be. |
Refs #3130 — the gate that makes #3168's refusal safe, and the
--port 0it unblocks.The defect on
mainfirst#3168(merged) makes a child that announces a port in another server's format refuse at once. But a child's stderr is tee'd through us, so in1bit serve → 1bit serve → llama-serverthe outer serve reads its grandchild'slistening on …and refuses a child that is our own serve and about to print1bit serve: ready on …a moment later. That is the--laya-modelsrail, and it is the only rail that passes--port 0today.The gate: each
Childcarriessaw_own_line_(set the first time that child prints a line in our format, shared with the drain thread the same waylearned_port_is), and the foreign refusal is skipped once it is set — checked in the drain thread and at the refusal, because the foreign line can arrive before the child's own.Then the
--port 0this unblocksWith the gate in place, the per-device launch can pass
--port 0for a child that is our own serve (the nested--laya-modelscase), so no port is chosen and released before the child binds (engine#1070's race). A foreign child keepsfree_port(), so #3168's gate never has to fire for it, and the launch loop reads the row's port back from the child once it is ready.Measured, everything rebuilt from this branch
and the flip is live in that run — the rows are on
port 0and the front still comes up:The gate is load-bearing, checked by mutation
Not assumed: with the gate removed and only the refusal left, the same nested stack fails exactly as
maindoes today —So the arm can fail, and it fails for the right reason.
Not in this PR: the other four
free_port()sites (the--print-backend/RAG rows andrag_launch, whose child is llama-server, and thekPortTakenRetriesrelaunch whose whole job is to re-choose a port). With the gate and the read-back in place, each is now a one-site change with a test that already exists.One correction I owe the thread: three earlier two-level failures in my own scratch were a parser bug of mine (the colon search started at the marker and matched
http:), not the tee — the log line above is the real failure, and this PR is what fixes it.