From 9ddcc52fe24f377fb2c20b6cf38fb549e8b74cad Mon Sep 17 00:00:00 2001 From: Andrei Matei Date: Mon, 13 Sep 2021 17:16:32 -0400 Subject: [PATCH] kvclient: switch error in ctx cancel edge case This patch changes the error returned by the DistSender on a cancelled ctx. Depending on the exactly who detects the ctx as cancelled, there are a number of different possibilities - too many to enumerate here. Generally, the client is supposed to get a context.Canceled error, wrapped in different layers. The code path that this patch changes is about the cancellation being detected by sendPartialBatch() without it having been previously detected by sendToReplicas(). This path is unusual (sendToReplicas() generally detects the ctx cancelled). It's also hard to test. Depending on the exact cancellation timing, it's possible though for sendPartialBatch() to detect it instead. In this case, this patch makes it so that, if sendToReplicas() returned a sendError (indicating that a suitable replica could not be reached and that the higher layer is expected to continue trying other replicas), the error returned to the client is a cancellation error instead of the sendError. Touches #69419 Release note: None --- pkg/kv/kvclient/kvcoord/dist_sender.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/kv/kvclient/kvcoord/dist_sender.go b/pkg/kv/kvclient/kvcoord/dist_sender.go index 30f6c72b389a..bbf409b74698 100644 --- a/pkg/kv/kvclient/kvcoord/dist_sender.go +++ b/pkg/kv/kvclient/kvcoord/dist_sender.go @@ -1558,9 +1558,6 @@ func (ds *DistSender) sendPartialBatch( } if err != nil { - // Set pErr so that, if we don't perform any more retries, the - // deduceRetryEarlyExitError() call below the loop is inhibited. - pErr = roachpb.NewError(err) switch { case errors.HasType(err, sendError{}): // We've tried all the replicas without success. Either they're all @@ -1582,6 +1579,9 @@ func (ds *DistSender) sendPartialBatch( routingTok.Evict(ctx) continue } + // Set pErr so that the deduceRetryEarlyExitError() call below the loop is + // inhibited. + pErr = roachpb.NewError(err) break }