From a4ccac76b7efa09dccd3f45779d492ef4cfb7893 Mon Sep 17 00:00:00 2001 From: Jason Xu Date: Mon, 26 Oct 2026 23:12:18 +0800 Subject: [PATCH] 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. --- internal/warp/pool.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/warp/pool.go b/internal/warp/pool.go index d725b61..ef6a840 100644 --- a/internal/warp/pool.go +++ b/internal/warp/pool.go @@ -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)