Skip to content

Commit

Permalink
fix some write error handling (go-sql-driver#1595)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
methane committed Jun 11, 2024
1 parent 2f7015e commit 05325d8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,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
Expand Down
2 changes: 2 additions & 0 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,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
}
}
Expand Down

0 comments on commit 05325d8

Please sign in to comment.