From e1f1b84d67da081fa8f8dbad1dceacdff7588711 Mon Sep 17 00:00:00 2001 From: Sean Teeling Date: Mon, 12 Oct 2020 21:45:25 -0700 Subject: [PATCH] Refresh state outside of grabbing the lock; only grab the lock on provisioning if the state file doesn't exist; this is similar to the GCS backend --- backend/remote-state/azure/backend_state.go | 25 +++++++++------------ 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/backend/remote-state/azure/backend_state.go b/backend/remote-state/azure/backend_state.go index e7d316287..b794bc32e 100644 --- a/backend/remote-state/azure/backend_state.go +++ b/backend/remote-state/azure/backend_state.go @@ -95,9 +95,13 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) { stateMgr := &remote.State{Client: client} + // Grab the value + if err := stateMgr.RefreshState(); err != nil { + return nil, err + } //if this isn't the default state name, we need to create the object so //it's listed by States. - if name != backend.DefaultStateName { + if v := stateMgr.State(); v == nil { // take a lock on this state while we write it lockInfo := statemgr.NewLockInfo() lockInfo.Operation = "init" @@ -114,29 +118,20 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) { return parent } - // Grab the value - if err := stateMgr.RefreshState(); err != nil { + // If we have no state, we have to create an empty state + if err := stateMgr.WriteState(states.NewState()); err != nil { err = lockUnlock(err) return nil, err } - - // If we have no state, we have to create an empty state - if v := stateMgr.State(); v == nil { - if err := stateMgr.WriteState(states.NewState()); err != nil { - err = lockUnlock(err) - return nil, err - } - if err := stateMgr.PersistState(); err != nil { - err = lockUnlock(err) - return nil, err - } + if err := stateMgr.PersistState(); err != nil { + err = lockUnlock(err) + return nil, err } // Unlock, the state should now be initialized if err := lockUnlock(nil); err != nil { return nil, err } - } return stateMgr, nil