From a44594205e16818434d27f02da3f81c26e51a74a Mon Sep 17 00:00:00 2001 From: POABOB Date: Fri, 7 Mar 2025 23:11:10 +0800 Subject: [PATCH] bug: don't reset the worker queue when rebooting preallocated pool (#360) Fixes #358 --- ants_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++ worker_loop_queue.go | 2 -- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/ants_test.go b/ants_test.go index 8fc1d5f..ecd8576 100644 --- a/ants_test.go +++ b/ants_test.go @@ -1535,3 +1535,71 @@ func TestMultiPoolWithFuncGeneric(t *testing.T) { mp.Tune(10) } + +func TestRebootNewPoolCalc(t *testing.T) { + atomic.StoreInt32(&sum, 0) + runTimes := 1000 + wg.Add(runTimes) + + pool, err := ants.NewPool(10) + require.NoError(t, err) + defer pool.Release() + // Use the default pool. + for i := 0; i < runTimes; i++ { + j := i + _ = pool.Submit(func() { + incSumInt(int32(j)) + }) + } + wg.Wait() + require.EqualValues(t, 499500, sum, "The result should be 499500") + + atomic.StoreInt32(&sum, 0) + wg.Add(runTimes) + err = pool.ReleaseTimeout(time.Second) // use both Release and ReleaseTimeout will occur panic + require.NoError(t, err) + pool.Reboot() + + for i := 0; i < runTimes; i++ { + j := i + _ = pool.Submit(func() { + incSumInt(int32(j)) + }) + } + wg.Wait() + require.EqualValues(t, 499500, sum, "The result should be 499500") +} + +func TestRebootNewPoolWithPreAllocCalc(t *testing.T) { + atomic.StoreInt32(&sum, 0) + runTimes := 1000 + wg.Add(runTimes) + + pool, err := ants.NewPool(10, ants.WithPreAlloc(true)) + require.NoError(t, err) + defer pool.Release() + // Use the default pool. + for i := 0; i < runTimes; i++ { + j := i + _ = pool.Submit(func() { + incSumInt(int32(j)) + }) + } + wg.Wait() + require.EqualValues(t, 499500, sum, "The result should be 499500") + + atomic.StoreInt32(&sum, 0) + err = pool.ReleaseTimeout(time.Second) + require.NoError(t, err) + pool.Reboot() + + wg.Add(runTimes) + for i := 0; i < runTimes; i++ { + j := i + _ = pool.Submit(func() { + incSumInt(int32(j)) + }) + } + wg.Wait() + require.EqualValues(t, 499500, sum, "The result should be 499500") +} diff --git a/worker_loop_queue.go b/worker_loop_queue.go index b372983..ad62bae 100644 --- a/worker_loop_queue.go +++ b/worker_loop_queue.go @@ -170,8 +170,6 @@ retry: w.finish() goto retry } - wq.items = wq.items[:0] - wq.size = 0 wq.head = 0 wq.tail = 0 }