From 74db5ed94e9956652b8b256be8b3d52c98679b5b Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Fri, 30 Jun 2017 21:03:16 +0200 Subject: [PATCH] fix(volume_attachment): fix volume_attachment deletion 4a153c47beda7abd8b061aebca1e295974f975cc introduced a deadlock caused my duplicated calls to `mu.Lock()` in a function as well as inside a `resource.Retry` closure. fixes #3 --- scaleway/provider.go | 3 --- scaleway/resource_volume_attachment.go | 24 +++++++++--------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/scaleway/provider.go b/scaleway/provider.go index 16069fe21..5fe71effb 100644 --- a/scaleway/provider.go +++ b/scaleway/provider.go @@ -3,7 +3,6 @@ package scaleway import ( "sync" - "github.com/hashicorp/terraform/helper/mutexkv" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -62,8 +61,6 @@ func Provider() terraform.ResourceProvider { } } -var scalewayMutexKV = mutexkv.NewMutexKV() - func providerConfigure(d *schema.ResourceData) (interface{}, error) { apiKey := "" if v, ok := d.Get("token").(string); ok { diff --git a/scaleway/resource_volume_attachment.go b/scaleway/resource_volume_attachment.go index 82b732555..16febe2fa 100644 --- a/scaleway/resource_volume_attachment.go +++ b/scaleway/resource_volume_attachment.go @@ -35,6 +35,9 @@ func resourceScalewayVolumeAttachment() *schema.Resource { var errVolumeAlreadyAttached = fmt.Errorf("Scaleway volume already attached") func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{}) error { + mu.Lock() + defer mu.Unlock() + scaleway := m.(*Client).scaleway vol, err := scaleway.GetVolume(d.Get("volume").(string)) @@ -46,10 +49,7 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{ return errVolumeAlreadyAttached } - // guard against server shutdown/ startup race conditiond serverID := d.Get("server").(string) - scalewayMutexKV.Lock(serverID) - defer scalewayMutexKV.Unlock(serverID) server, err := scaleway.GetServer(serverID) if err != nil { @@ -90,14 +90,11 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{ volumes[k] = v } - if err := resource.Retry(5*time.Minute, func() *resource.RetryError { - + if err := resource.Retry(serverWaitTimeout, func() *resource.RetryError { var req = api.ScalewayServerPatchDefinition{ Volumes: &volumes, } - mu.Lock() err := scaleway.PatchServer(serverID, req) - mu.Unlock() if err == nil { return nil @@ -169,18 +166,17 @@ func resourceScalewayVolumeAttachmentRead(d *schema.ResourceData, m interface{}) return nil } -func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{}) error { - scaleway := m.(*Client).scaleway +const serverWaitTimeout = 5 * time.Minute +func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{}) error { mu.Lock() defer mu.Unlock() + scaleway := m.(*Client).scaleway + var startServerAgain = false - // guard against server shutdown/ startup race conditiond serverID := d.Get("server").(string) - scalewayMutexKV.Lock(serverID) - defer scalewayMutexKV.Unlock(serverID) server, err := scaleway.GetServer(serverID) if err != nil { @@ -218,13 +214,11 @@ func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{ volumes[k] = v } - if err := resource.Retry(5*time.Minute, func() *resource.RetryError { + if err := resource.Retry(serverWaitTimeout, func() *resource.RetryError { var req = api.ScalewayServerPatchDefinition{ Volumes: &volumes, } - mu.Lock() err := scaleway.PatchServer(serverID, req) - mu.Unlock() if err == nil { return nil