backend/remote: Unlock workspace if Context fails

Previously any error case in the Context method would cause us to leave
the remote workspace locked on exit, requiring manual action to unlock it.
This commit is contained in:
Martin Atkins 2019-10-29 16:59:50 -07:00
parent 74249a8c23
commit 9f9f22091e
1 changed files with 11 additions and 0 deletions

View File

@ -49,6 +49,17 @@ func (b *Remote) Context(op *backend.Operation) (*terraform.Context, statemgr.Fu
return nil, nil, diags
}
defer func() {
// If we're returning with errors, and thus not producing a valid
// context, we'll want to avoid leaving the remote workspace locked.
if diags.HasErrors() {
err := op.StateLocker.Unlock(nil)
if err != nil {
diags = diags.Append(errwrap.Wrapf("Error unlocking state: {{err}}", err))
}
}
}()
log.Printf("[TRACE] backend/remote: reading remote state for workspace %q", workspace)
if err := stateMgr.RefreshState(); err != nil {
diags = diags.Append(errwrap.Wrapf("Error loading state: {{err}}", err))