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

fix(getter): race condition when SetClient is called #466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

srevinsaju
Copy link

This is a non-breaking change. This fixes an issue when Clients are created and called concurrently.
Clients called SetClient on each of the Getters. When no getters were provided, it modified the clients of the default getters defined in the Getters map.
Since the map was mutated from multiple goroutines, it creates a race condition 'concurrent map writes'. The issue is mitigated by creating a new copy of Getters using DefaultGetters() function for every new client created. SetClient now modifies its own copy of the Getter, not the default Getters.

To reproduce the issue:

mkdir hello
touch hello/world
// main.go
package main

import "github.com/hashicorp/go-getter"
import "context"
import "os"
import "sync"
import "path/filepath"

func main() {
    var wg sync.WaitGroup
    cwd, err := os.Getwd()
    if err != nil {
        panic(err)
    }
    keys := []string{"a", "b", "c"}

    for _, key := range keys {
        wg.Add(1)
        go func(key string) {
            get := &getter.Client{
                Ctx: context.Background(),
                Src: "./hello",
                Dst: filepath.Join("dest", key),
                Pwd: cwd,
                Dir: true,
            }
      err := get.Get()
            if err != nil {
                panic(err)
            }
            wg.Done()
        }(key)
    }
    wg.Wait()
}
go run -race .

this is a non-breaking change. this fixes an issue when Clients are created
and called concurrently. clients called SetClient on each of the
Getters. When no getters were provided, it modified the clients
of the default getters defined in a map. Since the map was mutated
from multiple goroutines, it creates a race condition 'concurrent map writes'.
The issue is mitigated by creating a new copy of Getters for every new client
created. SetClient now modifies its own copy of the Getter, not the default Getters
@hashicorp-cla
Copy link

hashicorp-cla commented Oct 23, 2023

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants