diff --git a/cgroup2/manager.go b/cgroup2/manager.go index 8bd2612..59e55f4 100644 --- a/cgroup2/manager.go +++ b/cgroup2/manager.go @@ -468,13 +468,33 @@ func (c *Manager) fallbackKill() error { } func (c *Manager) Delete() error { - // kernel prevents cgroups with running process from being removed, check the tree is empty - processes, err := c.Procs(true) + var ( + tasks []uint64 + threaded bool + ) + // Kernel prevents cgroups with running process from being removed, + // check the tree is empty. + // + // Pick the right file to read based on the cgs type. + cgType, err := c.GetType() + if err != nil { + if !os.IsNotExist(err) { + return err + } + } else { + threaded = cgType == Threaded + } + + if threaded { + tasks, err = c.Threads(true) + } else { + tasks, err = c.Procs(true) + } if err != nil { return err } - if len(processes) > 0 { - return fmt.Errorf("cgroups: unable to remove path %q: still contains running processes", c.path) + if len(tasks) > 0 { + return fmt.Errorf("cgroups: unable to remove path %q: still contains running tasks", c.path) } return remove(c.path) } diff --git a/cgroup2/manager_test.go b/cgroup2/manager_test.go index 738029b..c8ba0c5 100644 --- a/cgroup2/manager_test.go +++ b/cgroup2/manager_test.go @@ -284,7 +284,7 @@ func TestCgroupType(t *testing.T) { manager, err := NewManager(defaultCgroup2Path, "/test-type", ToResources(&specs.LinuxResources{})) require.NoError(t, err) t.Cleanup(func() { - os.RemoveAll(manager.path) + _ = manager.Delete() }) cgType, err := manager.GetType()