Skip to content

Commit f07951f

Browse files
committed
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.
1 parent 4395c45 commit f07951f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

connection.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
217217
buf, err := mc.buf.takeCompleteBuffer()
218218
if err != nil {
219219
// can not take the buffer. Something must be wrong with the connection
220-
mc.log(err)
221-
return "", ErrInvalidConn
220+
mc.cleanup()
221+
// interpolateParams would be called before sending any query.
222+
// So its safe to retry.
223+
return "", driver.ErrBadConn
222224
}
223225
buf = buf[:0]
224226
argPos := 0

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)