Skip to content

fix(http): rebind the listener during TIME_WAIT (SO_REUSEADDR on POSIX) - #46

Merged
ctharvey merged 2 commits into
masterfrom
fix/listener-reuseaddr
Sep 27, 2026
Merged

ctharvey merged 2 commits into
masterfrom
fix/listener-reuseaddr

Conversation

@ctharvey

Copy link
Copy Markdown
Contributor

Fixes the 2026-09-27 menhir.archolith.dev 502. After a rebuild, yawn's beacon-menhir (in Caddy's shared network namespace) crash-looped on http_bind_failed for about 55 seconds, because the old instance's closed connections held TIME_WAIT on 127.0.0.1:3366.

  • _new_listener() sets SO_REUSEADDR on POSIX only, used by serve-http and serve --transport http. It's never set on Windows, where the flag would allow port stealing. A port another socket is listening on is still refused.
  • tests/test_listener_rebind.py (Linux and macOS CI): it creates a real server-side TIME_WAIT, checks that a plain socket gets EADDRINUSE (so the test reproduces the bug), that the new listener rebinds, and that a live listener still gets EADDRINUSE.
  • Full suite locally (Windows, which skips the POSIX tests): 1287 passed, 15 skipped. ruff and mypy are clean.

After merge: rerun yawn's beacon-demo target. beacon-menhir builds from master, so no image release is needed; it should come back with 0 restarts. beacon.archolith.dev isn't affected (its own namespace) and picks this up at the next release.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JNe816x81LFW6db8T7vtsE

ctharvey and others added 2 commits September 27, 2026 12:02
A beacon container sharing its proxy's network namespace crash-looped on
http_bind_failed for ~55 s after a rebuild: the previous instance's closed
connections held TIME_WAIT on the port. serve-http and serve --transport
http now create the listener via _new_listener(), setting SO_REUSEADDR on
POSIX only (never Windows, where it allows port stealing); a port another
socket listens on is still refused. POSIX tests reproduce TIME_WAIT.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JNe816x81LFW6db8T7vtsE
…s SO_REUSEADDR)

On Linux a rebind through TIME_WAIT needs SO_REUSEADDR on both the new listener and the listener that accepted the TIME_WAIT connection. The rebind test now replaces a fixed server with a fixed one; the plain-socket test keeps reproducing the pre-fix failure. CHANGELOG notes the first rollout may still wait out one TIME_WAIT.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JNe816x81LFW6db8T7vtsE
@ctharvey

Copy link
Copy Markdown
Contributor Author

Linux CI caught a real subtlety: a rebind through TIME_WAIT needs SO_REUSEADDR on both the new listener and the listener that accepted the TIME_WAIT connection, because TIME_WAIT sockets inherit the flag. So the fix applies from the second rebuild on (a fixed server replaced by a fixed one). The first switch from the current build may still wait out one TIME_WAIT (about a minute). The test now models the fixed-to-fixed case; the plain-socket test still reproduces the original failure.

@ctharvey
ctharvey merged commit 276e985 into master Sep 27, 2026
14 checks passed
@ctharvey
ctharvey deleted the fix/listener-reuseaddr branch September 27, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant