Skip to content

Commit d86c452

Browse files
authored
fix race condition when context is canceled (#1562)
Fix #1559.
1 parent 1a64773 commit d86c452

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

connection.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (mc *mysqlConn) Close() (err error) {
138138
}
139139

140140
mc.cleanup()
141-
141+
mc.clearResult()
142142
return
143143
}
144144

@@ -153,13 +153,16 @@ func (mc *mysqlConn) cleanup() {
153153

154154
// Makes cleanup idempotent
155155
close(mc.closech)
156-
if mc.netConn == nil {
156+
nc := mc.netConn
157+
if nc == nil {
157158
return
158159
}
159-
if err := mc.netConn.Close(); err != nil {
160+
if err := nc.Close(); err != nil {
160161
mc.log(err)
161162
}
162-
mc.clearResult()
163+
// This function can be called from multiple goroutines.
164+
// So we can not mc.clearResult() here.
165+
// Caller should do it if they are in safe goroutine.
163166
}
164167

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

0 commit comments

Comments
 (0)