libpod: unlock containers when removing pod

It solves a race where a container cleanup process launched because of
the container process exiting normally would hang.

It also solves a problem when running as rootless on cgroup v1 since
it is not possible to force pids.max = 1 on conmon to limit spawning
the cleanup process.

Partially copied from https://github.com/containers/podman/pull/13403

Related to: https://github.com/containers/podman/issues/14057

[NO NEW TESTS NEEDED] it doesn't add any new functionality

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2022-04-29 12:02:00 +02:00
parent ab3e072a0c
commit 384c2359b7
No known key found for this signature in database
GPG key ID: 67E38F7A8BA21772

View file

@ -199,10 +199,15 @@ func (r *Runtime) removePod(ctx context.Context, p *Pod, removeCtrs, force bool,
// Go through and lock all containers so we can operate on them all at // Go through and lock all containers so we can operate on them all at
// once. // once.
// First loop also checks that we are ready to go ahead and remove. // First loop also checks that we are ready to go ahead and remove.
containersLocked := true
for _, ctr := range ctrs { for _, ctr := range ctrs {
ctrLock := ctr.lock ctrLock := ctr.lock
ctrLock.Lock() ctrLock.Lock()
defer ctrLock.Unlock() defer func() {
if containersLocked {
ctrLock.Unlock()
}
}()
// If we're force-removing, no need to check status. // If we're force-removing, no need to check status.
if force { if force {
@ -304,6 +309,12 @@ func (r *Runtime) removePod(ctx context.Context, p *Pod, removeCtrs, force bool,
} }
} }
// let's unlock the containers so if there is any cleanup process, it can terminate its execution
for _, ctr := range ctrs {
ctr.lock.Unlock()
}
containersLocked = false
// Remove pod cgroup, if present // Remove pod cgroup, if present
if p.state.CgroupPath != "" { if p.state.CgroupPath != "" {
logrus.Debugf("Removing pod cgroup %s", p.state.CgroupPath) logrus.Debugf("Removing pod cgroup %s", p.state.CgroupPath)
@ -332,7 +343,7 @@ func (r *Runtime) removePod(ctx context.Context, p *Pod, removeCtrs, force bool,
} }
} }
if err == nil { if err == nil {
if err := conmonCgroup.Delete(); err != nil { if err = conmonCgroup.Delete(); err != nil {
if removalErr == nil { if removalErr == nil {
removalErr = errors.Wrapf(err, "error removing pod %s conmon cgroup", p.ID()) removalErr = errors.Wrapf(err, "error removing pod %s conmon cgroup", p.ID())
} else { } else {