Skip to content

Commit 8a24bdf

Browse files
committed
fix race condition when context is canceled (go-sql-driver#1562)
Fix go-sql-driver#1559. (cherry picked from commit d86c452)
1 parent 33b7747 commit 8a24bdf

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Diff for: connection.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (mc *mysqlConn) Close() (err error) {
132132
}
133133

134134
mc.cleanup()
135-
135+
mc.clearResult()
136136
return
137137
}
138138

@@ -147,13 +147,16 @@ func (mc *mysqlConn) cleanup() {
147147

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

159162
func (mc *mysqlConn) error() error {

0 commit comments

Comments
 (0)