Skip to content

Commit 05325d8

Browse files
authored
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 2f7015e commit 05325d8

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
@@ -230,8 +230,10 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
230230
buf, err := mc.buf.takeCompleteBuffer()
231231
if err != nil {
232232
// can not take the buffer. Something must be wrong with the connection
233-
mc.log(err)
234-
return "", ErrInvalidConn
233+
mc.cleanup()
234+
// interpolateParams would be called before sending any query.
235+
// So its safe to retry.
236+
return "", driver.ErrBadConn
235237
}
236238
buf = buf[:0]
237239
argPos := 0

Diff for: packets.go

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

0 commit comments

Comments
 (0)