devchat-ai / gopool

GoPool is a high-performance, feature-rich, and easy-to-use worker pool library for Golang.
MIT License
187 stars 26 forks source link

runtime error: slice bounds out of range #17

Closed hbh112233abc closed 1 year ago

hbh112233abc commented 1 year ago

以下这平平无奇的代码,怎么就会发生崩溃呢

package main

import (
    "fmt"
    "math/rand"
    "sync"
    "time"

    "github.com/devchat-ai/gopool"
)

func main() {
    var m sync.Map
    pool := gopool.NewGoPool(
        20,
        gopool.WithMinWorkers(3),
    )
    defer pool.Release()
    var i = 0
    for {
        i += 1
        if _, ok := m.Load(i); !ok {
            pool.AddTask(func() (interface{}, error) {
                m.Store(i, 1)
                task(i)
                m.Delete(i)
                return nil, nil
            })
        }
        if i > 20 {
            break
        }
    }
    pool.Wait()
}

func task(t int) error {
    i := rand.Intn(20)
    time.Sleep(time.Second * time.Duration(i))
    fmt.Printf("> %d -- %d \n", t, i)
    return nil
}

image

daniel-hutao commented 1 year ago

@hbh112233abc 抱歉,上周有些事情忙着,没腾出手,到现在才细看你提的这个 issue;我已经修复了这个问题,麻烦更新到 0.6.2 版本再试试。