Skip to content

Commit

Permalink
Fix incorrect usage of sync.WaitGroup in RulesFromFile (#7)
Browse files Browse the repository at this point in the history
* `RulesFromFile`: fix `sync.WaitGroup` usage
  • Loading branch information
EthanThatOneKid authored Jun 21, 2023
1 parent 5c67598 commit 41e4fed
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,22 @@ func RulesFromFile(pathname string, ranges []Range, visited map[string]struct{},
return nil, errors.Wrapf(err, "failed to parse rules for file %s", pathname)
}

var innerWg sync.WaitGroup // WaitGroup for inner goroutines
for _, rule := range rules {
for _, target := range rule.Targets {
if target.File == nil {
continue
}

wg.Add(1)
innerWg.Add(1)
go func(pathname string) {
defer wg.Done()
defer innerWg.Done()

if _, ok := visited[pathname]; ok {
return
}

moreRules, err := RulesFromFile(pathname, nil, visited, wg, options)
moreRules, err := RulesFromFile(pathname, nil, visited, &innerWg, options)
if err != nil {
return
}
Expand All @@ -126,5 +127,9 @@ func RulesFromFile(pathname string, ranges []Range, visited map[string]struct{},
}
}

// Wait for all inner goroutines to complete before returning.
innerWg.Wait()

// Add rules to the map.
return rules, nil
}

0 comments on commit 41e4fed

Please sign in to comment.