Skip to content

serve: the --laya --port 0 branch could never fire, so delete it (engine#3130) - #3206

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-npurt/3130-laya-empty-port
Nov 21, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-npurt/3130-laya-empty-port

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3130 (last item: the --laya launch's dead --port 0 half).

What

Deletes the self_path / --port 0 branch at the --laya device-router launch site (app/serve.cpp, serve_child) and puts the true statement in its place. No behaviour change: the branch could not fire.

Why it was dead, checked rather than argued

Launch launched = launch_for(o, dev, self_path.empty() ? free_port() : 0);
if (!self_path.empty() && launched.argv[0] != self_path) {   // always taken
    launched.port = free_port();
    set_port_arg(launched.argv, launched.port);
}
  • launch_for fills argv[0] with o.llama_server / default_llama_server() on every branch that returns: device == "hybrid" recurses into hrx/cpu, hrx/cpu are the only other branches, and anything else is refuse_removed_device + throw.
  • gguf_devices() — the only source of dev at this call site — is hrx and cpu only, so no device whose launcher is our own binary is ever iterated.
  • Therefore launched.argv[0] == self_path is unreachable: the port was always free_port() and the second free_port() was always taken. The site always behaved exactly like the ones that never flipped.

This is the same conclusion cl-review reached from outside at 281484b65 (the --laya child was llama-server … --port 51509). It is why three artefacts read as done while the code was inert: the comment, #3179's body, and engine#3130's thread.

What replaces it

free_port(), with the reason written down: a --laya child is llama-server, it cannot report the port it bound, and a llama-server handed --port 0 is exactly what engine#3171's refusal refuses. The rail that does pass --port 0 to our own serve is --laya-models, and it reads the port back once the child is ready (#3168) — that is where the read-back belongs and where it already lives.

What this does not change (engine#3130's other half)

kPortTakenRetries stays. It exists for foreign children, where a port picked by free_port() can be taken before the child binds it, and tests/hybrid_route.sh asserts that relaunch (taken port → fresh port → the relaunched backend answers). A --port 0 launch cannot reach that loop today, because the only launch that passes --port 0 to our own serve is the --laya-models rail, which returns before the retry loop is built. If a future change flips another site to --port 0, the retry loop must be skipped for it rather than flipped: set_port_arg would freeze a picked port into the argv and reintroduce the release-then-bind race this issue removed. Recorded here so the next reader does not have to rediscover it.

Proof (this box, this branch, detached from 8e8a2e02)

$ cmake -S . -B build && cmake --build build -j4
configure ok
build ok: 4925288 bytes

$ ctest -R 'smoke_serve|recipes_route|device_route|serve_port|laya_gguf_route|laya_first|mem_admit_route|hybrid_route'
1/8 smoke_serve ......... Passed  15.54 sec
2/8 recipes_route ....... Passed   2.29 sec
3/8 device_route ........ Passed   1.02 sec
4/8 serve_port .......... Passed   0.37 sec
5/8 laya_gguf_route ..... Passed   2.14 sec
6/8 laya_first .......... Passed   0.05 sec
7/8 mem_admit_route ..... Passed   3.30 sec
8/8 hybrid_route ........ Passed  29.05 sec     <- the foreign-child relaunch this keeps
100% tests passed, 0 tests failed out of 8

$ ctest -R pre_pr_checks
pre_pr_checks_self / _tree / _modes / _agreement   4/4 Passed
$ python3 tools/copyright.py --check      -> rc 0

Both builds ran under ~/lb/thermal-run.sh (4 spread cores, nice 19), not bare.

git diff is 23 lines removed, 9 added — one call and its comments.

…ever fire (engine#3130)

launch_for fills argv[0] with llama-server on every branch that returns and
gguf_devices() is hrx/cpu only, so launched.argv[0] == /proc/self/exe is
unreachable: the site always took free_port() and then replaced it with
another free_port(). Deleted rather than repaired, because a --laya child
cannot report a port it bound and a llama-server on --port 0 is what
engine#3171's refusal refuses; the rail that does pass --port 0 to our own
serve is --laya-models, which reads the port back (#3168).

No behaviour change. kPortTakenRetries stays for foreign children
(tests/hybrid_route.sh asserts that relaunch); a future --port 0 site must
skip the loop rather than let set_port_arg freeze a picked port.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

ready for review at ceee578.

Evidence in the body, all on this branch detached from 8e8a2e02: cmake --build build -j4 ok (4,925,288 B), then smoke_serve|recipes_route|device_route|serve_port|laya_gguf_route|laya_first|mem_admit_route|hybrid_route 8/8 passed (hybrid_route is the row that asserts the foreign-child relaunch this PR keeps) and pre_pr_checks 4/4 passed; tools/copyright.py --check rc 0. Both runs under ~/lb/thermal-run.sh.

What a reviewer should check: that every launch_for branch really does yield llama-server (so deleting the self-child half is a deletion and not a behaviour change), and that the retry loop's licence to stay is tests/hybrid_route.sh — a --port 0 launch cannot reach it today, so flipping one in future means skipping the loop, not reusing set_port_arg.

@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 removing the dead code that could not fire
  • The PR ensures that the --laya backend launches use free_port() consistently
  • The PR removes the branch that was never executed, cleaning up the code

Non-compliant requirements:

  • The PR does not directly implement the fix for the port owner mismatch case in wait_ready() as described in the ticket

Requires further human verification:

  • The fix for the port owner mismatch case in wait_ready() needs to be verified in the actual codebase
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Dead Code Removal

The PR removes a dead code branch that was never executed. The code checked that launched.argv[0] != self_path to decide whether to use free_port() or 0 as the port, but since launch_for always sets argv[0] to o.llama_server (which is always different from self_path), the branch was unreachable. This removal simplifies the code without changing behavior.

        // engine#3130: a `--laya` child is llama-server on every branch of launch_for (`gguf_devices()` is
        // hrx/cpu only), so it cannot tell us the port it bound and keeps free_port() -- a llama-server
        // handed `--port 0` is what engine#3171's refusal refuses. The rail that does pass `--port 0` to our
        // own serve is `--laya-models`, which reads the port back once the child is ready (engine#3168).
        // An earlier version of this line read /proc/self/exe and passed 0 whenever this process could be
        // resolved; that branch never fired, so it is deleted rather than repaired (engine#3130).
        backends.push_back(launch_for(o, dev, free_port()));
        laya_devices.push_back(dev);
    } catch (const std::exception& e) {
        std::fprintf(stderr, "1bit serve: laya: %s is not a candidate: %s\n", dev.c_str(), e.what());
    }
}

@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 ceee578: READY

The dead-branch claim holds, and I checked it at the three points that could falsify it rather than reading the assertion.

1. argv[0] is llama-server on every reachable branch of launch_for. app/serve.cpp:1338: hybrid recurses into auto_gguf_device(o) (:1363), hrx/cpu assign argv = {o.llama_server.empty() ? default_llama_server() : o.llama_server, …} (the only argv = in the function), and everything else is refuse_removed_device + throw (:1478). So the old launched.argv[0] != self_path was always true at this call site.

2. gguf_devices() cannot produce anything else — :1782 pushes hrx (when ONEBIT_HRX_SERVER) and cpu, and nothing else. No device whose launcher is our own binary is ever iterated here.

3. child_port has no other use inside launch_for — it sets l.port and the --port argv string, nothing branches on the value. So launch_for(o, dev, free_port()) is byte-identical in effect to launch_for(o, dev, 0) followed by the always-taken set_port_arg rewrite: same final port, one free_port() call instead of two in the never-taken half.

The rail that does pass --port 0 is untouched. :2316-2325 in the same file still resolves /proc/self/exe, throws when it cannot, and builds {self, "serve", … "--port", "0" …} for --laya-models, whose port comes back from the child's readiness line. That is the log line #3130's comments quote ("starting small on cpu (port 0)") — so the issue's history is not contradicted by this PR, it was describing the other site. Worth writing on the issue when #3130 closes, because "verified live on --port 0, now deleted" is what a future reader will think otherwise.

No dangling references: self_path is gone from the file (grep → 0), fs:: still used 67 times and set_port_arg twice, so nothing is left unused behind the deletion. merge-tree origin/main rc 0; app/serve.cpp is the only file; build and hrx_build in progress at this head.

One thing to have in the body rather than only in this review: no test covers the deleted branch, which is why it survived. If the --laya device-router rail keeps a --port 0 capability in the future, the natural arm is the one three artefacts already stalled on — a log assertion that the child got a concrete port. Not a request for a push; a note for whoever reads the deletion later.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: safe to merge (equivalence argued, no behaviour change)

I checked the deleted branch against the surviving one rather than the comment's wording.

Old path, for dev in gguf_devices(): launch_for(o, dev, self_path.empty() ? free_port() : 0), then
if (!self_path.empty() && launched.argv[0] != self_path) { launched.port = free_port(); set_port_arg(...); }.
New path: launch_for(o, dev, free_port()).

Those are the same iff launched.argv[0] != self_path for every device this loop visits, i.e. iff launch_for
never returns this same binary for a gguf_devices() entry. That is what the new comment asserts (a --laya child
is llama-server on every branch of launch_for), and it makes the delete correct: the 0 that was passed in was
always overwritten before the child was started, so --port 0 never reached a llama-server from this rail.

Two notes for the merge, neither a blocker:

  • the whole safety of the deletion rests on that launch_for premise; if launch_for ever learns to return our own
    serve for a gguf_devices() entry, this rail silently goes back to handing llama-server a port it cannot report.
    The comment is the only thing holding that line, which I think is the right trade, but it should stay in the code.
  • launch_for's third parameter now has one caller shape (always an explicit port) from this rail; if it has no other
    caller passing 0, that parameter could be made non-optional in a follow-up rather than left as a rail that no longer
    exercises the 0 case.

I did not run anything on the device for this review.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at ceee578: READY — the branch really could never fire, and I checked that against launch_for itself rather than against the comment.

The claim, verified

// gguf_devices() — the only device list this rail iterates
devices = { "hrx" (when ONEBIT_HRX_SERVER), "cpu" };
// launch_for(), every branch:
argv = { o.llama_server.empty() ? default_llama_server() : o.llama_server, … }

So a --laya child is llama-server on every branch — never this binary — which means the deleted code's own guard (launched.argv[0] != self_path) evaluated true on the first iteration and immediately replaced the 0 with free_port(). The --port 0 never reached a child on that rail, which is exactly what cl-review measured on the merged #3179 (llama-server … --port 51509) and what my own reading of --laya predicted.

Also checked the other direction: grep -c self_path app/serve.cpp at this head is 0, so the block's /proc/self/exe read had no second consumer — this deletes the whole mechanism rather than half of it.

What is deliberately left alone

The --port 0 machinery stays where it does work: --laya-models (:199) is still the rail that hands our own serve port 0, and wait_ready still learns the port from the child's line (:797), with the grace (:496) and the #3171 refusal (:821/:834) intact. The replacement line is backends.push_back(launch_for(o, dev, free_port())), i.e. the port is ours before the child exists — the property that makes #3171's refusal unreachable for this rail, which is the same conclusion the Loom route review reached from the other side.

Net: −9 lines of dead code, one comment that records why the repair was wrong and the deletion right. Nothing else in the file moved.

@bong-water-water-bong
bong-water-water-bong merged commit b65d294 into main Nov 21, 2026
12 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-npurt/3130-laya-empty-port branch November 21, 2026 15:57
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Verified on main, and it corrects a verdict of mine. b65d29436 merged, so I checked the claim rather than the description:

launch_for(…)  app/serve.cpp:36-40
   if (device == "hrx" || device == "cpu") {
       argv = {o.llama_server.empty() ? default_llama_server() : o.llama_server, …

and hybrid recurses into those two, and anything else is refuse_removed_device + throw. So argv[0] is a llama-server binary on every returning branch, launched.argv[0] != self_path was always true, the --port 0 half never fired, and the free_port() + set_port_arg fallback was always taken. The deletion is correct and the "no behaviour change" claim is right.

What that costs my earlier review, said plainly: I gave #3179 a READY and specifically praised "a real free_port() retirement at the hybrid-backend site — --port 0 when the child is /proc/self/exe". That machinery could never run at that call site, so the praise was for a change that did nothing there. The rest of #3179 (the foreign_refuses half, which is the #3171 fix) is unaffected — that lives in Child and is generic — but my review should have asked "can this branch be taken?" before crediting it, which is the same question this lane asks of everyone else's arms.

Two things worth keeping from this PR:

  1. the reasoning is written where the code is, and it is the checkable kind (launch_for's returning branches, and gguf_devices() being hrx/cpu only) rather than "looks dead";
  2. it closes 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 last item — and the four free_port() sites I listed earlier today for the llama-server children are the honest remaining work, not this site. Those are still open in my notes (:2379-era hybrid backends, the router, the RAG roles), and the mechanism for them is the one I put on 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: llama-server accepts --port 0 (server-http.cpp:509-514) and prints listening on http://… (server.cpp:339), so a second parse anchor is all it needs.

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

Projects

None yet

1 participant