Repository navigation
listeners: bind the Websocket listener at Init and report the bound address - #542
Open
ChristophNetsch wants to merge 2 commits into
Open
ChristophNetsch wants to merge 2 commits into
ChristophNetsch wants to merge 2 commits into
Conversation
added 2 commits
September 3, 2026 01:42
…Messages RetainMessage writes particle.retainPath under the root and particle locks; scanMessages (a subscriber's retained-message scan, via Messages) read it with no lock at all. A retained publish racing a wildcard SUBSCRIBE is a data race the race detector reports as: Write at ... by (*TopicsIndex).RetainMessage topics.go:461 Previous read at ... by (*TopicsIndex).scanMessages topics.go:554 This is mochi-mqtt#200, closed as not reproducible; the added test reproduces it deterministically under -race and passes with the two reads locked.
The TCP listener binds in Init and Address() reports the socket's own address; the Websocket listener built its http.Server in Init and bound only inside Serve (ListenAndServe), so Address() echoed the configured string. Two consequences: a ":0" address could never be reported, and the port was not held between Init and Serve — a caller that pre-resolved ":0" by binding and releasing could see another listener take the port in between, after which Serve failed with "address already in use" and the address it had advertised was served by something else. Bind in Init like the TCP listener, serve that socket (Serve / ServeTLS), report the bound address, release the socket on Close even when Serve never ran, and refuse an unbindable address at Init where the caller can see it.
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
listeners.Websocketbuilds itshttp.ServerinInitbut binds only insideServe(ListenAndServe/ListenAndServeTLS), soAddress()echoes the configured string. Two consequences:":0"address can never be reported — the caller learns no port.InitandServe. A caller that works around (1) by pre-resolving":0"(bind, read the port, release, hand the concrete address toNewWebsocket) can see another listener in the same process take the port in between.Servethen fails withaddress already in use, and the address the caller already advertised is served by something else. We hit exactly this in our CI: a wss dial landed on an unrelated HTTPS server that had been given the released port, and failed withwebsocket: bad handshake.The
TCPlistener already does the right thing: it binds inInitandAddress()returnslisten.Addr().String().Change
Websocketbinds inInitlikeTCP, serves that socket (Serve(ln)/ServeTLS(ln, "", "")), reports the bound address fromAddress(), releases the socket onCloseeven whenServenever ran (http.Server.Shutdownonly closes listenersServeregistered), and refuses an unbindable address atInitwhere the caller can see the error.Behavioural differences a caller can observe:
Initnow returns a bind error for an unbindable address (TestWebsocketFailedToServeadapted: the error surfaces atInit;Serveon an unbound listener logs and returns instead of panicking).Address()afterInitreturns the bound address (e.g.[::]:1882for a config of:1882), consistent with theTCPlistener (TestServerAddListenersFromConfigadapted).New test:
TestWebsocketAddressIsTheBoundPortAfterInit— a":0"listener reports a real port afterInit, accepts a TCP dial beforeServe, and a secondnet.Listenon that port fails.