Skip to content

Commit

Permalink
remove a data race by compiling the exclude regexes in the preprocess…
Browse files Browse the repository at this point in the history
… phase (#677)

the regex compilation was done lazily on first access but did not properly synchronize
for being accessed by multiple watcher goroutines

between the option of adding a mutex for something that (should) only ever happen
once and removing the potential for a race, this seems like the better choice
  • Loading branch information
istyf authored Dec 16, 2024
1 parent 7a0a17f commit e0851cb
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ type cfgBuild struct {
}

func (c *cfgBuild) RegexCompiled() ([]*regexp.Regexp, error) {
if len(c.ExcludeRegex) > 0 && len(c.regexCompiled) == 0 {
c.regexCompiled = make([]*regexp.Regexp, 0, len(c.ExcludeRegex))
for _, s := range c.ExcludeRegex {
re, err := regexp.Compile(s)
if err != nil {
return nil, err
}
c.regexCompiled = append(c.regexCompiled, re)
}
}
return c.regexCompiled, nil
}

Expand Down Expand Up @@ -318,6 +308,19 @@ func (c *Config) preprocess() error {
runtimeArgs := flag.Args()
c.Build.ArgsBin = append(c.Build.ArgsBin, runtimeArgs...)

// Compile the exclude regexes if there are any patterns in the config file
if len(c.Build.ExcludeRegex) > 0 {
regexCompiled := make([]*regexp.Regexp, len(c.Build.ExcludeRegex))
for idx, expr := range c.Build.ExcludeRegex {
re, err := regexp.Compile(expr)
if err != nil {
return fmt.Errorf("failed to compile regex %s", expr)
}
regexCompiled[idx] = re
}
c.Build.regexCompiled = regexCompiled
}

c.Build.ExcludeDir = ed
if len(c.Build.FullBin) > 0 {
c.Build.Bin = c.Build.FullBin
Expand Down

0 comments on commit e0851cb

Please sign in to comment.