fix(warp): schedule pool timers under the pool lock

after() added to the pool wait group outside the lock, so a timer scheduled just as Close began Wait could be missed by the shutdown wait. Increment under the lock and refuse to schedule after Close, which removes the Add/Wait race.
This commit is contained in:
Jason Xu committed 2026-10-26 23:12:18 +08:00
1 parent 9f59241edc
commit fe49a02169
1 file changed
+10 -1
+10 -1
View File
@@ -645,9 +645,18 @@ func (p *Pool) scaleDown() {
}
}
// after runs fn once delay has elapsed, unless the pool closes first.
// after runs fn once delay has elapsed, unless the pool closes first. The wait
// group is incremented under the pool lock so it cannot race with Close's
// Wait: a timer scheduled after Close is not started at all, and one scheduled
// before it is counted before Close can begin waiting.
func (p *Pool) after(delay time.Duration, fn func()) {
p.mu.Lock()
if p.closed {
p.mu.Unlock()
return
}
p.wait.Add(1)
p.mu.Unlock()
go func() {
defer p.wait.Done()
timer := time.NewTimer(delay)