From 9c20ed61852dfcf8ef469aa85615ea91b2fe5137 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 17 Mar 2021 14:44:16 -0400 Subject: [PATCH] StateMgr must be able to return with locked state The current usage of internal remote state backends requires that `StateMgr` be able to return an instance of `statemgr.Full` even if the state is currently locked. --- .../remote-state/etcdv3/backend_state.go | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/backend/remote-state/etcdv3/backend_state.go b/internal/backend/remote-state/etcdv3/backend_state.go index b9bd3839d..b1a931460 100644 --- a/internal/backend/remote-state/etcdv3/backend_state.go +++ b/internal/backend/remote-state/etcdv3/backend_state.go @@ -58,16 +58,8 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) { lockInfo := statemgr.NewLockInfo() lockInfo.Operation = "init" - lockId, err := stateMgr.Lock(lockInfo) - if err != nil { - return nil, fmt.Errorf("Failed to lock state in etcd: %s.", err) - } - lockUnlock := func(parent error) error { - if err := stateMgr.Unlock(lockId); err != nil { - return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err) - } - return parent + return nil } if err := stateMgr.RefreshState(); err != nil { @@ -76,6 +68,18 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) { } if v := stateMgr.State(); v == nil { + lockId, err := stateMgr.Lock(lockInfo) + if err != nil { + return nil, fmt.Errorf("Failed to lock state in etcd: %s.", err) + } + + lockUnlock = func(parent error) error { + if err := stateMgr.Unlock(lockId); err != nil { + return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err) + } + return parent + } + if err := stateMgr.WriteState(states.NewState()); err != nil { err = lockUnlock(err) return nil, err