Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.1] runc delete: call systemd's reset-failed #3932

Merged
merged 5 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions libcontainer/cgroups/systemd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@ retry:
// In case a unit with the same name exists, this may
// be a leftover failed unit. Reset it, so systemd can
// remove it, and retry once.
resetFailedUnit(cm, unitName)
err = resetFailedUnit(cm, unitName)
if err != nil {
logrus.Warnf("unable to reset failed unit: %v", err)
}
retry = false
goto retry
}
Expand All @@ -385,11 +388,11 @@ retry:
close(statusChan)
// Please refer to https://pkg.go.dev/github.com/coreos/go-systemd/v22/dbus#Conn.StartUnit
if s != "done" {
resetFailedUnit(cm, unitName)
_ = resetFailedUnit(cm, unitName)
return fmt.Errorf("error creating systemd unit `%s`: got `%s`", unitName, s)
}
case <-timeout.C:
resetFailedUnit(cm, unitName)
_ = resetFailedUnit(cm, unitName)
return errors.New("Timeout waiting for systemd to create " + unitName)
}

Expand Down Expand Up @@ -417,16 +420,17 @@ func stopUnit(cm *dbusConnManager, unitName string) error {
return errors.New("Timed out while waiting for systemd to remove " + unitName)
}
}

// In case of a failed unit, let systemd remove it.
_ = resetFailedUnit(cm, unitName)

return nil
}

func resetFailedUnit(cm *dbusConnManager, name string) {
err := cm.retryOnDisconnect(func(c *systemdDbus.Conn) error {
func resetFailedUnit(cm *dbusConnManager, name string) error {
return cm.retryOnDisconnect(func(c *systemdDbus.Conn) error {
return c.ResetFailedUnitContext(context.TODO(), name)
})
if err != nil {
logrus.Warnf("unable to reset failed unit: %v", err)
}
}

func getUnitTypeProperty(cm *dbusConnManager, unitName string, unitType string, propertyName string) (*systemdDbus.Property, error) {
Expand Down
1 change: 0 additions & 1 deletion tests/integration/cgroups.bats
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ function setup() {
set_cgroups_path
# CPU shares of 3333 corresponds to CPU weight of 128.
update_config ' .linux.resources.memory |= {"limit": 33554432}
| .linux.resources.memorySwap |= {"limit": 33554432}
| .linux.resources.cpu |= {
"shares": 3333,
"quota": 40000,
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/delete.bats
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,30 @@ EOF
# check delete subcgroups success
[ ! -d "$CGROUP_PATH"/foo ]
}

@test "runc delete removes failed systemd unit" {
requires systemd_v244 # Older systemd lacks RuntimeMaxSec support.

set_cgroups_path
# shellcheck disable=SC2016
update_config ' .annotations += {
"org.systemd.property.RuntimeMaxSec": "2",
"org.systemd.property.TimeoutStopSec": "1"
}
| .process.args |= ["/bin/sleep", "10"]'

runc run -d --console-socket "$CONSOLE_SOCKET" test-failed-unit
[ "$status" -eq 0 ]

wait_for_container 10 1 test-failed-unit stopped

local user=""
[ $EUID -ne 0 ] && user="--user"

# Expect "unit is not active" exit code.
run -3 systemctl status $user "$SD_UNIT_NAME"

runc delete test-failed-unit
# Expect "no such unit" exit code.
run -4 systemctl status $user "$SD_UNIT_NAME"
}
6 changes: 6 additions & 0 deletions tests/integration/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ function requires() {
skip_me=1
fi
;;
systemd_v*)
var=${var#systemd_v}
if [ "$(systemd_version)" -lt "$var" ]; then
skip "requires systemd >= v${var}"
fi
;;
no_systemd)
if [ -n "${RUNC_USE_SYSTEMD}" ]; then
skip_me=1
Expand Down