Merge pull request #2503 from giuseppe/cgroup-fixes

cgroup, systemd: cleanup cgroups
This commit is contained in:
Mrunal Patel 2020-07-06 15:14:29 -07:00 committed by GitHub
commit 30dc54a995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 16 deletions

View File

@ -27,6 +27,9 @@ var (
versionOnce sync.Once versionOnce sync.Once
version int version int
versionErr error versionErr error
isRunningSystemdOnce sync.Once
isRunningSystemd bool
) )
// NOTE: This function comes from package github.com/coreos/go-systemd/util // NOTE: This function comes from package github.com/coreos/go-systemd/util
@ -37,11 +40,11 @@ var (
// checks whether /run/systemd/system/ exists and is a directory. // checks whether /run/systemd/system/ exists and is a directory.
// http://www.freedesktop.org/software/systemd/man/sd_booted.html // http://www.freedesktop.org/software/systemd/man/sd_booted.html
func IsRunningSystemd() bool { func IsRunningSystemd() bool {
fi, err := os.Lstat("/run/systemd/system") isRunningSystemdOnce.Do(func() {
if err != nil { fi, err := os.Lstat("/run/systemd/system")
return false isRunningSystemd = err == nil && fi.IsDir()
} })
return fi.IsDir() return isRunningSystemd
} }
// systemd represents slice hierarchy using `-`, so we need to follow suit when // systemd represents slice hierarchy using `-`, so we need to follow suit when

View File

@ -222,7 +222,14 @@ func (m *legacyManager) Destroy() error {
return err return err
} }
unitName := getUnitName(m.cgroups) unitName := getUnitName(m.cgroups)
if err := stopUnit(dbusConnection, unitName); err != nil {
err = stopUnit(dbusConnection, unitName)
// Both on success and on error, cleanup all the cgroups we are aware of.
// Some of them were created directly by Apply() and are not managed by systemd.
if err := cgroups.RemovePaths(m.paths); err != nil {
return err
}
if err != nil {
return err return err
} }
m.paths = make(map[string]string) m.paths = make(map[string]string)

View File

@ -12,24 +12,24 @@ function teardown() {
} }
@test "runc delete" { @test "runc delete" {
# run busybox detached runc run -d --console-socket $CONSOLE_SOCKET testbusyboxdelete
runc run -d --console-socket $CONSOLE_SOCKET test_busybox
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
# check state testcontainer testbusyboxdelete running
testcontainer test_busybox running
runc kill test_busybox KILL runc kill testbusyboxdelete KILL
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
# wait for busybox to be in the destroyed state retry 10 1 eval "__runc state testbusyboxdelete | grep -q 'stopped'"
retry 10 1 eval "__runc state test_busybox | grep -q 'stopped'"
# delete test_busybox runc delete testbusyboxdelete
runc delete test_busybox
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
runc state test_busybox runc state testbusyboxdelete
[ "$status" -ne 0 ] [ "$status" -ne 0 ]
run find /sys/fs/cgroup -wholename '*testbusyboxdelete*' -type d
[ "$status" -eq 0 ]
[ "$output" = "" ] || fail "cgroup not cleaned up correctly: $output"
} }
@test "runc delete --force" { @test "runc delete --force" {