Merge pull request #22059 from TomSweeneyRedHat/dev/tsweeney/deadlockfix_4.4.1

[v4.4.1-rhel] libpod: fix deadlock while parallel container create
This commit is contained in:
openshift-merge-bot[bot] 2024-03-18 15:49:08 +00:00 • committed by GitHub
commit 80daf82887
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -467,8 +467,6 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
// Go through named volumes and add them.
// If they don't exist they will be created using basic options.
// Maintain an array of them - we need to lock them later.
ctrNamedVolumes := make([]*Volume, 0, len(ctr.config.NamedVolumes))
for _, vol := range ctr.config.NamedVolumes {
isAnonymous := false
if vol.Name == "" {
@ -477,10 +475,9 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
vol.Name = stringid.GenerateRandomID()
isAnonymous = true
} else {
// Check if it exists already
dbVol, err := r.state.Volume(vol.Name)
// Check if it already exists
_, err := r.state.Volume(vol.Name)
if err == nil {
ctrNamedVolumes = append(ctrNamedVolumes, dbVol)
// The volume exists, we're good
continue
} else if !errors.Is(err, define.ErrNoSuchVolume) {
@ -532,12 +529,10 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
volOptions = append(volOptions, WithVolumeNoChown())
}
newVol, err := r.newVolume(ctx, false, volOptions...)
_, err = r.newVolume(ctx, false, volOptions...)
if err != nil {
return nil, fmt.Errorf("creating named volume %q: %w", vol.Name, err)
}
ctrNamedVolumes = append(ctrNamedVolumes, newVol)
}
switch ctr.config.LogDriver {
@ -563,20 +558,6 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
ctr.config.Mounts = append(ctr.config.Mounts, ctr.config.ShmDir)
}
// Lock all named volumes we are adding ourself to, to ensure we can't
// use a volume being removed.
volsLocked := make(map[string]bool)
for _, namedVol := range ctrNamedVolumes {
toLock := namedVol
// Ensure that we don't double-lock a named volume that is used
// more than once.
if volsLocked[namedVol.Name()] {
continue
}
volsLocked[namedVol.Name()] = true
toLock.lock.Lock()
defer toLock.lock.Unlock()
}
// Add the container to the state
// TODO: May be worth looking into recovering from name/ID collisions here
if ctr.config.Pod != "" {