From b6687aa28730f993e4e0a68e3eb33964e305d908 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 21 Nov 2016 15:21:27 -0800 Subject: [PATCH] terraform: write lock on post state updates Found race here: https://travis-ci.org/hashicorp/terraform/builds/177814212 Since WriteState calls `prune` and `init`, we're actually modifying the state structure so we need a full write lock to perform this operation. --- terraform/eval_state.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/terraform/eval_state.go b/terraform/eval_state.go index 35e7c2f26..126a0e63a 100644 --- a/terraform/eval_state.go +++ b/terraform/eval_state.go @@ -107,9 +107,10 @@ type EvalUpdateStateHook struct{} func (n *EvalUpdateStateHook) Eval(ctx EvalContext) (interface{}, error) { state, lock := ctx.State() - // Get a read lock so it doesn't change while we're calling this - lock.RLock() - defer lock.RUnlock() + // Get a full lock. Even calling something like WriteState can modify + // (prune) the state, so we need the full lock. + lock.Lock() + defer lock.Unlock() // Call the hook err := ctx.Hook(func(h Hook) (HookAction, error) {