Skip to content

Commit ada2a98

Browse files
committed
Fix: Ensure HealthCheck exec session terminates on timeout
Previously, the HealthCheck exec session would not terminate on timeout, allowing the healthcheck to run indefinitely. Fixes: https://issues.redhat.com/browse/RHEL-86096 Signed-off-by: Jan Rodák <[email protected]>
1 parent 45c03c9 commit ada2a98

File tree

6 files changed

+39
-20
lines changed

6 files changed

+39
-20
lines changed

cmd/podman/common/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ func DefineCreateFlags(cmd *cobra.Command, cf *entities.ContainerCreateOptions,
627627
createFlags.StringVar(
628628
&cf.HealthTimeout,
629629
healthTimeoutFlagName, define.DefaultHealthCheckTimeout,
630-
"the maximum time allowed to complete the healthcheck before an interval is considered failed",
630+
"the maximum time allowed to complete the healthcheck before an interval is considered failed and is send SIGTERM then SIGKILL after grace period (equal to timeout).",
631631
)
632632
_ = cmd.RegisterFlagCompletionFunc(healthTimeoutFlagName, completion.AutocompleteNone)
633633

docs/source/markdown/options/health-timeout.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
The maximum time allowed to complete the healthcheck before an interval is considered failed. Like start-period, the
88
value can be expressed in a time format such as **1m22s**. The default value is **30s**.
99

10-
Note: A timeout marks the healthcheck as failed but does not terminate the running process.
11-
This ensures that a slow but eventually successful healthcheck does not disrupt the container
12-
but is still accounted for in the health status.
10+
Note: A timeout marks the healthcheck as failed. If the healthcheck command itself runs longer than the specified *timeout*,
11+
it will be sent a `SIGTERM` signal. If the command does not exit within a grace period (equal in duration to the specified *timeout*),
12+
a `SIGKILL` signal is then sent.
1313

1414
Note: This parameter will overwrite related healthcheck configuration from the image.

libpod/container_exec.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ func (c *Container) ExecResize(sessionID string, newSize resize.TerminalSize) er
791791
return c.ociRuntime.ExecAttachResize(c, sessionID, newSize)
792792
}
793793

794-
func (c *Container) healthCheckExec(config *ExecConfig, streams *define.AttachStreams) (int, error) {
794+
func (c *Container) healthCheckExec(config *ExecConfig, timeout time.Duration, streams *define.AttachStreams) (int, error) {
795795
unlock := true
796796
if !c.batched {
797797
c.lock.Lock()
@@ -839,15 +839,24 @@ func (c *Container) healthCheckExec(config *ExecConfig, streams *define.AttachSt
839839
if err != nil {
840840
return -1, err
841841
}
842+
// TODO: Add Use of PIDdata (need rebase on #25906)
843+
session.PID = pid
842844

843845
if !c.batched {
844846
c.lock.Unlock()
845847
unlock = false
846848
}
847849

848-
err = <-attachErrChan
849-
if err != nil {
850-
return -1, fmt.Errorf("container %s light exec session with pid: %d error: %v", c.ID(), pid, err)
850+
select {
851+
case err = <-attachErrChan:
852+
if err != nil {
853+
return -1, fmt.Errorf("container %s light exec session with pid: %d error: %v", c.ID(), pid, err)
854+
}
855+
case <-time.After(timeout):
856+
if err := c.ociRuntime.ExecStopContainer(c, session.ID(), uint(timeout)); err != nil {
857+
return -1, err
858+
}
859+
return -1, define.ErrHealthCheckTimeout
851860
}
852861

853862
return c.readExecExitCode(session.ID())

libpod/define/errors.go

+3
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,7 @@ var (
217217
// ErrRemovingCtrs indicates that there was an error removing all
218218
// containers from a pod.
219219
ErrRemovingCtrs = errors.New("removing pod containers")
220+
221+
// ErrHealthCheckTimeout indicates that a HealthCheck timed out.
222+
ErrHealthCheckTimeout = errors.New("health check timeout")
220223
)

libpod/healthcheck.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,24 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
9393
streams.AttachInput = true
9494

9595
logrus.Debugf("executing health check command %s for %s", strings.Join(newCommand, " "), c.ID())
96-
timeStart := time.Now()
9796
hcResult := define.HealthCheckSuccess
9897
config := new(ExecConfig)
9998
config.Command = newCommand
100-
exitCode, hcErr := c.healthCheckExec(config, streams)
99+
timeStart := time.Now()
100+
exitCode, hcErr := c.healthCheckExec(config, c.HealthCheckConfig().Timeout, streams)
101+
timeEnd := time.Now()
101102
if hcErr != nil {
102103
hcResult = define.HealthCheckFailure
103-
if errors.Is(hcErr, define.ErrOCIRuntimeNotFound) ||
104+
switch {
105+
case errors.Is(hcErr, define.ErrOCIRuntimeNotFound) ||
104106
errors.Is(hcErr, define.ErrOCIRuntimePermissionDenied) ||
105-
errors.Is(hcErr, define.ErrOCIRuntime) {
107+
errors.Is(hcErr, define.ErrOCIRuntime):
106108
returnCode = 1
107109
hcErr = nil
108-
} else {
110+
case errors.Is(hcErr, define.ErrHealthCheckTimeout):
111+
returnCode = -1
112+
hcErr = fmt.Errorf("healthcheck command exceeded timeout of %s", c.HealthCheckConfig().Timeout.String())
113+
default:
109114
returnCode = 125
110115
}
111116
} else if exitCode != 0 {
@@ -140,7 +145,6 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
140145
hcResult = define.HealthCheckContainerStopped
141146
}
142147

143-
timeEnd := time.Now()
144148
if c.HealthCheckConfig().StartPeriod > 0 {
145149
// there is a start-period we need to honor; we add startPeriod to container start time
146150
startPeriodTime := c.state.StartedTime.Add(c.HealthCheckConfig().StartPeriod)
@@ -156,12 +160,6 @@ func (c *Container) runHealthCheck(ctx context.Context, isStartup bool) (define.
156160
eventLog = eventLog[:c.HealthCheckMaxLogSize()]
157161
}
158162

159-
if timeEnd.Sub(timeStart) > c.HealthCheckConfig().Timeout {
160-
returnCode = -1
161-
hcResult = define.HealthCheckFailure
162-
hcErr = fmt.Errorf("healthcheck command exceeded timeout of %s", c.HealthCheckConfig().Timeout.String())
163-
}
164-
165163
hcl := newHealthCheckLog(timeStart, timeEnd, returnCode, eventLog)
166164

167165
healthCheckResult, err := c.updateHealthCheckLog(hcl, hcResult, inStartPeriod, isStartup)

test/e2e/healthcheck_run_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,13 @@ HEALTHCHECK CMD ls -l / 2>&1`, ALPINE)
404404
Expect(ps.OutputToStringArray()).To(HaveLen(2))
405405
Expect(ps.OutputToString()).To(ContainSubstring("hc"))
406406
})
407+
408+
It("podman healthcheck - health timeout", func() {
409+
ctrName := "c-h-" + RandomString(6)
410+
podmanTest.PodmanExitCleanly("run", "-d", "--name", ctrName, "--health-cmd", "sleep 100; echo hc-done", "--health-timeout=3s", ALPINE, "top")
411+
412+
hc := podmanTest.Podman([]string{"healthcheck", "run", ctrName})
413+
hc.WaitWithTimeout(10)
414+
Expect(hc).Should(ExitWithError(125, "Error: healthcheck command exceeded timeout of 3s"))
415+
})
407416
})

0 commit comments

Comments
 (0)