fix(libbox): prevent goroutine leak in ExchangeContext.OnCancel

The OnCancel goroutine blocked indefinitely on c.context.Done().
If the context was never cancelled, the goroutine leaked.
Add a timeout via time.After with DNSTimeout to ensure termination.

Fixes #4162
This commit is contained in:
hklcf 2026-05-23 09:53:59 +08:00
parent 812a8fdaee
commit 320a3f6f43

View File

@ -5,6 +5,7 @@ import (
"net/netip"
"strings"
"syscall"
"time"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
@ -121,8 +122,11 @@ type ExchangeContext struct {
func (c *ExchangeContext) OnCancel(callback Func) {
go func() {
<-c.context.Done()
callback.Invoke()
select {
case <-c.context.Done():
callback.Invoke()
case <-time.After(C.DNSTimeout):
}
}()
}