Skip to content

Commit 511c8b2

Browse files
committed
Remove timer for HealthCheck when container is paused.
If is unpaused then new timer for HealthCheck is created. Fixes: https://issues.redhat.com/browse/RUN-2468 Signed-off-by: Jan Rodák <[email protected]>
1 parent 3857bff commit 511c8b2

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

libpod/container_internal.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,14 @@ func (c *Container) pause() error {
15461546
}
15471547
}
15481548

1549+
if c.state.HCUnitName != "" {
1550+
if err := c.removeTransientFiles(context.Background(),
1551+
c.config.StartupHealthCheckConfig != nil && !c.state.StartupHCPassed,
1552+
c.state.HCUnitName); err != nil {
1553+
return fmt.Errorf("failed to remove HealthCheck timer: %v", err)
1554+
}
1555+
}
1556+
15491557
if err := c.ociRuntime.PauseContainer(c); err != nil {
15501558
// TODO when using docker-py there is some sort of race/incompatibility here
15511559
return err
@@ -1554,6 +1562,7 @@ func (c *Container) pause() error {
15541562
logrus.Debugf("Paused container %s", c.ID())
15551563

15561564
c.state.State = define.ContainerStatePaused
1565+
c.state.HCUnitName = ""
15571566

15581567
return c.save()
15591568
}
@@ -1569,6 +1578,28 @@ func (c *Container) unpause() error {
15691578
return err
15701579
}
15711580

1581+
isStartupHealthCheck := c.config.StartupHealthCheckConfig != nil && !c.state.StartupHCPassed
1582+
isHealthCheckEnabled := c.config.HealthCheckConfig != nil &&
1583+
!(len(c.config.HealthCheckConfig.Test) == 1 && c.config.HealthCheckConfig.Test[0] == "NONE")
1584+
if isHealthCheckEnabled || isStartupHealthCheck {
1585+
timer := c.config.HealthCheckConfig.Interval.String()
1586+
if isStartupHealthCheck {
1587+
timer = c.config.StartupHealthCheckConfig.Interval.String()
1588+
}
1589+
if err := c.createTimer(timer, isStartupHealthCheck); err != nil {
1590+
return err
1591+
}
1592+
}
1593+
1594+
if isHealthCheckEnabled {
1595+
if err := c.updateHealthStatus(define.HealthCheckReset); err != nil {
1596+
return err
1597+
}
1598+
if err := c.startTimer(isStartupHealthCheck); err != nil {
1599+
return err
1600+
}
1601+
}
1602+
15721603
logrus.Debugf("Unpaused container %s", c.ID())
15731604

15741605
c.state.State = define.ContainerStateRunning

test/system/080-pause.bats

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55

66
load helpers
7+
load helpers.systemd
78

89
# bats test_tags=distro-integration, ci:parallel
910
@test "podman pause/unpause" {
@@ -86,4 +87,52 @@ load helpers
8687

8788
run_podman rm -t 0 -f $cname $cname_notrunning
8889
}
90+
91+
# bats test_tags=ci:parallel
92+
@test "podman pause/unpause with HealthCheck interval" {
93+
if is_rootless && ! is_cgroupsv2; then
94+
skip "'podman pause' (rootless) only works with cgroups v2"
95+
fi
96+
97+
local ctrname="c-$(safename)"
98+
local msg="healthmsg-$(random_string)"
99+
100+
run_podman run -d --name $ctrname \
101+
--health-cmd "echo $msg" \
102+
--health-interval 1s \
103+
$IMAGE /home/podman/pause
104+
cid="$output"
105+
106+
run_podman healthcheck run $ctrname
107+
is "$output" "" "output from 'podman healthcheck run'"
108+
109+
run -0 systemctl status $cid-*.{service,timer}
110+
assert "$output" =~ "active" "service should be running"
111+
112+
run_podman --noout pause $ctrname
113+
assert "$output" == "" "output should be empty"
114+
115+
run -0 systemctl status $cid-*.{service,timer}
116+
assert "$output" == "" "service should not be running"
117+
118+
run_podman --noout unpause $ctrname
119+
assert "$output" == "" "output should be empty"
120+
121+
run_podman healthcheck run $ctrname
122+
is "$output" "" "output from 'podman healthcheck run'"
123+
124+
run -0 systemctl status $cid-*.{service,timer}
125+
assert "$output" =~ "active" "service should be running"
126+
127+
run_podman rm -t 0 -f $ctrname
128+
129+
# Important check for https://github.com/containers/podman/issues/22884
130+
# We never should leak the unit files, healthcheck uses the cid in name so just grep that.
131+
# (Ignore .scope units, those are conmon and can linger for 5 minutes)
132+
# (Ignore .mount, too. They are created/removed by systemd based on the actual real mounts
133+
# on the host and that is async and might be slow enough in CI to cause failures.)
134+
run -0 systemctl list-units --quiet "*$cid*"
135+
except_scope_mount=$(grep -vF ".scope " <<<"$output" | { grep -vF ".mount" || true; } )
136+
assert "$except_scope_mount" == "" "Healthcheck systemd unit cleanup: no units leaked"
137+
}
89138
# vim: filetype=sh

0 commit comments

Comments
 (0)