FreeTCPAddr
returns duplicate ports, results in bind: address already in use
errors
#14837
Labels
FreeTCPAddr
returns duplicate ports, results in bind: address already in use
errors
#14837
Summary of Bug
We're seeing quite a few
bind: address already in use
issues in our cli tests, and I believe it's related to the way network.New finds ports usingFreeTCPAddr
.On Mac OS, calling
net.Listen
onlocalhost:0
generally produces monotonically increasing port numbers, so thebind: address already in use
error does not occur.On some distributions of Linux, however; the underlying TCP/IP stack is different, and the ports returned are more stochastic. As an experiment I created 10 goroutines which each called
FreeTcpAddr
100 times on an Alpine Linux distro. It was common to see that on the 50th or 60th call, that I would get a duplicate port.It's also important to note that when go tests are invoked with
go test ./...
, each package is run as a separate instance. Therefore, package level locks like this one don't work. Each package's tests will have a reference to a different lock.This means each package's
cli
tests are invokingnetwork.New
in parallel, and callingFreeTCPAddr
in parallel. Also note thatFreeTCPAddr
actually listens atlocalhost:0
, and then immediately closes the listener, and there is some time between this and actually starting the underlying gRPC server. Within this time the port is actually free again, and it's possible that other tests runningFreeTCPAddr
will claim the same port.I wonder if we can find a different way to find open ports for the
gRPC
servers rather than usingFreeTCPAddr
. For example, could we start the gRPC servers on port0
directly, rather than first callingFreeTCPAddr
, stopping the listener, and then starting the gRPC service on the returned port?Also linking this open issue which seems to reference this bug.
Version
0.47-alpha2
The text was updated successfully, but these errors were encountered: