From 84a67d15ac10fe25bedd496f1bd8735c0a43942d Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Thu, 28 Apr 2022 14:13:30 +0000 Subject: [PATCH] kvclient: fix gRPC stream leak in rangefeed client When the DistSender rangefeed client received a `RangeFeedError` message and propagated a retryable error up the stack, it would fail to close the existing gRPC stream, causing stream/goroutine leaks. Release note (bug fix): Fixed a goroutine leak when internal rangefeed clients received certain kinds of retriable errors. --- pkg/kv/kvclient/kvcoord/dist_sender_rangefeed.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/kv/kvclient/kvcoord/dist_sender_rangefeed.go b/pkg/kv/kvclient/kvcoord/dist_sender_rangefeed.go index 5228c57301a7..b7b8fad40499 100644 --- a/pkg/kv/kvclient/kvcoord/dist_sender_rangefeed.go +++ b/pkg/kv/kvclient/kvcoord/dist_sender_rangefeed.go @@ -388,6 +388,10 @@ func (ds *DistSender) singleRangeFeed( eventCh chan<- *roachpb.RangeFeedEvent, onRangeEvent onRangeEventCb, ) (hlc.Timestamp, error) { + // Ensure context is cancelled on all errors, to prevent gRPC stream leaks. + ctx, cancel := context.WithCancel(ctx) + defer cancel() + args := roachpb.RangeFeedRequest{ Span: span, Header: roachpb.Header{