This repo contains code for reproducing port binding problems in Go and for understanding how socket stuff works in general.
Sometimes your TCP server written in Go fails with error bind: address already in use
It could happen in such cases as:
- Other process already listens on this port on the same address. (non interesting case)
- Server code explicitly disables SO_REUSEADDR (enabled by default in *nix systems)
and tries to bind on same port and address which have unfinished connection in
TIME_WAIT
state. - Client uses random port for itself and make a lot of reconnects so it exhausted all ephemeral ports
(left all potential available port in
TIME_WAIT
state) - Client uses the same port to itself
Or sometimes your TCP client fails bind to ephemeral port.
TODO: describe
Scenario #1: Can't bind on the same port by the server w/o SO_REUSEADDR
option if socket in TIME_WAIT state
- Server listens on 1215 port, close connection and exit after 2 seconds:
go run server/main.go -l 127.0.0.1:1215 -s 2
. - Client connects to 1215 port, exit without closing connection after 4 seconds:
go run client/main.go -e 127.0.0.1:1215 -s 5 -noclose
. Lefts server side inTIME_WAIT
state for 30s (depends on system settings). - Check connections statuses:
(netstat -a -n | head -n 2) ; (netstat -a -n | grep 1215)
. - Try to bind on port w/o
SO_REUSEADDR
option:go run server/main.go -l 127.0.0.1:1215 -s 2 -no-reuse-addr
(should fail if you are fast enough) - Try to bind on port with default mode:
go run server/main.go -l 127.0.0.1:1215 -s 2
(should works if no other process listens on the same ip:port pair)
-
SO_REUSEPORT, SO_REUSEADDR related discussion: golang/go#9661
-
SO_REUSEPORT/ADDR
-
«why we do not use SO_REUSEADDR on windows»: https://github.com/golang/go/commit/c3733b29d494995859bb6d6241797f67ece4c53d