#4255 [Other] serve: refuse a port another program holds on 127.0.0.1, and say which program

open · @ByteJuggler · 0 Kommentare · Auf GitHub

BenchmarksSetup & installServer & APIAMD / HIPNVIDIA / CUDAWindowsLinux

Beschreibung

## Problem and result

Replaces #2168, which was closed in the 0.2.0-beta tracker reset; its branch was rebased on current `main`, so GitHub cannot reopen it.

On Windows, Strata on `0.0.0.0:8080` can start beside another program that holds `127.0.0.1:8080` (Steam's `steamwebhelper.exe` does on some PCs). The server says `ready`, but every client on the PC (web app, chat.py, coding tools, BENCH) reaches the other program, and the bench waits "still loading" for 10 minutes. Windows allows a wildcard bind next to another process's specific bind unless the socket uses `SO_EXCLUSIVEADDRUSE`. `SO_REUSEADDR` is already off (#769).

After this change the start fails before the model loads, and the message names the program:

```
[strata] cannot start: port 8080 is already in use by steamwebhelper.exe (PID 36952) on 127.0.0.1.
It listens only on this PC's own address, but programs on this PC (the web app, chat.py, your coding tools) use http://127.0.0.1:8080 and would reach it instead of Strata.
Close that program, or give Strata another port (8081 is free now):
  - this start only: add --port 8081
  - from now on: run setup again with --port 8081 (Windows: SETUP.bat --port 8081; Linux: ./setup.sh --setup --port 8081)
  then point your clients at http://127.0.0.1:8081/v1
```

The bench's port check now counts any listener on the port, not only one that answers `/health` with JSON (Steam answers 404), and names it:

```
Port 8080 on this PC already answers, but not as Strata: it is steamwebhelper.exe (PID 36952) on 127.0.0.1.
Close that program, or run the benchmark on another port: BENCH.bat --port 8081 (or python tools/strata_bench.py --port 8081).
```

(Both outputs are from the original PR, run with Steam holding the port; see "Not run".)

## Related discussion

#2168, where @qiuzan2001 reviewed it. Both points are addressed here:
- `tools/test_strata_bench.py` run as a script could not import `serve` (`ModuleNotFoundError`): the test now puts the repo root on `sys.path`.
- The port message replaced `ap.error`, and the start exited with status 1 instead of 2: it now prints the message and exits with 2, and a test starts the real server on a taken port and checks the status and that no usage line is printed.

Focused fix.

## Approach

- `Server.server_bind`: `SO_EXCLUSIVEADDRUSE` on Windows only. The existing early test bind in `main()` then fails before the model loads (`WinError 10048`). Linux and Mac are unchanged.
- The "port in use" message names the program and PID (psutil, when present; otherwise it gives the `netstat`/`ss` command), says when it is probably Strata itself, and offers a port that is free now. It is printed without argparse's usage line in front, with the same exit status 2.
- `tools/strata_bench.py`: the pre-check counts any listener on the port and names it.

## Validation

**Passed:**
- On a0e6ac41 (rebased on `main` 5e8a4cca, 0.2.7), Windows 11, Python 3.12:
  - `python -m unittest discover -s serve -p "test_*.py" -t .`: 1220 tests OK (47 skipped).
  - `python tools/test_strata_bench.py` (run as a script): 24 tests OK.
- The same tests with `main` 5e8a4cca's `serve/server.py` and `tools/strata_bench.py`:
  - `test_all_addresses_cannot_be_bound_beside_a_loopback_listener` (a real-socket test: `0.0.0.0:P` must not bind beside a `127.0.0.1:P` listener) fails, so the bug is still present on 0.2.7.
  - `test_a_taken_port_exits_2_without_the_usage_line` fails there (`ap.error` prints the usage line), the message test errors, and the two bench port-check tests error.

**Failed:**
- None observed.

**Not run:**
- The Steam case by hand on 0.2.7. It was checked with a live `steamwebhelper.exe` listener before the tracker reset (the outputs above); on 0.2.7 only the real-socket test was run.
- Linux and Mac runs: the bind change is Windows-only, and the message and bench changes are covered by the tests above.
- CUDA, HIP, SYCL and Metal builds: no engine code changed.

## Limits

Program names need psutil. Without it the message falls back to the `netstat`/`ss` command.

Mehr auf der Site

Links zu Install, Modellen, Releases.