Skip to content

Commit

Permalink
Add LockVolumeWithSnapshot function to prevent future deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucaber committed Sep 25, 2024
1 parent 0521ba4 commit d2e4509
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func (cs *controller) LockVolume(volume string) func() {
return func() { mtx.Unlock() }
}

func (cs *controller) LockVolumeWithSnapshot(volume string, snapshot string) func() {
unlockVol := cs.LockVolume(volume)
unlockSnap := cs.LockVolume(snapshot)
return func() {
unlockVol()
unlockSnap()
}
}

// NewController returns a new instance
// of CSI controller
func NewController(d *CSIDriver) csi.ControllerServer {
Expand Down Expand Up @@ -722,10 +731,8 @@ func (cs *controller) CreateSnapshot(
if err != nil {
return nil, err
}
unlockVol := cs.LockVolume(volumeID)
defer unlockVol()
unlockSnap := cs.LockVolume(snapName)
defer unlockSnap()
unlock := cs.LockVolumeWithSnapshot(volumeID, snapName)
defer unlock()

snapTimeStamp := time.Now().Unix()
var state string
Expand Down Expand Up @@ -822,10 +829,8 @@ func (cs *controller) DeleteSnapshot(
// should succeed when an invalid snapshot id is used
return &csi.DeleteSnapshotResponse{}, nil
}
unlockVol := cs.LockVolume(snapshotID[0])
defer unlockVol()
unlockSnap := cs.LockVolume(snapshotID[1])
defer unlockSnap()
unlock := cs.LockVolumeWithSnapshot(snapshotID[0], snapshotID[1])
defer unlock()
if err := zfs.DeleteSnapshot(snapshotID[1]); err != nil {
return nil, status.Errorf(
codes.Internal,
Expand Down

0 comments on commit d2e4509

Please sign in to comment.