From 036fb24bf2cff8b1c874dc29486275f17c59552e Mon Sep 17 00:00:00 2001 From: lysu Date: Fri, 3 Apr 2020 20:34:30 +0800 Subject: [PATCH] server: fix SIGSEGV caused by port probing with audit plugin (#16038) --- server/server.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/server/server.go b/server/server.go index faac3b105a187..7ef8d39f9c5df 100644 --- a/server/server.go +++ b/server/server.go @@ -346,18 +346,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()