Repository navigation
fix(desktop): LAN rebind fails with EADDRINUSE over TIME_WAIT sockets - #94
Merged
Merged
Conversation
…E_WAIT Uvicorn closes accepted keep-alive connections server-side during shutdown, leaving TIME_WAIT sockets pinned to the old address for ~15-30 s on macOS. With the listener deliberately binding without SO_REUSEADDR, the loopback→LAN rebind then failed with EADDRINUSE (Errno 48, BIND_CHANGE_FAILED) even though no listener held the port — deterministically, whenever a web client had ever connected. - _bind_listener now sets SO_REUSEADDR. The double-bind protection is preserved where it matters: find_available_port still probes without it, SO_REUSEPORT stays off, and double launches are arbitrated by the instance manifest/port handoff (issue #4). - stop() force-exits uvicorn if the graceful join times out, so a wedged client can no longer leave the port held across a rebind. - Regression test plants a server-side-closed connection (TIME_WAIT) on the exact bind address; verified to fail on the previous code. Also note: the earlier e227bd9 route test closes its HTTP connection client-first, so it never produced a server-side TIME_WAIT — that is why this shipped green.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Toggling LAN exposure in the web UI logged
BIND_CHANGE_FAILED host="0.0.0.0" error="... [Errno 48] Address already in use"— deterministically, on every attempt once a web client had ever connected. The server correctly reverted to loopback, so LAN mode was effectively unreachable.Root cause
The rebind path (
POST /api/system/bind, #93) stops uvicorn, then re-binds a fresh listener. During shutdown uvicorn closes the accepted keep-alive connections server-side, leaving TIME_WAIT sockets pinned to the old address for ~15–30 s on macOS.BackgroundServer._bind_listener()deliberately bound withoutSO_REUSEADDR, and on macOS/BSD a wildcard bind conflicts with same-port TIME_WAIT sockets →EADDRINUSEeven though no listener holds the port.A minimal socket repro confirmed the mechanism; it also explains why #93's tests shipped green: module-level
httpx.post()closes its connection client-first, which never produces a server-side TIME_WAIT — real browsers keep-alive, so only production hit it.Fix
_bind_listener()now setsSO_REUSEADDR. The protections the original omission cared about are preserved where they matter:find_available_port()still probes without it, so port discovery keeps refusing occupied ports;SO_REUSEPORTstays off (that's the option that lets two servers silently share a listener);stop()now force-exits uvicorn when the graceful 3 s join times out (wedged long-lived request), so the port is actually released before a subsequentstart().test_start_and_rebind_succeed_with_timewait_socketsplants a real server-side TIME_WAIT on the exact bind address, then starts and rebinds. Verified to fail on the previous code and pass with the fix.Testing
tests/desktop+tests/server+tests/web: 182 passedruff check/ruff formatclean; pre-commit hooks passed