Skip to content

Commit

Permalink
make sure Abort is holding untill inner goroutine finishes
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Sep 9, 2021
1 parent c4b4a17 commit 6a1013c
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions bar.go
Expand Up @@ -268,15 +268,19 @@ func (b *Bar) SetPriority(priority int) {
// if bar is already in complete state. If drop is true bar will be
// removed as well.
func (b *Bar) Abort(drop bool) {
done := make(chan struct{})
select {
case b.operateState <- func(s *bState) {
if s.completed == true {
close(done)
return
}
if drop {
go b.container.dropBar(b)
} else {
go func() {
// container must be run during lifetime of this inner goroutine
// we control this by done channel declared above
go func() {
if drop {
b.container.dropBar(b)
} else {
var uncompleted int
b.container.traverseBars(func(bar *Bar) bool {
if b != bar && !bar.Completed() {
Expand All @@ -286,16 +290,15 @@ func (b *Bar) Abort(drop bool) {
return true
})
if uncompleted == 0 {
select {
case b.container.refreshCh <- time.Now():
case <-b.container.done:
}
b.container.refreshCh <- time.Now()
}
}()
}
}
close(done) // release hold of Abort
}()
b.cancel()
}:
<-b.done
// guarantee: container is alive during lifetime of this hold
<-done
case <-b.done:
}
}
Expand Down

0 comments on commit 6a1013c

Please sign in to comment.