Skip to content

Commit a832658

Browse files
committed
fix race, another try
1 parent e9b7c9a commit a832658

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Diff for: connection.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (mc *mysqlConn) Close() (err error) {
133133
}
134134

135135
mc.cleanup()
136-
136+
mc.clearResult()
137137
return
138138
}
139139

@@ -148,13 +148,16 @@ func (mc *mysqlConn) cleanup() {
148148

149149
// Makes cleanup idempotent
150150
close(mc.closech)
151-
if mc.netConn == nil {
151+
nc := mc.netConn
152+
if nc == nil {
152153
return
153154
}
154-
if err := mc.netConn.Close(); err != nil {
155+
if err := nc.Close(); err != nil {
155156
mc.cfg.Logger.Print(err)
156157
}
157-
mc.clearResult()
158+
// This function can be called from multiple goroutines.
159+
// So we can not mc.clearResult() here.
160+
// Caller should do it if they are in safe goroutine.
158161
}
159162

160163
func (mc *mysqlConn) error() error {
@@ -439,13 +442,7 @@ func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
439442
// finish is called when the query has canceled.
440443
func (mc *mysqlConn) cancel(err error) {
441444
mc.canceled.Set(err)
442-
nc := mc.netConn
443-
if nc != nil {
444-
_ = nc.SetDeadline(time.Now()) // wake up pending reads/writes
445-
// Ignore error because:
446-
// - If the connection is already closed, thats fine.
447-
// - If the connection return error other reasons, we can not do anything about it.
448-
}
445+
mc.cleanup()
449446
}
450447

451448
// finish is called when the query has succeeded.

0 commit comments

Comments
 (0)