Merge pull request #20329 from Luap99/deadlock

libpod: fix deadlock while parallel container create
This commit is contained in:
openshift-ci[bot] 2023-10-11 13:13:02 +00:00 committed by GitHub
commit 3dcd6af6e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 21 deletions

View File

@ -470,8 +470,6 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
// Go through named volumes and add them. // Go through named volumes and add them.
// If they don't exist they will be created using basic options. // 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 { for _, vol := range ctr.config.NamedVolumes {
isAnonymous := false isAnonymous := false
if vol.Name == "" { if vol.Name == "" {
@ -481,9 +479,8 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
isAnonymous = true isAnonymous = true
} else { } else {
// Check if it already exists // Check if it already exists
dbVol, err := r.state.Volume(vol.Name) _, err := r.state.Volume(vol.Name)
if err == nil { if err == nil {
ctrNamedVolumes = append(ctrNamedVolumes, dbVol)
// The volume exists, we're good // The volume exists, we're good
continue continue
} else if !errors.Is(err, define.ErrNoSuchVolume) { } else if !errors.Is(err, define.ErrNoSuchVolume) {
@ -538,12 +535,10 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
volOptions = append(volOptions, WithVolumeNoChown()) volOptions = append(volOptions, WithVolumeNoChown())
} }
newVol, err := r.newVolume(ctx, false, volOptions...) _, err = r.newVolume(ctx, false, volOptions...)
if err != nil { if err != nil {
return nil, fmt.Errorf("creating named volume %q: %w", vol.Name, err) return nil, fmt.Errorf("creating named volume %q: %w", vol.Name, err)
} }
ctrNamedVolumes = append(ctrNamedVolumes, newVol)
} }
switch ctr.config.LogDriver { switch ctr.config.LogDriver {
@ -565,20 +560,6 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
ctr.config.Mounts = append(ctr.config.Mounts, ctr.config.ShmDir) 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 // Add the container to the state
// TODO: May be worth looking into recovering from name/ID collisions here // TODO: May be worth looking into recovering from name/ID collisions here
if ctr.config.Pod != "" { if ctr.config.Pod != "" {