Skip to content

Commit

Permalink
Merge pull request #318 from dcantah/cg-type-delete
Browse files Browse the repository at this point in the history
  • Loading branch information
fuweid authored Jan 24, 2024
2 parents c00d22e + 6f5001d commit d131035
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 d131035

Please sign in to comment.