Skip to content

Fix race condition with closed channels #906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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