From 920b4167748faae0faa61318cfa74bf0d4c58465 Mon Sep 17 00:00:00 2001 From: xhe Date: Tue, 23 Aug 2022 21:48:28 +0800 Subject: [PATCH] session: handle nil store for plugins Signed-off-by: xhe --- session/tidb.go | 12 ++++++++++-- session/tidb_test.go | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/session/tidb.go b/session/tidb.go index eb2e51074989f..fd4411a18518c 100644 --- a/session/tidb.go +++ b/session/tidb.go @@ -51,11 +51,19 @@ type domainMap struct { } func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) { - key := store.UUID() - dm.mu.Lock() defer dm.mu.Unlock() + if store == nil { + for _, d := range dm.domains { + // return available domain if any + return d, nil + } + return nil, errors.New("can not find available domain for a nil store") + } + + key := store.UUID() + d = dm.domains[key] if d != nil { return diff --git a/session/tidb_test.go b/session/tidb_test.go index b88d1fd19873d..4f2a9e01bba4a 100644 --- a/session/tidb_test.go +++ b/session/tidb_test.go @@ -26,6 +26,14 @@ import ( "github.com/stretchr/testify/require" ) +func TestDomapHandleNil(t *testing.T) { + // this is required for enterprise plugins + // ref: https://github.com/pingcap/tidb/issues/37319 + require.NotPanics(t, func() { + _, _ = domap.Get(nil) + }) +} + func TestSysSessionPoolGoroutineLeak(t *testing.T) { store, dom := createStoreAndBootstrap(t) defer func() { require.NoError(t, store.Close()) }()