diff --git a/pkg/distsql/select_result.go b/pkg/distsql/select_result.go index 82a6bec7135c8..d298be1ff4b61 100644 --- a/pkg/distsql/select_result.go +++ b/pkg/distsql/select_result.go @@ -612,7 +612,7 @@ func (r *selectResult) Close() error { } } } - if r.stats != nil { + if r.stats != nil && r.ctx != nil { defer func() { if ci, ok := r.resp.(copr.CopInfo); ok { r.stats.buildTaskDuration = ci.GetBuildTaskElapsed()