Skip to content

Commit

Permalink
fix: concurrent map read and write error (#2867)
Browse files Browse the repository at this point in the history
This switches the Terragrunt output cache to use a sync.Map instead so we don't hit concurrent map access panics when we're adding output values to the map.
  • Loading branch information
aliscott committed Feb 7, 2024
1 parent 32d2b00 commit f5fc595
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions internal/providers/terraform/terragrunt_hcl_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (
terragruntDownloadedDirs = sync.Map{}

terragruntOutputCache = &TerragruntOutputCache{
cache: map[string]cty.Value{},
cache: sync.Map{},
mu: &infSync.KeyMutex{},
}
)
Expand All @@ -71,7 +71,7 @@ func (p panicError) Error() string {
}

type TerragruntOutputCache struct {
cache map[string]cty.Value
cache sync.Map
mu *infSync.KeyMutex
}

Expand All @@ -82,17 +82,19 @@ func (o *TerragruntOutputCache) Set(key string, getVal func() (cty.Value, error)
unlock := o.mu.Lock(key)
defer unlock()

val, ok := o.cache[key]
if ok {
return val, nil
cacheVal, exists := o.cache.Load(key)
if exists {
if val, ok := cacheVal.(cty.Value); ok {
return val, nil
}
}

val, err := getVal()
if err != nil {
return cty.NilVal, err
}

o.cache[key] = val
o.cache.Store(key, val)
return val, nil
}

Expand Down

0 comments on commit f5fc595

Please sign in to comment.