Skip to content

BUILD-1193: Reject out-of-range gallery ports - #4193

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
akimfomin:fix/gallery-port-range
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
akimfomin:fix/gallery-port-range

Conversation

@akimfomin

Copy link
Copy Markdown

Claimed as BUILD-1193 in the coordination issue.

What and why

tools/gallery.py --port -1 and --port 65536 reached socket setup and raised OverflowError. Validate the 0–65535 range during command-line handling, before starting the server or preparing directories. Port 0, port 65535, and the default 8080 still reach the existing server startup path. This is separate from #4058, which reports the actual bound port.

Tested

  • Windows 11, Python 3.12.14. Both new out-of-range cases fail on the original code and pass after the fix.
  • Direct CLI checks for -1 and 65536 returned 2 with the range error, without a traceback or creating work directories.
  • python -B -I tests/test_gallery.py (5 passed), python -B -I tests/test_docs_cli_flags.py (3 passed), and git diff --check.
  • The two edited files combine cleanly with gallery: advertise the bound port when using --port 0 #4058 at 18cd8e2b.

Checklist

  • No game data, keys or decryption, including in logs or reports.
  • Translator contract update (not applicable to gallery argument handling).

AI-assisted implementation and testing. No GPU was needed.

Validate --port before starting the gallery. Add CLI regression tests for
invalid ports, both valid boundaries, and the default port.

BUILD-1193

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at 9096d806 on Windows 11: python -I tests/test_gallery.py -> 5 tests, OK; git diff --check clean.

The new command-line cases reject -1 and 65536 with exit 2 and --port must be between 0 and 65535 before start_server is called, and accept the default 8080 plus the boundary values 0 (ephemeral) and 65535. The existing server/endpoint tests still pass. No blocking issue.

@paratustra

Copy link
Copy Markdown
Contributor

Checked 9096d80 on macOS, Python 3.15.2. The range check works, but the boundary value it accepts can still crash. When the requested port is busy, start_server retries on port + 1 (tools/gallery.py:296-297). With 65535 busy, that retry is 65536:

# another socket listening on 127.0.0.1:65535
python3 -I tools/gallery.py --port 65535 --no-browser --models-dir E --textures-dir E
-> exit 1, traceback ending: OverflowError: bind(): port must be 0-65535.

For any other busy port, the studio moves to the next port without saying it was asked for another one. viewer/serve.py handles this case with ap.error("can't listen on port N: ... Is a viewer already running?") and exit 2. Doing the same here, instead of retrying on port + 1, would close the last path to the OverflowError this PR is about.

No game files. AI-assisted (Claude Code).

Keep the automatic fallback for the implicit default. An explicit unavailable port reports a command-line error, including port 65535, rather than retrying an adjacent port. Cover explicit failures, exhausted default fallback and successful default fallback.

AI-assisted.
@akimfomin

Copy link
Copy Markdown
Author

Thanks for finding the busy-port case, @paratustra. I reproduced the traceback on Windows with a loopback socket holding port 65535 and updated this PR at f21b1951.

An explicit --port now either binds that port or reports a clear error with exit code 2. If you omit --port, the existing 8080 → 8081 fallback remains. If both default ports are unavailable, the CLI reports the failure with exit code 2.

On Windows 11 the gallery suite passed 7/7 and the documented CLI checks passed 3/3. Live loopback checks covered busy 65535, a regular busy explicit port, both default ports busy, and the implicit fallback serving the gallery API at 8081. The gallery suite also passed 8/8 with the current #4058 patch applied alongside it, including the port-zero URL check. Synthetic inputs only. AI-assisted.

@paratustra

Copy link
Copy Markdown
Contributor

Re-checked f21b195 on macOS, Python 3.15.2. This resolves my busy-port note:

  • --port 65535 with the port held by another socket: exit 2, can't listen on port 65535: [Errno 48] Address already in use.
  • Any other explicit busy port behaves the same way.
  • --port 70000: exit 2, the range error.

No traceback in any case, and no server process was left running.

No game files. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested f21b195 merged with main de67135 on Linux, Python 3.12.16: pass

tests/test_gallery.py TestGalleryCommandLine: 4 tests OK. Negative control (tools/gallery.py from main, new tests kept): 5 failures, 3 errors, so the tests demonstrate the change. The whole file also contains the existing test_endpoints, which errors here with ConnectionRefused on loopback in WSL; it errors identically on main with the PR files removed, so it is environmental and unrelated. Behaviour note, not a defect: an explicit --port 8080 no longer falls back to 8081 (only the implicit default does), which the tests cover. Linux only.

@lines9686-rgb

Copy link
Copy Markdown
Contributor

Tested f21b195 on Windows 11 (26200), Python 3.12: pass. tests/test_gallery.py OK on this branch; against the base's code (aa2988c) the same file fails 5 and errors 3, the out-of-range port cases. Merges cleanly with main; compileall OK. No game files. AI-assisted (Claude Code).

@KkelebekK

Copy link
Copy Markdown

Reviewed and tested f21b1951 (alone and merged with main 3faac557), CPU only: Linux, Python 3.14.10. Since 9096d806, an explicit --port no longer retries on port + 1, and a failed bind becomes GalleryListenError and then parser.error (exit 2).

No blocking issues. Two notes:

  • The title and body still describe only the range check. They say port 0, 65535 and the default 8080 "still reach the existing server startup path", and they cite 5 gallery tests. The body does not mention that an explicit --port 8080 now exits 2 instead of moving to 8081. Please update the body.
  • When both default ports are busy, the error names only 8081 (can't listen on port 8081), although 8080 was tried first.

AI-assisted.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants