Skip to content

Commit

Permalink
Merge pull request containerd#228 from jseba/delete_empty_cgroups_only
Browse files Browse the repository at this point in the history
Check that cgroup is empty before deleting
  • Loading branch information
kzys authored Oct 17, 2022
2 parents 38b9b00 + 6eb2c7a commit e3fea41
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Load(hierarchy Hierarchy, path Path, opts ...InitOpts) (Cgroup, error) {
for _, s := range pathers(subsystems) {
p, err := path(s.Name())
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if errors.Is(err, os.ErrNotExist) {
return nil, ErrCgroupDeleted
}
if err == ErrControllerNotActive {
Expand Down Expand Up @@ -228,6 +228,15 @@ func (c *cgroup) Delete() error {
}
var errs []string
for _, s := range c.subsystems {
// kernel prevents cgroups with running process from being removed, check the tree is empty
procs, err := c.processes(s.Name(), true, cgroupProcs)
if err != nil {
return err
}
if len(procs) > 0 {
errs = append(errs, fmt.Sprintf("%s (contains running processes)", string(s.Name())))
continue
}
if d, ok := s.(deleter); ok {
sp, err := c.path(s.Name())
if err != nil {
Expand All @@ -247,6 +256,7 @@ func (c *cgroup) Delete() error {
if err := remove(path); err != nil {
errs = append(errs, path)
}
continue
}
}
if len(errs) > 0 {
Expand Down
8 changes: 8 additions & 0 deletions v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,14 @@ func (c *Manager) AddThread(tid uint64) 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)
if err != nil {
return err
}
if len(processes) > 0 {
return fmt.Errorf("cgroups: unable to remove path %q: still contains running processes", c.path)
}
return remove(c.path)
}

Expand Down

0 comments on commit e3fea41

Please sign in to comment.