Skip to content

Commit fe49a02

Browse files
committed
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.
1 parent 9f59241 commit fe49a02

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎internal/warp/pool.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,18 @@ func (p *Pool) scaleDown() {
645645
}
646646
}
647647

648-
// after runs fn once delay has elapsed, unless the pool closes first.
648+
// after runs fn once delay has elapsed, unless the pool closes first. The wait
649+
// group is incremented under the pool lock so it cannot race with Close's
650+
// Wait: a timer scheduled after Close is not started at all, and one scheduled
651+
// before it is counted before Close can begin waiting.
649652
func (p *Pool) after(delay time.Duration, fn func()) {
653+
p.mu.Lock()
654+
if p.closed {
655+
p.mu.Unlock()
656+
return
657+
}
650658
p.wait.Add(1)
659+
p.mu.Unlock()
651660
go func() {
652661
defer p.wait.Done()
653662
timer := time.NewTimer(delay)

0 commit comments

Comments
 (0)