Skip to content

Commit

Permalink
Merge pull request #22764 from giuseppe/give-more-time-to-healthcheck…
Browse files Browse the repository at this point in the history
…-status-change

libpod: wait another interval for healthcheck
  • Loading branch information
openshift-merge-bot[bot] authored May 28, 2024
2 parents 7ec22ab + 7f567a4 commit af8fe2b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
30 changes: 22 additions & 8 deletions libpod/container_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,16 @@ func (c *Container) Wait(ctx context.Context) (int32, error) {
// WaitForExit blocks until the container exits and returns its exit code. The
// argument is the interval at which checks the container's status.
func (c *Container) WaitForExit(ctx context.Context, pollInterval time.Duration) (int32, error) {
id := c.ID()
if !c.valid {
// if the container is not valid at this point as it was deleted,
// check if the exit code was recorded in the db.
exitCode, err := c.runtime.state.GetContainerExitCode(id)
if err == nil {
return exitCode, nil
}
return -1, define.ErrCtrRemoved
}

id := c.ID()
var conmonTimer time.Timer
conmonTimerSet := false

Expand Down Expand Up @@ -767,7 +772,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()
Expand All @@ -784,10 +789,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 {
Expand All @@ -798,7 +810,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
}
Expand Down
23 changes: 16 additions & 7 deletions test/system/260-sdnotify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,22 @@ READY=1" "Container log after healthcheck run"

run_podman rm -f -t0 $ctr

ctr=$(random_string)
run_podman run --name $ctr \
--health-cmd="touch /terminate" \
--sdnotify=healthy \
$IMAGE sh -c 'while test \! -e /terminate; do sleep 0.1; done; echo finished'
is "$output" "finished" "make sure container exited successfully"
run_podman rm -f -t0 $ctr
# Disable until the race condition https://github.com/containers/podman/issues/22760 is fixed
# ctr=$(random_string)
# run_podman run --name $ctr \
# --health-cmd="touch /terminate" \
# --sdnotify=healthy \
# $IMAGE sh -c 'while test \! -e /terminate; do sleep 0.1; done; echo finished'
# is "$output" "finished" "make sure container exited successfully"
# run_podman rm -f -t0 $ctr

# ctr=$(random_string)
# run_podman 12 run --name $ctr --rm \
# --health-cmd="touch /terminate" \
# --sdnotify=healthy \
# $IMAGE sh -c 'while test \! -e /terminate; do sleep 0.1; done; echo finished; exit 12'
# is "$output" "finished" "make sure container exited"
# run_podman rm -f -t0 $ctr

_stop_socat
}
Expand Down

1 comment on commit af8fe2b

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.