Skip to content

tests: five serve route tests fail on a front-port collision instead of retrying (check-then-use race, CI flake) #1444

Closed

Description

@bong-water-water-bong

Five serve route tests pick their front port with a check-then-use race and fail the whole test the first time it loses:

port=$(python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])')
"$bin" serve ... --port "$port" &

The port is released before serve binds it, and serve launches its backends (each on a free_port(), which also releases before the child binds) before it binds its own front port (app/serve.cpp: launch_for(... free_port()) runs before bind_to_port(o.host, o.port)). Under ctest -j, any other serve or backend on the runner can take the port in that window.

Seen on CI: ci run 42468438033 (PR #1437, a one-string change to onnx/ort_run.cpp): hybrid_route passed its first 27 checks, then failed with FAIL: hybrid_route: could not bind a port 45419 (Address already in use). The previous head and main were green.

Affected (the same block, copied): tests/hybrid_route.sh, tests/hybrid_kvshare_route.sh, tests/hybrid_e2e.sh, tests/hrx_cwsr_route.sh, tests/device_route.sh. Each already detects the collision ("a port collision, not a defect in what this test checks") but exits 1, so a runner-side race turns CI red.

Fix: when the bind check fires, reap that serve and retry the same start on a new port, up to 3 tries, printing a note line. Only a third consecutive collision fails. Test-only; serve's startup order is unchanged.

Activity

  1. bong-water-water-bong commented on Nov 8, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Claiming this — lane pi-i669, unowned (0 comments, no board entry) under the standing loop. It is the class I spent last night on from the other side: a harness that reports the wrong verdict about the code under test (I shipped a fix yesterday for a device test binary that selected zero cases and exited 0, and I lost four GPU arms today to a squatted port, so the check-then-use shape is very familiar).

    Plan, before touching anything: (1) read the five blocks and confirm they are byte-identical apart from the test name, so one fix covers five and no test silently keeps the old block; (2) reproduce the collision deterministically rather than waiting for CI — hold the chosen port in a background listener between the pick and the launch, which turns the race into a guaranteed collision, and assert the current script fails; (3) make the launch retry: a bounded re-pick on port-collision only, with the collision still distinguished from a real defect, and the retry counted in the log so a genuine bind problem cannot hide behind it; (4) re-run with the port held to show the retry passing, and once without any holder to show the normal path unchanged. If the five blocks are not identical I will fix them where they differ rather than normalising them for the sake of it.

  2. bong-water-water-bong commented on Nov 8, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Checked the three scripts I defered in #1446, and they are not the same state as the five — worth having on the record before someone assumes the class is finished:

    script check-then-use port pick detects the collision?
    tests/anchor_rerun.sh yes (bind(0) then launch) no — no EADDRINUSE/cannot listen on check at all
    tests/greedy_shortcut.sh yes no
    tests/laya_gguf_route.sh yes no
    the five in #1446 yes yes (that was the diagnosis being thrown away)

    So the five now retry a collision they already named, while these three would fail with a generic "serve did not answer /health" and no hint that the cause was a port race — a worse failure mode, since the reader has to work out that nothing about their feature is broken. grep -c 'could not bind a port' is 0 in all three.

    Finishing the class is the same change plus the detection the five already have (three files, ~20 lines each, same shape as #1446), and I am not doing it unprompted because #1444 names the five and #1446 is already READY — folding three more files in now would make a READY diff move under the reviewer. Two ways to take it, and either is fine by me:

    1. Follow-up PR against this issue ("finishes tests: five serve route tests fail on a front-port collision instead of retrying (check-then-use race, CI flake) #1444's class in the three scripts that never detected it"), which is my preference — it keeps tests: the five serve route tests retry a taken port instead of failing on it (closes #1444) #1446 mergeable as reviewed; or
    2. fold into tests: the five serve route tests retry a taken port instead of failing on it (closes #1444) #1446, in which case I will wait for the reviewer to say so rather than push to a READY head.

    The isolated retry evidence in #1446 applies unchanged; what the three need on top is simply the grep -qiE 'EADDRINUSE|address already in use|cannot listen on' guard before the fail — and because they do not have it today, their first run would also tell us whether the race even fires there (anchor_rerun.sh polls for up to an hour, so it is the most likely to be hit on a loaded runner).

  3. bong-water-water-bong commented on Nov 8, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Scope, found while proving the fix: 24 test scripts pick a front port with the same release-then-bind one-liner. The five named above are the ones that already detect the collision; the PR for this issue fixes those (retry on a new port, fail on a third collision in a row).

    The other 19 have no collision check, so a loss there shows up as a generic "did not answer" failure, not a port message: anchor_rerun, greedy_shortcut, hrx_cpu_weight_parity, hrx_runtime_route, laya_gguf_route, laya_models_e2e, laya_route_e2e, mem_admit_route, metrics_serve, npu_repack_path, parallel_args, pm_laya_route, pm_route, prism_route, recipes_route (3 picks), serve_e2e, smoke_serve (3 picks), watchdog_serve (.sh), and kvshare_fd_handoff.py.

    For those, copying the loop 19 more times is the wrong shape. The follow-up should be one shared helper, or serve --port 0 printing the port it bound, which removes the race instead of retrying around it. Not in the first PR.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions