Skip to content

Commit c4c5c1a

Browse files
mheonrh-atomic-bot
authored andcommitted
Remove parent cgroup we create with cgroupfs
Signed-off-by: Matthew Heon <[email protected]> Closes: containers#507 Approved by: baude
1 parent b70f6cc commit c4c5c1a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

libpod/container_internal.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"syscall"
1515
"time"
1616

17+
"github.com/containerd/cgroups"
1718
"github.com/containers/storage"
1819
"github.com/containers/storage/pkg/archive"
1920
"github.com/containers/storage/pkg/chrootarchive"
@@ -721,6 +722,30 @@ func (c *Container) prepare() (err error) {
721722
return nil
722723
}
723724

725+
// cleanupCgroup cleans up residual CGroups after container execution
726+
// This is a no-op for the systemd cgroup driver
727+
func (c *Container) cleanupCgroups() error {
728+
if c.runtime.config.CgroupManager == SystemdCgroupsManager {
729+
return nil
730+
}
731+
732+
// Remove the base path of the container's cgroups
733+
path := filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-%s", c.ID()))
734+
735+
logrus.Debugf("Removing CGroup %s", path)
736+
737+
cgroup, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(path))
738+
if err != nil {
739+
return err
740+
}
741+
742+
if err := cgroup.Delete(); err != nil {
743+
return err
744+
}
745+
746+
return nil
747+
}
748+
724749
// cleanupNetwork unmounts and cleans up the container's network
725750
func (c *Container) cleanupNetwork() error {
726751
// Stop the container's network namespace (if it has one)
@@ -764,11 +789,21 @@ func (c *Container) cleanupStorage() error {
764789
func (c *Container) cleanup() error {
765790
var lastError error
766791

792+
logrus.Debugf("Cleaning up container %s", c.ID())
793+
767794
// Clean up network namespace, if present
768795
if err := c.cleanupNetwork(); err != nil {
769796
lastError = nil
770797
}
771798

799+
if err := c.cleanupCgroups(); err != nil {
800+
if lastError != nil {
801+
logrus.Errorf("Error cleaning up container %s CGroups: %v", c.ID(), err)
802+
} else {
803+
lastError = err
804+
}
805+
}
806+
772807
// Unmount storage
773808
if err := c.cleanupStorage(); err != nil {
774809
if lastError != nil {

libpod/runtime_ctr.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ func (r *Runtime) removeContainer(c *Container, force bool) error {
219219
return errors.Wrapf(ErrCtrExists, "container %s has dependent containers which must be removed before it: %s", c.ID(), depsStr)
220220
}
221221

222+
// Tear down the container's cgroups (if they exist)
223+
if err := c.cleanupCgroups(); err != nil {
224+
return err
225+
}
226+
222227
// Stop the container's network namespace (if it has one)
223228
if err := r.teardownNetNS(c); err != nil {
224229
return err

0 commit comments

Comments
 (0)