Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/util: fix panic caused by logging grpc err (#52179) #52188

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions util/topsql/reporter/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"errors"
"time"

tidberrors "github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tipb/go-tipb"
Expand Down Expand Up @@ -96,6 +98,11 @@ func (ds *pubSubDataSink) OnReporterClosing() {

func (ds *pubSubDataSink) run() error {
defer func() {
if r := recover(); r != nil {
err := tidberrors.Errorf("%v", r)
// To catch panic when log grpc error. https://github.com/pingcap/tidb/issues/51301.
logutil.BgLogger().Error("[top-sql] got panic in pub sub data sink, just ignore", zap.Error(err))
}
ds.registerer.Deregister(ds)
ds.cancel()
}()
Expand Down Expand Up @@ -132,6 +139,7 @@ func (ds *pubSubDataSink) run() error {
return ctx.Err()
}

failpoint.Inject("mockGrpcLogPanic", nil)
if err != nil {
logutil.BgLogger().Warn(
"[top-sql] pubsub datasink failed to send data to subscriber",
Expand Down
5 changes: 5 additions & 0 deletions util/topsql/reporter/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/tipb/go-tipb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
)

Expand Down Expand Up @@ -85,6 +87,8 @@ func TestPubSubDataSink(t *testing.T) {
_ = ds.run()
}()

panicPath := "github.com/pingcap/tidb/pkg/util/topsql/reporter/mockGrpcLogPanic"
require.NoError(t, failpoint.Enable(panicPath, "panic"))
err := ds.TrySend(&ReportData{
DataRecords: []tipb.TopSQLRecord{{
SqlDigest: []byte("S1"),
Expand Down Expand Up @@ -117,4 +121,5 @@ func TestPubSubDataSink(t *testing.T) {
mockStream.Unlock()

ds.OnReporterClosing()
require.NoError(t, failpoint.Disable(panicPath))
}