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

cg2: Don't read cgroup.procs when deleting threaded cg #318

Merged
merged 1 commit into from
Jan 24, 2024
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why we don't try to remove and check for EBSUY instead?

Copy link
Member Author

@dcantah dcantah Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, and this is for what was here today, it's mainly for a nice error so I tried to keep in line with what was here already. I suppose we could just immediately remove(c.path) (which tries to handle a reasonable amount of ebusy looping) although afterwards we'd still need to know what file to read to surface some decent error if the remove didn't go through.

}
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