fix(contextutil): prevent goroutine leak on context cancellation - #761
Open
G360-Niek wants to merge 1 commit into
Open
fix(contextutil): prevent goroutine leak on context cancellation#761G360-Niek wants to merge 1 commit into
G360-Niek wants to merge 1 commit into
Conversation
ExecFunc, ExecFuncWithTwoReturns and ExecFuncWithThreeReturns run fn in a goroutine and return via ctx.Done() when the context is cancelled before fn finishes. The worker then sends its result on an unbuffered channel that no longer has a receiver and blocks forever, leaking one goroutine (and, through reader.ConnReadN, the underlying connection) per cancelled call. Buffer each result channel (cap 1) so the worker can always send and exit even after the caller has already returned. Add regression tests that drive many context-cancelled calls whose fn outlives the context and assert the goroutine count returns to baseline; they fail on the unbuffered channels and pass with the fix. Refs projectdiscovery#759
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ExecFunc,ExecFuncWithTwoReturns, andExecFuncWithThreeReturnsrunfnin a goroutine and return viactx.Done()when the context is cancelled beforefnfinishes. The worker then sends its result on an unbuffered channel that no longer has a receiver and blocks forever — leaking one goroutine (and, throughreader.ConnReadN, the underlying connection) per cancelled call.This buffers each result channel (cap 1) so the worker can always send and exit even after the caller has returned.
Proof (added regression tests)
context/leak_test.godrives 50 context-cancelled calls per function whosefnoutlives the context, then asserts the goroutine count returns to baseline.~50 goroutines still alive after 50 context-cancelled calls.context/suite and-racestay green.Scope
This fixes the root leak in the
ExecFunc*helpers, which also makesreader.ConnReadNfully cleanable — a caller that closes the connection on cancellation now leaks nothing. The one residual case (aConnReadNwhose caller never closes the connection leaves the worker parked inFD.Read) would needConnReadNto close / set a read deadline on cancel; happy to follow up separately if you'd like that addressed here too.Refs #759