Skip to content

Commit

Permalink
incusd/task: Fix wait group logic (more entries than running tasks)
Browse files Browse the repository at this point in the history
Suggested-by: Lucas Bremgartner <lucas.bremgartner@futurfusion.io>
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
  • Loading branch information
stgraber committed Nov 28, 2024
1 parent 820c995 commit 914e8e3
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions internal/server/task/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (g *Group) Start(ctx context.Context) {
defer g.mu.Unlock()

ctx, g.cancel = context.WithCancel(ctx)
g.wg.Add(len(g.tasks))

if g.running == nil {
g.running = make(map[int]bool)
Expand All @@ -56,18 +55,18 @@ func (g *Group) Start(ctx context.Context) {

g.running[i] = true
task := g.tasks[i] // Local variable for the closure below.
g.wg.Add(1)

go func(i int) {
defer g.wg.Done()

task.loop(ctx)

// Ensure running map is updated before wait group Done() is called.
g.mu.Lock()
defer g.mu.Unlock()

if g.running != nil {
g.running[i] = false
g.wg.Done()
}
g.running[i] = false
}(i)
}
}
Expand Down

0 comments on commit 914e8e3

Please sign in to comment.