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

worker.go中executeTaskWithTimeout死锁问题 #21

Open zzjha-cn opened 10 months ago

zzjha-cn commented 10 months ago

worker.go中,executeTaskWithTimeout执行有超时限制的任务会死锁。

image

执行代码:

func main() {
    var errCount int32

    pool := gopool.NewGoPool(100, gopool.WithTimeout(1*time.Second), gopool.WithErrorCallback(func(err error) {
        fmt.Println("get a error")
        atomic.StoreInt32(&errCount, 1)
    }))
    defer pool.Release()

    // 超时任务
    // pool.AddTask(func() (interface{}, error) {
    //  time.Sleep(2 * time.Second)
    //  return nil, nil
    // })

    // 不超时任务
    pool.AddTask(func() (interface{}, error) {
        fmt.Println("run normal task")
        return nil, nil
    })
    pool.AddTask(func() (interface{}, error) {
        fmt.Println("run normal task")
        return nil, nil
    })
    pool.AddTask(func() (interface{}, error) {
        fmt.Println("run normal task")
        return nil, nil
    })

    pool.Wait()

    fmt.Println("err count:", errCount)
}

因为result的chan接收到值后,会在下面等待err的chan 消息,但是此时已经没有发送方了。