Skip to content

Commit

Permalink
Fix data race: collect results, then append. (#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deleplace authored Feb 22, 2021
1 parent b183a92 commit 096f003
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,8 @@ func (s *scanner) preprocessInjectedFiles() {
go func() { s.resultChannel <- result }()
}

results := make([]config.InjectedFile, len(s.options.InjectAbsPaths))
j := 0
for _, absPath := range s.options.InjectAbsPaths {
prettyPath := s.res.PrettyPath(logger.Path{Text: absPath, Namespace: "file"})
lowerAbsPath := lowerCaseAbsPathForWindows(absPath)
Expand All @@ -1074,20 +1076,22 @@ func (s *scanner) preprocessInjectedFiles() {
continue
}

i := len(injectedFiles)
injectedFiles = append(injectedFiles, config.InjectedFile{})
channel := make(chan config.InjectedFile)
s.maybeParseFile(*resolveResult, prettyPath, nil, logger.Range{}, nil, inputKindNormal, channel)

// Wait for the results in parallel
// The results slice is large enough, it is not reallocated during the computations
injectWaitGroup.Add(1)
go func(i int, prettyPath string, resolveResult *resolver.ResolveResult) {
injectedFiles[i] = <-channel
go func(i int) {
results[i] = <-channel
injectWaitGroup.Done()
}(i, prettyPath, resolveResult)
}(j)
j++
}

injectWaitGroup.Wait()
injectedFiles = append(injectedFiles, results[:j]...)

s.options.InjectedFiles = injectedFiles
}

Expand Down

0 comments on commit 096f003

Please sign in to comment.