@@ -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
725750func (c * Container ) cleanupNetwork () error {
726751 // Stop the container's network namespace (if it has one)
@@ -764,11 +789,21 @@ func (c *Container) cleanupStorage() error {
764789func (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 {
0 commit comments