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

Merged
kirigayakazima merged 1 commit from upstream-pr/700 into main 2026-11-28 13:04:10 +00:00
kirigayakazima commented 2026-11-24 12:23:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-24T12:23:19Z.


Claimed in the coordination issue as: GALLERY-PORT-BIND-AND-URL https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7131322740

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)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5472072174 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/700). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-24T12:23:19Z. --- Claimed in the coordination issue as: GALLERY-PORT-BIND-AND-URL https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7131322740 ### 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)
abnalstlll-byte commented 2026-11-24 12:29:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T12:29:28Z.

Review: APPROVED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6039201154 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/700#pullrequestreview-6039201154). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T12:29:28Z. **Review:** APPROVED --- 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.
MoonEyes commented 2026-11-28 13:04:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T13:04:04Z.


Red/green: tests/test_gallery.py 4 failed on main, 32 passed with the PR.
Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239665747 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/700#issuecomment-7239665747). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T13:04:04Z. --- Red/green: tests/test_gallery.py 4 failed on main, 32 passed with the PR. Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!700
No description provided.