Skip to content

Commit c1423fc

Browse files
defer cancel() leaks memory (#748)
* defer cancel() leaks memory * Update interceptors/retry/retry.go Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> --------- Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
1 parent d75a1b8 commit c1423fc

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

interceptors/retry/retry.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ func UnaryClientInterceptor(optFuncs ...CallOption) grpc.UnaryClientInterceptor
4444
callOpts.onRetryCallback(parentCtx, attempt, lastErr)
4545
}
4646
callCtx, cancel := perCallContext(parentCtx, callOpts, attempt)
47-
defer cancel() // Clean up potential resources.
4847
lastErr = invoker(callCtx, method, req, reply, cc, grpcOpts...)
48+
// Cancel the context immediately after invoking the next call in the chain to avoid
49+
// holing onto its memory until this function returns.
50+
cancel()
4951
// TODO(mwitkow): Maybe dial and transport errors should be retriable?
5052
if lastErr == nil {
5153
return nil

0 commit comments

Comments
 (0)