Skip to content

task内 panic时 会恐慌,报错:使用了关闭的channel #6

@1340691923

Description

@1340691923

复现代码:

task := &timing2.Task{
Uuid: "uuid",
Job: timing2.GetJob(func() {
panic("err")
}),
RunTime: time.Now().Unix() + 90,
}

异常代码处:
job.go func (j *taskJob) run()

原因是 defer的顺序是 先进后出,原代码会先触发 j.done <- true 然后进行了 finish的逻辑,代码应改为:

defer func() {
	if !isPanic {
		j.done <- true
	}
}()

defer func() {
	if x := recover(); x != nil {
		err := errors.Errorf("job error with panic:%v", x)
		j.err <- err
		isPanic = true
		return
	}
}()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions