Skip to content

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

Open
hklcf wants to merge 1 commit into
SagerNet:testingfrom
hklcf:fix/oncancel-goroutine-leak
Open

fix(libbox): prevent goroutine leak in ExchangeContext.OnCancel#4167
hklcf wants to merge 1 commit into
SagerNet:testingfrom
hklcf:fix/oncancel-goroutine-leak

Conversation

@hklcf
Copy link
Copy Markdown

@hklcf hklcf commented May 23, 2026

Description

\ExchangeContext.OnCancel()\ at \experimental/libbox/dns.go:122-127\ spawns a goroutine that blocks on \c.context.Done(). If the context passed to \Exchange()\ is never cancelled (e.g., caller does not set a deadline or cancellation), the goroutine leaks indefinitely.

Fix

Add a \select\ with \ ime.After(C.DNSTimeout)\ as a fallback to ensure the goroutine terminates even if the context is never done.

Related Issue

Fixes #4162

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 SagerNet#4162
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Potential goroutine leak in ExchangeContext.OnCancel when context is never cancelled

1 participant