fix: enable SO_REUSEADDR in native listener for rapid restarts - #27
Merged
Conversation
Pass `reuse_addr=true` when constructing the native HTTP server so a new process can immediately rebind a port that a previous process left in TIME_WAIT. Without it, repeated development restarts on the same address can fail with "Address already in use" even though no process is listening. Document the BSD/macOS side effect noted by moonbitlang/async: with reuse_addr enabled, a specific-address bind may steal an interface from a wildcard bind and vice versa. Co-Authored-By: Claude <noreply@anthropic.com>
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.
What
The native Mocket listener previously constructed
@http.Server(addr)withoutreuse_addr, soSO_REUSEADDRwas disabled (the pinnedmoonbitlang/async@0.21.0TcpServerdefaults it tofalse). During rapid development restarts, a socket left inTIME_WAITon the same127.0.0.1:PORTcan make the new process report "Address already in use" even though no process is listening.Fix
Pass
reuse_addr=truewhen building the native HTTP server:This lets a fresh process immediately rebind a port a previous process just left in
TIME_WAIT.Also
SO_REUSEADDRbehavior and themoonbitlang/asyncBSD/macOS side effect directly onlisten_ffi: with reuse enabled, a bind to a specific address may "steal" the interface from an existing wildcard0.0.0.0bind, and a wildcard bind can bind to the remaining interfaces if a specific-interface bind already exists.Verification
moon checkis green onnative,js, andwasm.responderexample on:4000, sent traffic (HTTP 200), killed the process, and immediately relaunched — the second server successfully rebinds the same port.Note
The "propagate bind failures as checked errors" improvement is a separate, larger API-signature change (the public
Mocket::listen/serveareUnit noraise); I scoped this PR to the primary fix.🤖 Generated with Claude Code