fix panic close on closed channel

This commit is contained in:
yusing 2025-01-03 18:55:38 +08:00
parent 7e60d1803c
commit 65afc73f25

View file

@ -3,7 +3,6 @@ package task
import ( import (
"errors" "errors"
"fmt" "fmt"
"sync/atomic"
"time" "time"
) )
@ -20,17 +19,19 @@ func (t *Task) addCallback(about string, fn func(), waitSubTasks bool) {
} }
func (t *Task) addChildCount() { func (t *Task) addChildCount() {
if atomic.AddUint32(&t.children, 1) == 1 {
t.mu.Lock() t.mu.Lock()
if t.childrenDone == nil { defer t.mu.Unlock()
t.children++
if t.children == 1 {
t.childrenDone = make(chan struct{}) t.childrenDone = make(chan struct{})
} }
t.mu.Unlock()
}
} }
func (t *Task) subChildCount() { func (t *Task) subChildCount() {
switch atomic.AddUint32(&t.children, ^uint32(0)) { t.mu.Lock()
defer t.mu.Unlock()
t.children--
switch t.children {
case 0: case 0:
close(t.childrenDone) close(t.childrenDone)
case ^uint32(0): case ^uint32(0):