Skip to content

Commit e3fea41

Browse files
author
Kazuyoshi Kato
authored
Merge pull request #228 from jseba/delete_empty_cgroups_only
Check that cgroup is empty before deleting
2 parents 38b9b00 + 6eb2c7a commit e3fea41

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

cgroup.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func Load(hierarchy Hierarchy, path Path, opts ...InitOpts) (Cgroup, error) {
8484
for _, s := range pathers(subsystems) {
8585
p, err := path(s.Name())
8686
if err != nil {
87-
if errors.Is(err, os.ErrNotExist) {
87+
if errors.Is(err, os.ErrNotExist) {
8888
return nil, ErrCgroupDeleted
8989
}
9090
if err == ErrControllerNotActive {
@@ -228,6 +228,15 @@ func (c *cgroup) Delete() error {
228228
}
229229
var errs []string
230230
for _, s := range c.subsystems {
231+
// kernel prevents cgroups with running process from being removed, check the tree is empty
232+
procs, err := c.processes(s.Name(), true, cgroupProcs)
233+
if err != nil {
234+
return err
235+
}
236+
if len(procs) > 0 {
237+
errs = append(errs, fmt.Sprintf("%s (contains running processes)", string(s.Name())))
238+
continue
239+
}
231240
if d, ok := s.(deleter); ok {
232241
sp, err := c.path(s.Name())
233242
if err != nil {
@@ -247,6 +256,7 @@ func (c *cgroup) Delete() error {
247256
if err := remove(path); err != nil {
248257
errs = append(errs, path)
249258
}
259+
continue
250260
}
251261
}
252262
if len(errs) > 0 {

v2/manager.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ func (c *Manager) AddThread(tid uint64) error {
345345
}
346346

347347
func (c *Manager) Delete() error {
348+
// kernel prevents cgroups with running process from being removed, check the tree is empty
349+
processes, err := c.Procs(true)
350+
if err != nil {
351+
return err
352+
}
353+
if len(processes) > 0 {
354+
return fmt.Errorf("cgroups: unable to remove path %q: still contains running processes", c.path)
355+
}
348356
return remove(c.path)
349357
}
350358

0 commit comments

Comments
 (0)