gallery: disable Windows port double-bind, report bound --port 0 URL, and close socket in finally (GALLERY-PORT-BIND-AND-URL) #700
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/700"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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):allow_reuse_address = os.name != "nt"): Just likeViewerServerinviewer/serve.py,http.server.HTTPServerdefaults toallow_reuse_address = True. On Windows,SO_REUSEADDRlets a secondThreadingServerbind a port that another gallery instance is already actively listening on instead of raisingOSError— silently breaking bothGalleryListenErrorreporting and default--port(8080->8081) fallback.--port 0(httpd.server_port):start_serverbuilt the printed URL and browser launch target fromaddr[1]instead ofhttpd.server_port, printing and openinghttp://localhost:0when--port 0asked the OS for an ephemeral port.try ... finally: httpd.server_close()): Movedhttpd.server_close()into afinallyblock covering banner printing,webbrowser.open(url), andhttpd.serve_forever(), so the listening socket closes cleanly on normal return or error as well asKeyboardInterrupt.Adds 3 regression tests in
tests/test_gallery.py(using real loopback sockets and synthetic empty temp dirs; no game files) and closes the mockedserver_closeintest_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: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]);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 (exit0).GPUs tested: n/a
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)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.
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)