Skip to content

Commit e0851cb

Browse files
authored
remove a data race by compiling the exclude regexes in the preprocess 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
1 parent 7a0a17f commit e0851cb

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

runner/config.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,6 @@ type cfgBuild struct {
6262
}
6363

6464
func (c *cfgBuild) RegexCompiled() ([]*regexp.Regexp, error) {
65-
if len(c.ExcludeRegex) > 0 && len(c.regexCompiled) == 0 {
66-
c.regexCompiled = make([]*regexp.Regexp, 0, len(c.ExcludeRegex))
67-
for _, s := range c.ExcludeRegex {
68-
re, err := regexp.Compile(s)
69-
if err != nil {
70-
return nil, err
71-
}
72-
c.regexCompiled = append(c.regexCompiled, re)
73-
}
74-
}
7565
return c.regexCompiled, nil
7666
}
7767

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

311+
// Compile the exclude regexes if there are any patterns in the config file
312+
if len(c.Build.ExcludeRegex) > 0 {
313+
regexCompiled := make([]*regexp.Regexp, len(c.Build.ExcludeRegex))
314+
for idx, expr := range c.Build.ExcludeRegex {
315+
re, err := regexp.Compile(expr)
316+
if err != nil {
317+
return fmt.Errorf("failed to compile regex %s", expr)
318+
}
319+
regexCompiled[idx] = re
320+
}
321+
c.Build.regexCompiled = regexCompiled
322+
}
323+
321324
c.Build.ExcludeDir = ed
322325
if len(c.Build.FullBin) > 0 {
323326
c.Build.Bin = c.Build.FullBin

0 commit comments

Comments
 (0)