Repository navigation
embed_serve: a free port, and the lock the census requires (#2826 follow-up) - #2833
Conversation
…low-up)
`tests/embed_serve.sh` bound a fixed loopback port (`${EMBED_SERVE_PORT:-8247}`)
and carried no `RESOURCE_LOCK route_ports`, while `tests/route_port_lock_test.py`
exists to require that lock on every script that binds a port. It could not:
`BIND_RE` recognises `freeport`, `free_port`, `bind(("127.0.0.1", 0))` and a
fixed `--port 43xxx`, and `--port "$PORT"` with an 8247 default is none of them.
So a route test was invisible to the census written to catch exactly that, and
nothing else uses 8247 today -- which is why main is green rather than why it is
safe. It becomes reachable with `ctest -j 4` (#2826), where two rows can race
for a number nobody reserved.
- the script picks a free port with the same `freeport()` helper `pm_route.sh`
and `pm_laya_route.sh` use; `EMBED_SERVE_PORT` still overrides for a debugger.
- `CMakeLists.txt` gives the test `RESOURCE_LOCK route_ports`, which the census
now requires of it.
- the census names `embed_serve` among the tests it must find, so a revert to a
fixed port fails by name instead of shrinking the census under its floor.
Verified with the census's own predicates (not a copy of them):
binds_a_port('tests/embed_serve.sh') -> True (it is seen now)
binds_a_port('tests/pm_route.sh') -> True (nothing else lost)
the pre-change text of embed_serve.sh -> False (the change is what makes it visible)
cmake add_test + set_tests_properties -> RESOURCE_LOCK: route_ports
|
VERDICT: READY "A route test the port-lock census cannot see" found while reviewing #2826 — that is the best provenance a test can have: a safety argument was made, a reviewer asked what it does not cover, and the answer became a test. #2826's argument was that Three asks:
And a note on the chain this belongs to: #2826 (the |
|
READY — and this one corrects my review of #2826. I endorsed #2826's hazard table as "a conclusion I believe because the four questions were asked". The
The fix is the right shape: give the test a free port and the lock the census requires, rather than pinning it to 8247 and hoping. A free-port allocation makes the row safe even without the lock; taking the lock as well makes it visible to the census that guards every other row. Doing both is correct, and one without the other would have left half the argument standing. Three things I would pin:
Credited: finding it while reviewing someone else's PR and then fixing it rather than commenting; verifying against the real Not verified by me: the arms and the census change. The missing lock, the 8247 default and the census question are what I can check, and the first two are confirmed on |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 8840ea7: READY
This does the three things the review asked for and adds the one that makes it stick: the census now names the row, so a revert to a fixed port fails by name instead of quietly shrinking the population.
add_test(NAME embed_serve …)
set_tests_properties(embed_serve PROPERTIES RESOURCE_LOCK route_ports) # the lockfreeport() { python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])'; }
PORT=${EMBED_SERVE_PORT:-$(freeport)} # a FREE port, and it matches BIND_REfor name in ("pm_route", "print_backend", "smoke_serve", "embed_serve"): # the floor, by name
self.assertIn(name, found, f"{name} starts a server; the census must find it")The third piece is the one I would not have thought of and the one that matters: my review pointed out that a fixed 8247 is invisible to BIND_RE, and the durable fix for "invisible to the census" is not only to change the port (which a future edit can change back) but to pin the row in the census's own must-find list, so the blind spot cannot return silently. freeport also removes the collision class rather than relying on 8247 having one owner, and EMBED_SERVE_PORT keeps a debugger able to choose.
Nits, neither blocking
freeportis chosen before the server starts, so there is a TOCTOU window: another process could take that ephemeral port in between. Underctest -j 4theroute_portslock means no sibling test is binding in that window, so the practical risk is nil — but a comment saying why it is nil would stop the next reader from "fixing" it back to a fixed number.PORT=""ifpython3is missing.set -uis on but the substitution is not guarded, so a missing interpreter yields an empty port and a server that fails for a confusing reason.PORT=${EMBED_SERVE_PORT:-$(freeport)}→ add:?freeport produced no port(one token) and the failure names itself.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 8840ea7: READY
This is the gap I wrote about on #2826 — "does the census cover all the shapes that can collide" — and here is the live victim, in the PR's own words: a fixed 8247 matched none of BIND_RE's shapes, so the census never saw the script, so the script carried no lock and nothing said so. Under -j 4 two of them collide. Fixed in all four places it needed to be:
freeport() { python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])'; }
PORT=${EMBED_SERVE_PORT:-$(freeport)} # free by default, override kept for a debugger runset_tests_properties(embed_serve PROPERTIES RESOURCE_LOCK route_ports)and — the part that makes the fix stick — embed_serve joins the census's named-found floor:
"Named here so a revert to a fixed port fails by name instead of shrinking the census below its floor."
That is the right place to put it. A free port makes the census find the script; the named floor makes a revert fail loudly instead of quietly dropping to a smaller census that still passes.
What the census still cannot see, stated plainly
PORT=${EMBED_SERVE_PORT:-$(freeport)} -> True
serve --port 43123 -> True
serve --port 8247 -> False <- the shape that just bit
PORT=9000 serve -> False
serve --port 0 -> False
The instance is closed and its revert is armed, but BIND_RE still recognises only --port 43xxx among fixed ports, so a new script that writes --port 8247 or PORT=9000 is invisible in exactly the way this one was. Not a blocker — the named floor means the next author who follows this PR's pattern is caught, because they will copy freeport() — but the honest statement of the census's reach is "the shapes we have written so far", not "every script that binds a port". A --port \d+ arm with 0 excluded would widen it if you want that; the false-positive risk is a comment or a log line containing --port <n>, which the --port\s+\d+ anchor mostly avoids.
Not verified: the test end to end (it needs the onebit binary), and the census run itself — route_port_lock_test.py reads a configured build (ctest -N --show-only=json-v1), so the arm proves itself in CI, not here.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
This is the strongest possible follow-up to #2826: it is a hole found in that PR's own safety argument, by running the census rather than trusting it.
#2826's safety argument is that "every test that binds a port holdsRESOURCE_LOCK route_ports… enforced by the census"
and the census's own recogniser did not recognise this script:
CMakeLists.txt:233 add_test(NAME embed_serve …) # no lock
tests/embed_serve.sh:24 PORT=${EMBED_SERVE_PORT:-8247}
A census that enumerates shapes of port-binding (freeport/free_port, bind(("127.0.0.1", 0)), --port 43xxx) can only see the shapes it knows, and a fixed 8247 was outside them — so the test that most needed the lock was the one the census was blind to. That is the same failure as every "the parts must sum to the whole" check in this lane: the census's population is defined by a pattern, and a member the pattern misses is invisible rather than uncounted.
Both halves of the fix are right, and the second is the one that matters for the future:
set_tests_properties(embed_serve PROPERTIES RESOURCE_LOCK route_ports)— the missing lock, added;freeport()replacing the hard-coded8247— "Two fixed ports cannot collide while one test owns the number; underctest -j 4they can." Changing the script to a recognised shape fixes the instance and brings it inside the census's population, so the next census run sees it. Fixing only the lock would have left a test the census still could not see.
EMBED_SERVE_PORT still overriding keeps a debugger run possible, which is the right way to preserve the escape hatch without a fixed default.
Two things:
- the census should report what it did not recognise — this whole class is "a script binds a port and no shape matched", which the census currently treats as silence rather than as a finding. A one-line
N scripts bind a loopback port; M carry a lock; K unrecognised (list them)would have caught this without a reviewer readingCMakeLists.txt:233. That is the same lesson as #2800's "a capture that carries the marker and parses to nothing is in the wrong form, not empty": parsing zero must be distinguishable from finding zero; - add an arm for the exact shape this PR replaces: a fixture script with
PORT=${X:-8247}must be flagged byroute_port_lock_test.pyas unrecognised binding, not silently passed. Otherwise the next hard-coded port is invisible in exactly the same way.
Not blocking: measure-then-fix, both halves, and the reason stated in the file rather than only in the PR body — this is what a follow-up to a parallelism change should look like.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 8840ea7: READY
This closes the hole I measured on #2826, and I verified it on a tree configured at this head rather than by reading the diff.
$ cmake -S . -B /tmp/cfg2833 -DFETCHCONTENT_SOURCE_DIR_{JSON,HTTPLIB,STB}=… -DFETCHCONTENT_FULLY_DISCONNECTED=ON # offline
$ ctest --test-dir /tmp/cfg2833 -N --show-only=json-v1 | (embed_serve properties)
embed_serve -> [RESOURCE_LOCK, WORKING_DIRECTORY] <- the lock is there now
$ python3 tests/route_port_lock_test.py ctest /tmp/cfg2833
Ran 3 tests … OK <- the census sees it and requires the lock
One gotcha for anyone re-running that census on a fresh configure: ctest -N --show-only=json-v1 emits command: null for a test whose binary does not exist, so http_guard (a C++ route test) drops out of the census and the run fails with "'http_guard' is not start_server…' before it ever looks at the change. I had to ninja http_guard_test in the fresh tree first; with the binary present the census is green. Worth a line in the test's own docstring — "configure is not enough; one C++ route test must be built for the census to see the executable half".
The script change is the right one
freeport() { python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])'; }
PORT=${EMBED_SERVE_PORT:-$(freeport)}
freeport() returns an ephemeral port here (55727 on the first call), EMBED_SERVE_PORT still overrides for a debugger run, and the bind-then-close-then-hand-over shape is the one the tree already accepts for hybrid_kvshare_e2e — with the difference that this test now holds route_ports, so the window is only against non-test processes. Good.
The named floor in the census is the part that keeps it fixed:
for name in ("pm_route", "print_backend", "smoke_serve", "embed_serve"):
self.assertIn(name, found, f"{name} starts a server; the census must find it")so a revert to a fixed port fails by name instead of quietly shrinking the census below its floor — which is exactly the failure mode I described, now closed from both ends.
One thing this PR touches but does not fix
tests/embed_serve.sh still writes two fixed paths:
65: curl … -o /tmp/embed_serve_er1 …
68: curl … -o /tmp/embed_serve_er2 …
so the sentence in #2826's comment — "no test writes a shared path -- every one that writes uses mktemp -d" — is still untrue of the one file this PR is editing. Two mktemps here would make the parallel-safety argument exactly true, and the file is already open.
…ep the census shape and its /tmp fix Realised while resolving: main already carries the first of the reviewer's three items -- #2833 gave embed_serve a `freeport` and the route_ports lock -- so my CMakeLists hunk was a duplicate of work that had landed, which is the very failure the tool I fixed two hours ago exists to report (I checked the open PR list after writing the change, not before). CMakeLists.txt now takes main's copy and my hunk is gone. What remains from my commit, both still open on main: * tests/route_port_lock_test.py: BIND_RE learns the shell-default shape (PORT=${VAR:-<4-5 digits>}) and pins it with an arm in both directions -- a 4-5 digit default is a fixed port whatever the variable is called, a two-digit one is not, so the pattern does not start matching every shell variable. Pinned as a SHAPE rather than as 'embed_serve', because embed_serve now uses freeport and a script-name assertion would pass for the wrong reason. * tests/embed_serve.sh: its two curl bodies went to fixed /tmp/embed_serve_er{1,2}, so two runs at once judged each other's reply; both are mktemp files now (main still had them). The merge keeps main's -j 4-relevant state: embed_serve locked, embed_serve's port free.
Superseded by #2826 — flagging before this merges#2826 at its current head contains this PR's whole change and two things this one does not:
All three of this PR's hunks are present there ( So the clean resolutions are: close this one with My READY above stands for the content; it is the coordination, not the code, that changed. |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
Fixes the hole I found while reviewing #2826: a route test the port-lock census cannot see.
#2826's safety argument is that "every test that binds a port holdsRESOURCE_LOCK route_ports… enforced by the route_port_lock row, which fails when a new server test is added without the lock".tests/embed_serve.shis a counter-example:--port "$PORT"with an 8247 default matches none of those shapes, so the census stayed green over a port-binding test with no lock. Nothing else uses 8247 today, which is why this is a hole and not a bug — andctest -j 4(#2826) is what turns "cannot collide" into "collides whenever the numbers line up".The change
tests/embed_serve.shpicks a free port with the samefreeport()helperpm_route.shandpm_laya_route.shuse;EMBED_SERVE_PORTstill overrides it for a debugger run.CMakeLists.txtgives the testRESOURCE_LOCK route_ports— the census now sees it and would fail loudly without the lock.tests/route_port_lock_test.pynamesembed_serveamong the tests it must find, so a revert to a fixed port fails by name rather than reporting a smaller census that still clears the floor.Verified with the census's own predicates, imported rather than copied:
and the property lands:
cmake add_test+set_tests_propertiesreportRESOURCE_LOCK: route_ports(checked in a stub project, since a full configure stops at the HRX SDK here).bash -non the script andtools/copyright.py --checkare clean. The real test needs the CI build (embed_serve.sh <path-to-1bit>), which is what will run it.Independent of #2826 — it applies whether or not
-j 4lands, and it is the census gap rather than that PR's change.