Skip to content

Commit

Permalink
fix: avoid race when allocating ports (#2781)
Browse files Browse the repository at this point in the history
The port can change between when we increment and when we return the
value. This can cause ports to not be unique when calling `Next()`.
  • Loading branch information
matt2e authored Sep 23, 2024
1 parent 36a7d27 commit c1a01c1
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/bind/bind_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func (b *BindAllocator) NextPort() int {
var l *net.TCPListener
var err error
for {
b.port.Add(1)
l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP(b.baseURL.Hostname()), Port: int(b.port.Load())})
port := int(b.port.Add(1))
l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP(b.baseURL.Hostname()), Port: port})

if err != nil {
continue
}
_ = l.Close()

return int(b.port.Load())
return port
}
}

Expand Down

0 comments on commit c1a01c1

Please sign in to comment.