-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
Data race due to "Unsynchronized send and close operations" #70
Comments
Hey @hongkuancn, thanks for opening this issue. It looks like this send inside // maybeStopIdleWorker attempts to stop an idle worker by sending it a nil task
func (p *WorkerPool) maybeStopIdleWorker() bool {
if decremented := p.decrementWorkerCount(); !decremented {
return false
}
// If the pool context has been canceled the tasks channel could be closed, so sending a nil to it will panic
select {
case p.context.Done():
return false
default:
}
// Send a nil task to stop an idle worker
p.tasks <- nil
return true
} What do you think? Feel free to open a pull request with these changes and I'll be glad to merge it. |
Hey @alitto ! Thanks for the suggestion. I noticed there might still be synchronization issue after your change
I just looked back the history and come up with an idea. The issue is probably brought in by Pull Request #62. It wants to stop the pool with long running tasks when the context is canceled. However, when draining the tasks, it depends on a closed task channel. Worker goroutines are blocked because of this task channel. So the PR moves So I suggest to move func drainTasks(tasks <-chan func(), tasksWaitGroup *sync.WaitGroup) {
for {
select {
case task, ok := <-tasks:
if task != nil && ok {
tasksWaitGroup.Done()
}
default:
return
}
}
} So after the change, the
What do you think? Please let me know if anything I missed out. |
Hi!
I came across a data race with the test
It seems the issue is related to
purge()
send nil task to the task channel to recycle the workers. Meanwhile,stop()
close the task channel. The race detector will report the issue according to Unsynchronized send and close operations | Data Race Detector - The Go Programming LanguageThe text was updated successfully, but these errors were encountered: