Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/template: fix race causing panic in template_file #4694

Merged
merged 1 commit into from
Jan 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builtin/providers/template/resource_template_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func execute(s string, vars map[string]interface{}) (string, error) {
cfg := lang.EvalConfig{
GlobalScope: &ast.BasicScope{
VarMap: varmap,
FuncMap: config.Funcs,
FuncMap: config.Funcs(),
},
}

Expand Down
24 changes: 24 additions & 0 deletions builtin/providers/template/resource_template_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import (
"fmt"
"sync"
"testing"

r "github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -76,6 +77,29 @@ func TestTemplateVariableChange(t *testing.T) {
})
}

// This test covers a panic due to config.Func formerly being a
// shared map, causing multiple template_file resources to try and
// accessing it parallel during their lang.Eval() runs.
//
// Before fix, test fails under `go test -race`
func TestTemplateSharedMemoryRace(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
go func(wg sync.WaitGroup, t *testing.T, i int) {
wg.Add(1)
out, err := execute("don't panic!", map[string]interface{}{})
if err != nil {
t.Fatalf("err: %s", err)
}
if out != "don't panic!" {
t.Fatalf("bad output: %s", out)
}
wg.Done()
}(wg, t, i)
}
wg.Wait()
}

func testTemplateConfig(template, vars string) string {
return fmt.Sprintf(`
resource "template_file" "t0" {
Expand Down
6 changes: 2 additions & 4 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import (
)

// Funcs is the mapping of built-in functions for configuration.
var Funcs map[string]ast.Function

func init() {
Funcs = map[string]ast.Function{
func Funcs() map[string]ast.Function {
return map[string]ast.Function{
"cidrhost": interpolationFuncCidrHost(),
"cidrnetmask": interpolationFuncCidrNetmask(),
"cidrsubnet": interpolationFuncCidrSubnet(),
Expand Down
2 changes: 1 addition & 1 deletion config/raw_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ type gobRawConfig struct {
// langEvalConfig returns the evaluation configuration we use to execute.
func langEvalConfig(vs map[string]ast.Variable) *lang.EvalConfig {
funcMap := make(map[string]ast.Function)
for k, v := range Funcs {
for k, v := range Funcs() {
funcMap[k] = v
}
funcMap["lookup"] = interpolationFuncLookup(vs)
Expand Down