Skip to content

Commit

Permalink
cg2: Don't read cgroup.procs when deleting threaded cg
Browse files Browse the repository at this point in the history
Reading cgroup.procs seems to return ENOTSUPP when threaded, so check
the type of the cg when going to delete and read the relevant file.

Signed-off-by: Danny Canter <[email protected]>
  • Loading branch information
dcantah committed Dec 31, 2023
1 parent 1e05688 commit 6f5001d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
28 changes: 24 additions & 4 deletions cgroup2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cgroup2/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6f5001d

Please sign in to comment.