From be8468ed011fbe8699266337bfa0a101470b199f Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 17 Mar 2024 13:52:22 +0900 Subject: [PATCH] fix race condition when context is canceled (#1562) Fix #1559. (cherry picked from commit d86c4527bae98ccd4e5060f72887520ce30eda5e) --- connection.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/connection.go b/connection.go index 2bc081c43..768794f8e 100644 --- a/connection.go +++ b/connection.go @@ -137,7 +137,7 @@ func (mc *mysqlConn) Close() (err error) { } mc.cleanup() - + mc.clearResult() return } @@ -152,13 +152,16 @@ func (mc *mysqlConn) cleanup() { // Makes cleanup idempotent close(mc.closech) - if mc.netConn == nil { + nc := mc.netConn + if nc == nil { return } - if err := mc.netConn.Close(); err != nil { + if err := nc.Close(); err != nil { mc.log(err) } - mc.clearResult() + // This function can be called from multiple goroutines. + // So we can not mc.clearResult() here. + // Caller should do it if they are in safe goroutine. } func (mc *mysqlConn) error() error {