B: protect local var access with a mutex
concourse/concourse#6454
package exec
import (
+ "sync"
+
"github.com/concourse/concourse/vars"
)
@@ -12,6 +14,8 @@ type buildVariables struct {
localVars vars.StaticVariables
tracker *vars.Tracker
+
+ lock sync.RWMutex
}
func newBuildVariables(credVars vars.Variables, enableRedaction bool) *buildVariables {
@@ -27,7 +31,9 @@ func newBuildVariables(credVars vars.Variables, enableRedaction bool) *buildVari
func (b *buildVariables) Get(ref vars.Reference) (interface{}, bool, error) {
if ref.Source == "." {
+ b.lock.RLock()
val, found, err := b.localVars.Get(ref)
+ b.lock.RUnlock()
if found || err != nil {
return val, found, err
}
@@ -40,6 +46,8 @@ func (b *buildVariables) List() ([]vars.Reference, error) {
if err != nil {
return nil, err
}
+ b.lock.RLock()
+ defer b.lock.RUnlock()
for k := range b.localVars {
list = append(list, vars.Reference{Source: ".", Path: k})
}
@@ -60,7 +68,10 @@ func (b *buildVariables) NewLocalScope() *buildVariables {
}
func (b *buildVariables) AddLocalVar(name string, val interface{}, redact bool) {
+ b.lock.Lock()
b.localVars[name] = val
+ b.lock.Unlock()
+
if redact {
b.tracker.Track(vars.Reference{Source: ".", Path: name}, val)
}
