Unlock state before workspace deletion

We need to release the lock just before deleting the state, in case the backend
can't remove the resource while holding the lock. This is currently true for
Windows local files.

TODO: While there is little safety in locking while deleting the state, it
might be nice to be able to coordinate processes around state deletion, i.e. in
a CI environment. Adding Delete() as a required method of States would allow
the removal of the resource to be delegated from the Backend to the State
itself.
This commit is contained in:
James Bardin 2017-06-27 14:31:08 -04:00
parent 8cf04f8e4e
commit 1c78dfb286
1 changed files with 12 additions and 1 deletions

View File

@ -105,6 +105,7 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int {
return 1
}
// Honor the lock request, for consistency and one final safety check.
if c.stateLock {
lockCtx, cancel := context.WithTimeout(context.Background(), c.stateLockTimeout)
defer cancel()
@ -117,7 +118,17 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("Error locking state: %s", err))
return 1
}
defer clistate.Unlock(sMgr, lockID, c.Ui, c.Colorize())
// We need to release the lock just before deleting the state, in case
// the backend can't remove the resource while holding the lock. This
// is currently true for Windows local files.
//
// TODO: While there is little safety in locking while deleting the
// state, it might be nice to be able to coordinate processes around
// state deletion, i.e. in a CI environment. Adding Delete() as a
// required method of States would allow the removal of the resource to
// be delegated from the Backend to the State itself.
clistate.Unlock(sMgr, lockID, c.Ui, c.Colorize())
}
err = b.DeleteState(delEnv)