From 91d182216ffb67b7cbb64d095ccbc09c8d278b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juho=20M=C3=A4kinen?= Date: Thu, 3 Oct 2024 15:01:34 +1000 Subject: [PATCH] fix: infinite loop if port binding fails for all ports for some reason (#2972) --- internal/bind/bind_allocator.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/bind/bind_allocator.go b/internal/bind/bind_allocator.go index 5bc8932a98..d6291ed52f 100644 --- a/internal/bind/bind_allocator.go +++ b/internal/bind/bind_allocator.go @@ -33,11 +33,19 @@ func NewBindAllocator(url *url.URL) (*BindAllocator, error) { func (b *BindAllocator) NextPort() int { var l *net.TCPListener var err error + + maxTries := 5000 + + tries := 0 for { + tries++ port := int(b.port.Add(1)) l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP(b.baseURL.Hostname()), Port: port}) if err != nil { + if tries >= maxTries { + panic("failed to find an open port: " + err.Error()) + } continue } _ = l.Close()