Skip to content

fix: prevent goroutine leak on context-cancelled reads (contextutil + reader.ConnReadN) - #761

Open
G360-Niek wants to merge 2 commits into
projectdiscovery:mainfrom
guardian360:fix/contextutil-goroutine-leak
Open

fix: prevent goroutine leak on context-cancelled reads (contextutil + reader.ConnReadN)#761
G360-Niek wants to merge 2 commits into
projectdiscovery:mainfrom
guardian360:fix/contextutil-goroutine-leak

Conversation

@G360-Niek

@G360-Niek G360-Niek commented Jul 31, 2026

Copy link
Copy Markdown

What

Fixes a goroutine leak on context-cancelled reads, at both layers involved.

1. contextutil.ExecFunc* — unbuffered result channel. 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 — 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 socket Read, so a cancelled ConnReadN returned the context error while the read goroutine stayed parked in Read — leaking the goroutine and pinning the connection. When the reader supports deadlines (net.Conn and 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 on ConnReadN).

Proof (added regression tests)

  • context/leak_test.go: 50 context-cancelled calls per ExecFunc* helper, asserting the goroutine count returns to baseline — fails on the unbuffered channels, passes with the fix.
  • reader/conn_read_cancel_test.go: asserts ConnReadN leaks no goroutine on cancellation, and that partial data received before cancellation is returned rather than dropped.

Full context/ and reader/ suites and -race stay green.

Note

The residual case of a reader that blocks in Read yet exposes no SetReadDeadline cannot be interrupted by any of the above; ConnReadN still returns to the caller on cancel (that read goroutine is unavoidable for such readers). This targets net.Conn-style readers, which is ConnReadN's documented use.

Fixes #759

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
@G360-Niek G360-Niek changed the title fix(contextutil): prevent goroutine leak on context cancellation fix: prevent goroutine leak on context-cancelled reads (contextutil + reader.ConnReadN) Aug 6, 2026
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.

reader/contextutil: ConnReadN leaks a goroutine per context-cancelled read (unbuffered chan in ExecFuncWithTwoReturns)

1 participant