Skip to content

worker.go中executeTaskWithTimeout死锁问题 #21

@zzjha-cn

Description

@zzjha-cn

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 消息,但是此时已经没有发送方了。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions