From 32b9de38c57accb143679ade78fb46e7a3ba6553 Mon Sep 17 00:00:00 2001 From: Luca Berneking Date: Wed, 25 Sep 2024 14:12:11 +0200 Subject: [PATCH] Add LockVolumeWithSnapshot function to prevent future deadlocks Signed-off-by: Luca Berneking --- pkg/driver/controller.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 7fa18d57..f1d98e13 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -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 { @@ -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 @@ -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,