From f76cdc8ad35d46f1504ffc6743a908694b85beb7 Mon Sep 17 00:00:00 2001 From: Geoff Flarity Date: Fri, 29 Sep 2023 07:15:02 -0400 Subject: [PATCH] avoid race conditions by waiting one polling interval before checking ports are bound (#1697) --- wait/http.go | 6 ++++-- wait/http_test.go | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/wait/http.go b/wait/http.go index 94ff12bc89..ad99c6c73e 100644 --- a/wait/http.go +++ b/wait/http.go @@ -146,8 +146,10 @@ func (ws *HTTPStrategy) WaitUntilReady(ctx context.Context, target StrategyTarge var mappedPort nat.Port if ws.Port == "" { - ports, err := target.Ports(ctx) - for err != nil { + var err error + var ports nat.PortMap + // we wait one polling interval before we grab the ports otherwise they might not be bound yet on startup + for err != nil || ports == nil { select { case <-ctx.Done(): return fmt.Errorf("%w: %w", ctx.Err(), err) diff --git a/wait/http_test.go b/wait/http_test.go index f1df456bcd..8ae6075451 100644 --- a/wait/http_test.go +++ b/wait/http_test.go @@ -562,7 +562,8 @@ func TestHttpStrategyFailsWhileGettingPortDueToNoExposedPorts(t *testing.T) { }, StateImpl: func(_ context.Context) (*types.ContainerState, error) { return &types.ContainerState{ - Status: "running", + Status: "running", + Running: true, }, nil }, PortsImpl: func(ctx context.Context) (nat.PortMap, error) { @@ -602,7 +603,8 @@ func TestHttpStrategyFailsWhileGettingPortDueToOnlyUDPPorts(t *testing.T) { }, StateImpl: func(_ context.Context) (*types.ContainerState, error) { return &types.ContainerState{ - Status: "running", + Running: true, + Status: "running", }, nil }, PortsImpl: func(ctx context.Context) (nat.PortMap, error) { @@ -649,7 +651,8 @@ func TestHttpStrategyFailsWhileGettingPortDueToExposedPortNoBindings(t *testing. }, StateImpl: func(_ context.Context) (*types.ContainerState, error) { return &types.ContainerState{ - Status: "running", + Running: true, + Status: "running", }, nil }, PortsImpl: func(ctx context.Context) (nat.PortMap, error) {