From d86c4527bae98ccd4e5060f72887520ce30eda5e Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 17 Mar 2024 13:30:21 +0900 Subject: [PATCH] fix race condition when context is canceled (#1562) Fix #1559. --- connection.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/connection.go b/connection.go index 5061b69ca..f3656f0e6 100644 --- a/connection.go +++ b/connection.go @@ -138,7 +138,7 @@ func (mc *mysqlConn) Close() (err error) { } mc.cleanup() - + mc.clearResult() return } @@ -153,13 +153,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 {