From 24c7f8c39b3e5c8a7dc7f3a1abcb46757381966b Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 9 Nov 2023 17:54:49 +0800 Subject: [PATCH] server,session: make sure ResultSet.Close() errors return to the client (#48447) ref pingcap/tidb#48411, close pingcap/tidb#48446 --- pkg/server/conn.go | 9 ++++----- pkg/session/session.go | 16 ++++++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/server/conn.go b/pkg/server/conn.go index 2af3c01dd468c..1aa95b32a4cda 100644 --- a/pkg/server/conn.go +++ b/pkg/server/conn.go @@ -2049,11 +2049,7 @@ func (cc *clientConn) handleStmt(ctx context.Context, stmt ast.StmtNode, warns [ execStmt.(*executor.ExecStmt).FinishExecuteStmt(0, err, false) } } - if err != nil { - return false, err - } - - return false, nil + return false, err } func (cc *clientConn) handleFileTransInConn(ctx context.Context, status uint16) (bool, error) { @@ -2285,6 +2281,9 @@ func (cc *clientConn) writeChunks(ctx context.Context, rs resultset.ResultSet, b stmtDetail.WriteSQLRespDuration += time.Since(start) } } + if err := rs.Close(); err != nil { + return false, err + } if stmtDetail != nil { start = time.Now() diff --git a/pkg/session/session.go b/pkg/session/session.go index 74e4877d8bae1..102c0b8f164c7 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -2475,16 +2475,20 @@ const ExecStmtVarKey ExecStmtVarKeyType = 0 // RecordSet, so this struct exists and RecordSet.Close() is overrided handle that. type execStmtResult struct { sqlexec.RecordSet - se *session - sql sqlexec.Statement + se *session + sql sqlexec.Statement + closed bool } func (rs *execStmtResult) Close() error { - se := rs.se - if err := rs.RecordSet.Close(); err != nil { - return finishStmt(context.Background(), se, err, rs.sql) + if rs.closed { + return nil } - return finishStmt(context.Background(), se, nil, rs.sql) + se := rs.se + err := rs.RecordSet.Close() + err = finishStmt(context.Background(), se, err, rs.sql) + rs.closed = true + return err } // rollbackOnError makes sure the next statement starts a new transaction with the latest InfoSchema.