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

Open
opened 2026-11-26 20:18:54 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/700
Author: @kirigayakazima
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: fix/gallery-port-bind-and-url


📝 Commits (1)

  • f690f3d gallery: disable Windows port double-bind, report bound --port 0 URL, and close socket in finally

📊 Changes

2 files changed (+112 additions, -13 deletions)

View changed files

📝 tests/test_gallery.py (+95 -0)
📝 tools/gallery.py (+17 -13)

📄 Description

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)

🔄 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/ubiquitarianism/leonida/pull/700 **Author:** [@kirigayakazima](https://github.com/kirigayakazima) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/gallery-port-bind-and-url` --- ### 📝 Commits (1) - [`f690f3d`](https://github.com/ubiquitarianism/leonida/commit/f690f3d501a218e8c23545d6932e50deb01c9ba4) gallery: disable Windows port double-bind, report bound --port 0 URL, and close socket in finally ### 📊 Changes **2 files changed** (+112 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_gallery.py` (+95 -0) 📝 `tools/gallery.py` (+17 -13) </details> ### 📄 Description 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) --- <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 labels
bug
pull-request
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
starred/leonida#742
No description provided.