diff --git a/libpod/container_api.go b/libpod/container_api.go index 52b7e145de..00d432a9bb 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -767,7 +767,7 @@ func (c *Container) WaitForConditionWithInterval(ctx context.Context, waitTimeou wg.Add(1) go func() { defer wg.Done() - + stoppedCount := 0 for { if len(wantedStates) > 0 { state, err := c.State() @@ -784,10 +784,17 @@ func (c *Container) WaitForConditionWithInterval(ctx context.Context, waitTimeou // even if we are interested only in the health check // check that the container is still running to avoid // waiting until the timeout expires. - state, err := c.State() - if err != nil { - trySend(-1, err) - return + if stoppedCount > 0 { + stoppedCount++ + } else { + state, err := c.State() + if err != nil { + trySend(-1, err) + return + } + if state != define.ContainerStateCreated && state != define.ContainerStateRunning && state != define.ContainerStatePaused { + stoppedCount++ + } } status, err := c.HealthCheckStatus() if err != nil { @@ -798,7 +805,9 @@ func (c *Container) WaitForConditionWithInterval(ctx context.Context, waitTimeou trySend(-1, nil) return } - if state != define.ContainerStateCreated && state != define.ContainerStateRunning && state != define.ContainerStatePaused { + // wait for another waitTimeout interval to give the health check process some time + // to record the healthy status. + if stoppedCount > 1 { trySend(-1, define.ErrCtrStopped) return }