Skip to content

Commit 3bb13db

Browse files
committed
Make Close idempotent
1 parent fdeb568 commit 3bb13db

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

connection.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,16 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {
105105
}
106106

107107
func (mc *mysqlConn) Close() (err error) {
108-
mc.writeCommandPacket(comQuit)
108+
// Makes Close idempotent
109+
if mc.netConn != nil {
110+
mc.writeCommandPacket(comQuit)
111+
mc.netConn.Close()
112+
mc.netConn = nil
113+
}
114+
109115
mc.cfg = nil
110116
mc.buf = nil
111-
mc.netConn.Close()
112-
mc.netConn = nil
117+
113118
return
114119
}
115120

rows.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ func (rows *mysqlRows) Close() (err error) {
4343

4444
// Remove unread packets from stream
4545
if !rows.eof {
46-
if rows.mc == nil {
46+
if rows.mc == nil || rows.mc.netConn == nil {
4747
return errors.New("Invalid Connection")
4848
}
4949

5050
err = rows.mc.readUntilEOF()
51+
52+
// explicitly set because readUntilEOF might return early in case of an
53+
// error
54+
rows.eof = true
5155
}
5256

5357
return
@@ -58,7 +62,7 @@ func (rows *mysqlRows) Next(dest []driver.Value) error {
5862
return io.EOF
5963
}
6064

61-
if rows.mc == nil {
65+
if rows.mc == nil || rows.mc.netConn == nil {
6266
return errors.New("Invalid Connection")
6367
}
6468

0 commit comments

Comments
 (0)