Skip to content

Commit

Permalink
defer cancel() leaks memory (#748)
Browse files Browse the repository at this point in the history
* defer cancel() leaks memory

* Update interceptors/retry/retry.go

Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]>

---------

Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]>
  • Loading branch information
JacobSMoller and johanbrandhorst authored Feb 11, 2025
1 parent d75a1b8 commit c1423fc
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion interceptors/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ func UnaryClientInterceptor(optFuncs ...CallOption) grpc.UnaryClientInterceptor
callOpts.onRetryCallback(parentCtx, attempt, lastErr)
}
callCtx, cancel := perCallContext(parentCtx, callOpts, attempt)
defer cancel() // Clean up potential resources.
lastErr = invoker(callCtx, method, req, reply, cc, grpcOpts...)
// Cancel the context immediately after invoking the next call in the chain to avoid
// holing onto its memory until this function returns.
cancel()
// TODO(mwitkow): Maybe dial and transport errors should be retriable?
if lastErr == nil {
return nil
Expand Down

0 comments on commit c1423fc

Please sign in to comment.