Skip to content

Commit

Permalink
lightning: fix context.Cancelled nested in aws err (#49764)
Browse files Browse the repository at this point in the history
close #49117
  • Loading branch information
D3Hunter authored Dec 25, 2023
1 parent 116456d commit b719406
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 0 additions & 1 deletion br/pkg/lightning/log/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ go_library(
deps = [
"//pkg/util/logutil",
"@com_github_aws_aws_sdk_go//aws/awserr",
"@com_github_aws_aws_sdk_go//aws/request",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_log//:log",
"@org_golang_google_grpc//codes",
Expand Down
12 changes: 9 additions & 3 deletions br/pkg/lightning/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/pingcap/errors"
pclog "github.com/pingcap/log"
"github.com/pingcap/tidb/pkg/util/logutil"
Expand Down Expand Up @@ -188,8 +187,15 @@ func IsContextCanceledError(err error) bool {
}

// see https://github.com/aws/aws-sdk-go/blob/9d1f49ba/aws/credentials/credentials.go#L246-L249
if v, ok := err.(awserr.Error); ok {
return v.Code() == request.CanceledErrorCode
// 2 cases that have meet:
// awserr.New("RequestCanceled", "request context canceled", err) and the nested err is context.Canceled
// awserr.New( "MultipartUpload", "upload multipart failed", err) and the nested err is the upper one
if v, ok := err.(awserr.BatchedErrors); ok {
for _, origErr := range v.OrigErrs() {
if IsContextCanceledError(origErr) {
return true
}
}
}
return false
}
Expand Down
7 changes: 5 additions & 2 deletions br/pkg/lightning/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ func TestIsContextCanceledError(t *testing.T) {
require.True(t, log.IsContextCanceledError(context.Canceled))
require.True(t, log.IsContextCanceledError(status.Error(codes.Canceled, "")))
require.True(t, log.IsContextCanceledError(errors.Annotate(context.Canceled, "foo")))
require.True(t, log.IsContextCanceledError(awserr.New(request.CanceledErrorCode, "", nil)))
require.True(t, log.IsContextCanceledError(awserr.New(request.CanceledErrorCode, "", context.Canceled)))
require.True(t, log.IsContextCanceledError(awserr.New(
"MultipartUpload", "upload multipart failed",
awserr.New(request.CanceledErrorCode, "", context.Canceled))))
require.True(t, log.IsContextCanceledError(awserr.New(request.ErrCodeRequestError, "", context.Canceled)))

require.False(t, log.IsContextCanceledError(awserr.New(request.ErrCodeRequestError, "", nil)))
require.False(t, log.IsContextCanceledError(nil))
}

0 comments on commit b719406

Please sign in to comment.