libpod: remove extra state lookup in HealthCheck()

Checking the state outside of locks is not safe and does not guarantee
us anything as the container could be stopped afterwards anyways.

So just skip the state check and then later in the exec logic we do the
same check again and return ErrCtrStateInvalid so just handle that
afterwards.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2026-06-23 16:20:46 +02:00
committed by Matt Heon
parent 7d803ff592
commit 596cdd008c
2 changed files with 5 additions and 18 deletions

View File

@@ -28,9 +28,8 @@ func (r *Runtime) HealthCheck(ctx context.Context, name string) (define.HealthCh
return define.HealthCheckContainerNotFound, fmt.Errorf("unable to look up %s to perform a health check: %w", name, err)
}
hcStatus, err := checkHealthCheckCanBeRun(container)
if err != nil {
return hcStatus, err
if !container.HasHealthCheck() {
return define.HealthCheckNotDefined, fmt.Errorf("container %s has no defined healthcheck", container.ID())
}
isStartupHC := false
@@ -119,6 +118,8 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
if hcErr != nil {
hcResult = define.HealthCheckFailure
switch {
case errors.Is(hcErr, define.ErrCtrStateInvalid):
return define.HealthCheckContainerStopped, "", fmt.Errorf("container %s is not running: %w", c.ID(), hcErr)
case errors.Is(hcErr, define.ErrOCIRuntimeNotFound) ||
errors.Is(hcErr, define.ErrOCIRuntimePermissionDenied) ||
errors.Is(hcErr, define.ErrOCIRuntime):
@@ -219,20 +220,6 @@ func (c *Container) processHealthCheckStatus(status string) error {
return nil
}
func checkHealthCheckCanBeRun(c *Container) (define.HealthCheckStatus, error) {
cstate, err := c.State()
if err != nil {
return define.HealthCheckInternalError, err
}
if cstate != define.ContainerStateRunning {
return define.HealthCheckContainerStopped, fmt.Errorf("container %s is not running", c.ID())
}
if !c.HasHealthCheck() {
return define.HealthCheckNotDefined, fmt.Errorf("container %s has no defined healthcheck", c.ID())
}
return define.HealthCheckDefined, nil
}
// Increment the current startup healthcheck success counter.
// Can stop the startup HC and start the regular HC if the startup HC has enough
// consecutive successes.

View File

@@ -147,7 +147,7 @@ var _ = Describe("Podman healthcheck run", func() {
hc := podmanTest.Podman([]string{"healthcheck", "run", "hc"})
hc.WaitWithDefaultTimeout()
Expect(hc).Should(ExitWithError(125, "is not running"))
Expect(hc).Should(ExitWithError(125, "is not running: can only create exec sessions on running containers: container state improper"))
})
It("podman healthcheck on container without healthcheck", func() {