Skip to content

Commit

Permalink
Merge pull request #906 from aledbf/fix-race-condition
Browse files Browse the repository at this point in the history
Fix race condition with closed channels
  • Loading branch information
aledbf authored Jun 28, 2017
2 parents 3c9ac43 + 8e8277f commit 9af4fb5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions core/pkg/task/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Queue struct {
// sync is called for each item in the queue
sync func(interface{}) error
// workerDone is closed when the worker exits
workerDone chan struct{}
workerDone chan bool

fn func(obj interface{}) (interface{}, error)
}
Expand Down Expand Up @@ -79,7 +79,9 @@ func (t *Queue) worker() {
for {
key, quit := t.queue.Get()
if quit {
close(t.workerDone)
if !isClosed(t.workerDone) {
close(t.workerDone)
}
return
}

Expand All @@ -95,6 +97,16 @@ func (t *Queue) worker() {
}
}

func isClosed(ch <-chan bool) bool {
select {
case <-ch:
return true
default:
}

return false
}

// Shutdown shuts down the work queue and waits for the worker to ACK
func (t *Queue) Shutdown() {
t.queue.ShutDown()
Expand All @@ -117,7 +129,7 @@ func NewCustomTaskQueue(syncFn func(interface{}) error, fn func(interface{}) (in
q := &Queue{
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
sync: syncFn,
workerDone: make(chan struct{}),
workerDone: make(chan bool),
fn: fn,
}

Expand Down

0 comments on commit 9af4fb5

Please sign in to comment.