Skip to content

Commit

Permalink
server: fix connection close on network timeout/read error (#34757) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Jun 20, 2022
1 parent 62fd5bb commit de7612f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,16 @@ func (cc *clientConn) Close() error {

func closeConn(cc *clientConn, connections int) error {
metrics.ConnGauge.Set(float64(connections))
err := cc.bufReadConn.Close()
terror.Log(err)
if cc.bufReadConn != nil {
err := cc.bufReadConn.Close()
if err != nil {
// We need to expect connection might have already disconnected.
// This is because closeConn() might be called after a connection read-timeout.
logutil.Logger(context.Background()).Debug("could not close connection", zap.Error(err))
}
}
// Close statements and session
// This will release advisory locks, row locks, etc.
if cc.ctx != nil {
return cc.ctx.Close()
}
Expand Down Expand Up @@ -1021,10 +1029,6 @@ func (cc *clientConn) Run(ctx context.Context) {
terror.Log(err)
metrics.PanicCounter.WithLabelValues(metrics.LabelSession).Inc()
}
if atomic.LoadInt32(&cc.status) != connStatusShutdown {
err := cc.Close()
terror.Log(err)
}
}()

// Usually, client connection status changes between [dispatching] <=> [reading].
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ func (s *Server) onConn(conn *clientConn) {
logutil.Logger(ctx).Debug("new connection", zap.String("remoteAddr", conn.bufReadConn.RemoteAddr().String()))

defer func() {
terror.Log(conn.Close())
logutil.Logger(ctx).Debug("connection closed")
}()
s.rwlock.Lock()
Expand Down

0 comments on commit de7612f

Please sign in to comment.