fix: accumulating context.WithTimeout cancel funcs in retry loop (goroutine/timer leak)#36
Open
amathxbt wants to merge 1 commit into
Open
fix: accumulating context.WithTimeout cancel funcs in retry loop (goroutine/timer leak)#36amathxbt wants to merge 1 commit into
amathxbt wants to merge 1 commit into
Conversation
In doRequestWithRetry(), each retry iteration that has a timeout configured creates a new context with context.WithTimeout and defers its cancel. Because defer runs when the enclosing *function* returns (not when the loop iteration ends), every retry's cancel accumulates on the defer stack and is only called when doRequestWithRetry exits. This means: 1. Contexts from previous retries are not cancelled promptly, leaking their goroutine/timer resources for the duration of all remaining retries. 2. On a long retry chain (e.g. rsh-retry=5 with a short timeout) this creates 5 live timer goroutines simultaneously instead of 1. Fix: call cancel() explicitly after the client.Do() call (both success and error paths) so each iteration cleans up its own context immediately.
Author
|
Hey @akasuv @HappySean2845 @hughzhou-gif @ZhimaoL, wanted to bring this one to your attention. Anyone running with both a timeout and retries set is hitting this, which is probably the majority of production usage. The timer goroutines stacking up is not going to crash anything immediately but it does build up quietly and the context chaining issue can cause some unexpected cancellation behaviour on longer retry sequences. The fix is just one line change. |
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.
Problem
In
cli/request.go,doRequestWithRetry()deferscancel()inside the retry loop:With
--rsh-retry=5and a timeout, 6 cancel funcs accumulate on the defer stack. Eachcontext.WithTimeoutspawns an internal timer goroutine that stays alive until its cancel is called. This creates 6 simultaneous timer goroutines instead of 1.Each
req.WithContext(ctx)also chains contexts, so retry N carries N layers of deadline wrappers — an inner timeout cancellation can propagate outward unexpectedly.Fix
Call
cancel()explicitly afterclient.Do(req)completes (success, error, or retry) so each iteration cleans up its context and timer immediately.Impact
Affects any invocation with both
--rsh-timeoutand--rsh-retry > 0set, which is the recommended configuration for unreliable networks.