diff --git a/domain/domain.go b/domain/domain.go index a4caf5baa7af7..8061751fc852b 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -337,6 +337,12 @@ func (do *Domain) Reload() error { // LogSlowQuery keeps topN recent slow queries in domain. func (do *Domain) LogSlowQuery(query *SlowQueryInfo) { + do.slowQuery.mu.RLock() + defer do.slowQuery.mu.RUnlock() + if do.slowQuery.mu.closed { + return + } + select { case do.slowQuery.ch <- query: default: @@ -459,8 +465,8 @@ func (do *Domain) Close() { if do.etcdClient != nil { terror.Log(errors.Trace(do.etcdClient.Close())) } - do.slowQuery.Close() do.sysSessionPool.Close() + do.slowQuery.Close() do.wg.Wait() log.Info("[domain] close") } diff --git a/domain/topn_slow_query.go b/domain/topn_slow_query.go index 52251d34475f2..f8ae69c0be67e 100644 --- a/domain/topn_slow_query.go +++ b/domain/topn_slow_query.go @@ -115,6 +115,11 @@ type topNSlowQueries struct { period time.Duration ch chan *SlowQueryInfo msgCh chan *showSlowMessage + + mu struct { + sync.RWMutex + closed bool + } } func newTopNSlowQueries(topN int, period time.Duration, queueSize int) *topNSlowQueries { @@ -196,6 +201,10 @@ func (q *topNSlowQueries) QueryTop(count int, kind ast.ShowSlowKind) []*SlowQuer } func (q *topNSlowQueries) Close() { + q.mu.Lock() + q.mu.closed = true + q.mu.Unlock() + close(q.ch) }