Skip to content

Commit b45a6fc

Browse files
authored
fix some write error handling (#1596)
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. This PR is backport of #1595.
1 parent 62847f6 commit b45a6fc

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Diff for: connection.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
229229
buf, err := mc.buf.takeCompleteBuffer()
230230
if err != nil {
231231
// can not take the buffer. Something must be wrong with the connection
232-
mc.log(err)
233-
return "", ErrInvalidConn
232+
mc.cleanup()
233+
// interpolateParams would be called before sending any query.
234+
// So its safe to retry.
235+
return "", driver.ErrBadConn
234236
}
235237
buf = buf[:0]
236238
argPos := 0

Diff for: packets.go

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ func (mc *mysqlConn) writePacket(data []byte) error {
116116
// Write packet
117117
if mc.writeTimeout > 0 {
118118
if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil {
119+
mc.cleanup()
120+
mc.log(err)
119121
return err
120122
}
121123
}

0 commit comments

Comments
 (0)