Merge pull request #24599 from hashicorp/jbardin/races

Fix races in GetVariableValue and login
This commit is contained in:
James Bardin 2020-04-08 17:13:21 -04:00 committed by GitHub
commit 4f7d30900e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 3 deletions

View File

@ -364,7 +364,7 @@ func (c *LoginCommand) interactiveGetTokenByCode(hostname svchost.Hostname, cred
}), }),
} }
go func() { go func() {
err = server.Serve(listener) err := server.Serve(listener)
if err != nil && err != http.ErrServerClosed { if err != nil && err != http.ErrServerClosed {
diags = diags.Append(tfdiags.Sourceless( diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error, tfdiags.Error,
@ -417,8 +417,7 @@ func (c *LoginCommand) interactiveGetTokenByCode(hostname svchost.Hostname, cred
return nil, diags return nil, diags
} }
err = server.Close() if err := server.Close(); err != nil {
if err != nil {
// The server will close soon enough when our process exits anyway, // The server will close soon enough when our process exits anyway,
// so we won't fuss about it for right now. // so we won't fuss about it for right now.
log.Printf("[WARN] login: callback server can't shut down: %s", err) log.Printf("[WARN] login: callback server can't shut down: %s", err)

View File

@ -41,6 +41,7 @@ func TestFilesystemRace(t *testing.T) {
ls.WriteState(current) ls.WriteState(current)
}() }()
} }
wg.Wait()
} }
func TestFilesystemLocks(t *testing.T) { func TestFilesystemLocks(t *testing.T) {

View File

@ -330,6 +330,9 @@ func (ctx *BuiltinEvalContext) SetModuleCallArguments(n addrs.ModuleCallInstance
} }
func (ctx *BuiltinEvalContext) GetVariableValue(addr addrs.AbsInputVariableInstance) cty.Value { func (ctx *BuiltinEvalContext) GetVariableValue(addr addrs.AbsInputVariableInstance) cty.Value {
ctx.VariableValuesLock.Lock()
defer ctx.VariableValuesLock.Unlock()
modKey := addr.Module.String() modKey := addr.Module.String()
modVars := ctx.VariableValues[modKey] modVars := ctx.VariableValues[modKey]
val, ok := modVars[addr.Variable.Name] val, ok := modVars[addr.Variable.Name]

View File

@ -1,6 +1,7 @@
package terraform package terraform
import ( import (
"sync"
"testing" "testing"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
@ -19,6 +20,7 @@ func TestNilHook_impl(t *testing.T) {
// It is intended for testing that core code is emitting the correct hooks // It is intended for testing that core code is emitting the correct hooks
// for a given situation. // for a given situation.
type testHook struct { type testHook struct {
mu sync.Mutex
Calls []*testHookCall Calls []*testHookCall
} }
@ -33,70 +35,98 @@ type testHookCall struct {
} }
func (h *testHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) { func (h *testHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PreApply", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PreApply", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) { func (h *testHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PostApply", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PostApply", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) { func (h *testHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PreDiff", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PreDiff", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) { func (h *testHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PostDiff", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PostDiff", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) { func (h *testHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PreProvisionInstance", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PreProvisionInstance", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) { func (h *testHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PostProvisionInstance", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PostProvisionInstance", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) { func (h *testHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PreProvisionInstanceStep", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PreProvisionInstanceStep", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) { func (h *testHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PostProvisionInstanceStep", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PostProvisionInstanceStep", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) { func (h *testHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"ProvisionOutput", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"ProvisionOutput", addr.String()})
} }
func (h *testHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) { func (h *testHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PreRefresh", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PreRefresh", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) { func (h *testHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PostRefresh", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PostRefresh", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) { func (h *testHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PreImportState", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PreImportState", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) { func (h *testHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PostImportState", addr.String()}) h.Calls = append(h.Calls, &testHookCall{"PostImportState", addr.String()})
return HookActionContinue, nil return HookActionContinue, nil
} }
func (h *testHook) PostStateUpdate(new *states.State) (HookAction, error) { func (h *testHook) PostStateUpdate(new *states.State) (HookAction, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.Calls = append(h.Calls, &testHookCall{"PostStateUpdate", ""}) h.Calls = append(h.Calls, &testHookCall{"PostStateUpdate", ""})
return HookActionContinue, nil return HookActionContinue, nil
} }