gallery: disable Windows port double-bind, report bound --port 0 URL, and close socket in finally (GALLERY-PORT-BIND-AND-URL) #700

Open
github-bot wants to merge 1 commit from github-bot/gh-700 into main AGit
Owner

@⁠kirigayakazima on GitHub · 2026-11-24 12:23 UTC · original
kirigayakazima:fix/gallery-port-bind-and-url → main · +112 −13 · 2 files · 1 commits · open

Claimed in the coordination issue as: GALLERY-PORT-BIND-AND-URL #14

What and why

Fixes three listening-socket and lifecycle bugs in tools/gallery.py (start_server):

  1. Windows double-bind / broken fallback (allow_reuse_address = os.name != "nt"): Just like ViewerServer in viewer/serve.py, http.server.HTTPServer defaults to allow_reuse_address = True. On Windows, SO_REUSEADDR lets a second ThreadingServer bind a port that another gallery instance is already actively listening on instead of raising OSError — silently breaking both GalleryListenError reporting and default --port (8080 -> 8081) fallback.
  2. Wrong URL for --port 0 (httpd.server_port): start_server built the printed URL and browser launch target from addr[1] instead of httpd.server_port, printing and opening http://localhost:0 when --port 0 asked the OS for an ephemeral port.
  3. Socket cleanup (try ... finally: httpd.server_close()): Moved httpd.server_close() into a finally block covering banner printing, webbrowser.open(url), and httpd.serve_forever(), so the listening socket closes cleanly on normal return or error as well as KeyboardInterrupt.

Adds 3 regression tests in tests/test_gallery.py (using real loopback sockets and synthetic empty temp dirs; no game files) and closes the mocked server_close in test_implicit_port_can_use_the_next_port.

Tested

Windows 11 x64, Python 3.14.7, CPU only, synthetic assets only (no game files). Commit f690f3d501a218e8c23545d6932e50deb01c9ba4.

  • python -I -W error::ResourceWarning tests/test_gallery.py -v:
    • against main (21625bca) with only the 3 new tests: all 3 new tests fail (AssertionError: 0 not greater than 0, AssertionError: GalleryListenError not raised, AssertionError: Lists differ: [] != [-1]);
    • with the fix: 32 tests, OK (0 skipped).
  • python -I tests/test_ci_registration.py: 15 tests, OK.
  • python -I tests/test_text_write_line_endings.py: 3 tests, OK.
  • python -I scripts/check_no_game_data.py tools/gallery.py tests/test_gallery.py, python -m compileall -q tools tests, git diff --check: clean (exit 0).

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)
**@⁠kirigayakazima** on GitHub · 2026-11-24 12:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/700) `kirigayakazima:fix/gallery-port-bind-and-url` → `main` · +112 −13 · 2 files · 1 commits · open Claimed in the coordination issue as: GALLERY-PORT-BIND-AND-URL https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 ### What and why Fixes three listening-socket and lifecycle bugs in `tools/gallery.py` (`start_server`): 1. **Windows double-bind / broken fallback (`allow_reuse_address = os.name != "nt"`)**: Just like `ViewerServer` in `viewer/serve.py`, `http.server.HTTPServer` defaults to `allow_reuse_address = True`. On Windows, `SO_REUSEADDR` lets a second `ThreadingServer` bind a port that another gallery instance is already actively listening on instead of raising `OSError` — silently breaking both `GalleryListenError` reporting and default `--port` (`8080` -> `8081`) fallback. 2. **Wrong URL for `--port 0` (`httpd.server_port`)**: `start_server` built the printed URL and browser launch target from `addr[1]` instead of `httpd.server_port`, printing and opening `http://localhost:0` when `--port 0` asked the OS for an ephemeral port. 3. **Socket cleanup (`try ... finally: httpd.server_close()`)**: Moved `httpd.server_close()` into a `finally` block covering banner printing, `webbrowser.open(url)`, and `httpd.serve_forever()`, so the listening socket closes cleanly on normal return or error as well as `KeyboardInterrupt`. Adds 3 regression tests in `tests/test_gallery.py` (using real loopback sockets and synthetic empty temp dirs; no game files) and closes the mocked `server_close` in `test_implicit_port_can_use_the_next_port`. ### Tested Windows 11 x64, Python 3.14.7, CPU only, synthetic assets only (no game files). Commit `f690f3d501a218e8c23545d6932e50deb01c9ba4`. - `python -I -W error::ResourceWarning tests/test_gallery.py -v`: - against `main` (`21625bca`) with only the 3 new tests: all 3 new tests fail (`AssertionError: 0 not greater than 0`, `AssertionError: GalleryListenError not raised`, `AssertionError: Lists differ: [] != [-1]`); - with the fix: **32 tests, OK** (0 skipped). - `python -I tests/test_ci_registration.py`: **15 tests, OK**. - `python -I tests/test_text_write_line_endings.py`: **3 tests, OK**. - `python -I scripts/check_no_game_data.py tools/gallery.py tests/test_gallery.py`, `python -m compileall -q tools tests`, `git diff --check`: clean (exit `0`). GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:17:01 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 12:29 UTC · original · approved these changes

Reviewed and verified GALLERY-PORT-BIND-AND-URL. Setting \llow_reuse_address = os.name != 'nt'\ prevents Windows \SO_REUSEADDR\ socket hijacking/double-binding on already listening ports, reporting \httpd.server_port\ properly reflects actual bound ephemeral ports when passing port 0, and invoking \httpd.server_close()\ in a \ inally\ block ensures clean teardown. Tested with comprehensive socket/mock tests in \ ests/test_gallery.py. Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 12:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/700#pullrequestreview-6039201154) · **approved these changes** Reviewed and verified GALLERY-PORT-BIND-AND-URL. Setting \llow_reuse_address = os.name != 'nt'\ prevents Windows \SO_REUSEADDR\ socket hijacking/double-binding on already listening ports, reporting \httpd.server_port\ properly reflects actual bound ephemeral ports when passing port 0, and invoking \httpd.server_close()\ in a \ inally\ block ensures clean teardown. Tested with comprehensive socket/mock tests in \ ests/test_gallery.py\. Approved.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/700/head:github-bot/gh-700'
git switch 'github-bot/gh-700'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-700'
git switch 'github-bot/gh-700'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-700'
git switch 'github-bot/gh-700'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-700'
git switch 'main'
git merge --squash 'github-bot/gh-700'
git switch 'main'
git merge --ff-only 'github-bot/gh-700'
git switch 'main'
git merge 'github-bot/gh-700'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.