From f07951f34841b499388ea97789a7ec1c3cd84141 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 11 Jun 2024 22:34:45 +0900 Subject: [PATCH] fix some write error handling (#1595) interpolateParams() returned ErrInvalidConn without closing the connection. Since database/sql doesn't understand ErrInvalidConn, there is a risk that database/sql reuse this connection and ErrInvalidConn is returned repeatedly. --- connection.go | 6 ++++-- packets.go | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/connection.go b/connection.go index eff978d93..9c771b83c 100644 --- a/connection.go +++ b/connection.go @@ -217,8 +217,10 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin buf, err := mc.buf.takeCompleteBuffer() if err != nil { // can not take the buffer. Something must be wrong with the connection - mc.log(err) - return "", ErrInvalidConn + mc.cleanup() + // interpolateParams would be called before sending any query. + // So its safe to retry. + return "", driver.ErrBadConn } buf = buf[:0] argPos := 0 diff --git a/packets.go b/packets.go index 90a34728b..3bcba1c92 100644 --- a/packets.go +++ b/packets.go @@ -116,6 +116,8 @@ func (mc *mysqlConn) writePacket(data []byte) error { // Write packet if mc.writeTimeout > 0 { if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil { + mc.cleanup() + mc.log(err) return err } }