diff --git a/server/server.go b/server/server.go index 3553c1cdda44e..24a664db554c5 100644 --- a/server/server.go +++ b/server/server.go @@ -365,18 +365,18 @@ func (s *Server) Close() { func (s *Server) onConn(conn *clientConn) { ctx := logutil.WithConnID(context.Background(), conn.connectionID) if err := conn.handshake(ctx); err != nil { - if plugin.IsEnable(plugin.Audit) { + if plugin.IsEnable(plugin.Audit) && conn.ctx != nil { conn.ctx.GetSessionVars().ConnectionInfo = conn.connectInfo() + err = plugin.ForeachPlugin(plugin.Audit, func(p *plugin.Plugin) error { + authPlugin := plugin.DeclareAuditManifest(p.Manifest) + if authPlugin.OnConnectionEvent != nil { + pluginCtx := context.WithValue(context.Background(), plugin.RejectReasonCtxValue{}, err.Error()) + return authPlugin.OnConnectionEvent(pluginCtx, plugin.Reject, conn.ctx.GetSessionVars().ConnectionInfo) + } + return nil + }) + terror.Log(err) } - err = plugin.ForeachPlugin(plugin.Audit, func(p *plugin.Plugin) error { - authPlugin := plugin.DeclareAuditManifest(p.Manifest) - if authPlugin.OnConnectionEvent != nil { - pluginCtx := context.WithValue(context.Background(), plugin.RejectReasonCtxValue{}, err.Error()) - return authPlugin.OnConnectionEvent(pluginCtx, plugin.Reject, conn.ctx.GetSessionVars().ConnectionInfo) - } - return nil - }) - terror.Log(err) // Some keep alive services will send request to TiDB and disconnect immediately. // So we only record metrics. metrics.HandShakeErrorCounter.Inc()