-
-
Notifications
You must be signed in to change notification settings - Fork 73
fix(pool): avoid race conditions when a task is submitted while the pool is stopping #105
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
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #105 +/- ##
==========================================
+ Coverage 94.62% 95.27% +0.65%
==========================================
Files 11 11
Lines 725 720 -5
==========================================
Hits 686 686
+ Misses 35 31 -4
+ Partials 4 3 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Hey @alitto ! I prefer your solution over mine for its simplicity. It reduces the chance of a data race significantly, but doesn't eliminate it completely because Please take a look at a slightly modified test from my PR that proves data race is still there (better to run this one standalone — func TestSubmitClose(t *testing.T) {
pool := NewPool(0)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(500 * time.Microsecond)
pool.StopAndWait()
}()
for i := 0; i < 100000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
pool.Submit(func() {
time.Sleep(10 * time.Millisecond)
})
}()
}
wg.Wait()
} |
IIUC, the unlock operation needs to be done at the end of |
@liznear that seems legit to me. |
f39b7c7
to
72cb0d4
Compare
72cb0d4
to
9e8faee
Compare
Thanks @korotin for providing that test 🙌, i added it to |
4430a43
to
0ccf029
Compare
@alitto seems like a nice and clean solution! 😎 |
This pull request provides an alternative solution to the one proposed by @korotin in his pull request #104 to address a race condition that can occur on
workersWaitGroup
when tasks are submitted while the pool is stopping.Changes
closed
atomic bool is toggled and checked while holding the mutex to avoid race conditions.workersWaitGroup.Add()
is always called while holding the mutex to avoid race conditions.trySubmit
method to make it simpler and more clear.launchWorker
.subpoolSubmit
withsubpoolWorker
method.Fixes
workerCount
counter when the pool context is cancelled.Resize()
now supports settingmaxConcurrency
to 0 (no limit)