fix: prevent goroutine leak on context-cancelled reads (contextutil + reader.ConnReadN) - #761
Open
G360-Niek wants to merge 2 commits into
Open
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
A context deadline can't interrupt a blocking socket Read, so when ctx was cancelled ConnReadN returned the context error but left the read goroutine parked in Read for the connection's lifetime, leaking the goroutine and the connection on every cancelled read. When the reader supports deadlines (net.Conn and friends), expire its read deadline on cancellation so the read returns; the deferred stop leaves the deadline untouched when the read finishes in time. Normalize the post-cancel error to the context error so the existing partial-data-on-timeout handling is deterministic instead of depending on which goroutine wins the select. Document that a connection whose read was cancelled should be discarded rather than reused. Adds tests asserting no goroutine leak on cancellation and that partial data received before cancellation is returned. Depends on the buffered result channel in contextutil (projectdiscovery#761): without it the unblocked read goroutine would still block sending its result. 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
Fixes a goroutine leak on context-cancelled reads, at both layers involved.
1.
contextutil.ExecFunc*— unbuffered result channel.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 — one leaked goroutine per cancelled call. Buffering each result channel (cap 1) lets the worker always send and exit.2.
reader.ConnReadN— blocking read not interrupted on cancel. Even with (1), a context deadline can't interrupt a blocking socketRead, so a cancelledConnReadNreturned the context error while the read goroutine stayed parked inRead— leaking the goroutine and pinning the connection. When the reader supports deadlines (net.Connand friends) it now expires the read deadline on cancellation so the read returns; the deferred stop leaves the deadline untouched when the read finishes in time. The post-cancel error is normalized to the context error so the existing "return partial data on timeout" handling is deterministic rather than racing the unblocked read. A connection whose read was cancelled is left with an expired deadline and should be discarded (documented onConnReadN).Proof (added regression tests)
context/leak_test.go: 50 context-cancelled calls perExecFunc*helper, asserting the goroutine count returns to baseline — fails on the unbuffered channels, passes with the fix.reader/conn_read_cancel_test.go: assertsConnReadNleaks no goroutine on cancellation, and that partial data received before cancellation is returned rather than dropped.Full
context/andreader/suites and-racestay green.Note
The residual case of a reader that blocks in
Readyet exposes noSetReadDeadlinecannot be interrupted by any of the above;ConnReadNstill returns to the caller on cancel (that read goroutine is unavoidable for such readers). This targetsnet.Conn-style readers, which isConnReadN's documented use.Fixes #759