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

Open
opened 2026-11-07 16:31:43 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4255
Author: @ByteJuggler
Created: 10/26/2026
Status: 🔄 Open

Base: main ← Head: pr/port-in-use


📝 Commits (3)

  • 6742f84 fix(serve): refuse a port another program holds on 127.0.0.1, and say which program
  • 9ef1a51 tests: test_strata_bench's port-owner test finds serve/ when run as a script (tools\test_strata_bench.py)
  • f58068a fix(serve): a taken port still exits with status 2, as ap.error did

📊 Changes

4 files changed (+178 additions, -5 deletions)

View changed files

📝 serve/server.py (+73 -3)
📝 serve/test_server.py (+54 -0)
📝 tools/strata_bench.py (+27 -2)
📝 tools/test_strata_bench.py (+24 -0)

📄 Description

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".)

#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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4255 **Author:** [@ByteJuggler](https://github.com/ByteJuggler) **Created:** 10/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/port-in-use` --- ### 📝 Commits (3) - [`6742f84`](https://github.com/Niko1221/Strata/commit/6742f847a07c5f3813ba7f95721cddf9dfff4e98) fix(serve): refuse a port another program holds on 127.0.0.1, and say which program - [`9ef1a51`](https://github.com/Niko1221/Strata/commit/9ef1a515e22b7ef16a2e5f0b13d9b79a4cbaa9e1) tests: test_strata_bench's port-owner test finds serve/ when run as a script (tools\test_strata_bench.py) - [`f58068a`](https://github.com/Niko1221/Strata/commit/f58068a0bc82f41374d63a9d1338f5fbe9ed7475) fix(serve): a taken port still exits with status 2, as ap.error did ### 📊 Changes **4 files changed** (+178 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+73 -3) 📝 `serve/test_server.py` (+54 -0) 📝 `tools/strata_bench.py` (+27 -2) 📝 `tools/test_strata_bench.py` (+24 -0) </details> ### 📄 Description ## 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4975
No description provided.