Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-50307

While running in parallel for a loop, the index is wrong for each iteration

    XMLWordPrintable

Details

    Description

      When using a parallel step for an indexed loop, using the loop-iterator-index for internal usage results with wrong value (showing the max-index-value for all iterations).
      A workaround for it is to set a local variable for saving the current iteration-index and use the local variable.
      The following pipeline demonstrates the problem (the issue is demonstrated with the internal 'echo' steps):

      pipeline
       {
           agent any
           stages
           {
               stage('Explore bug')
               {
                    steps
                   {
                        script
                        {
                            int maxIndex1=3
                            def loopers1 = [:]
                            for (int loopIndex1 = 0; loopIndex1 < maxIndex1; loopIndex1++)
                           {
                               echo "loopIndex1=${loopIndex1}"
                               loopers1["loop-$\{loopIndex1}"] =
                               {
                                   echo "loopIndex1=${loopIndex1}"
                               }
                          }
                          parallel loopers1
                     }
                 }
           }
           stage('Bug workaround')
           {
               steps
               {
                   script
                   {
                       int maxIndex2=3
                       def loopers2 = [:]
                       for (int loopIndex2 = 0; loopIndex2 < maxIndex2; loopIndex2++)
                       {
                           int index2=loopIndex2
                           echo "index2=${index2}"
                           loopers2["loop-$\{index2}"] =
                           {
                               echo "index2=${index2}"
                           }
                      }
                     parallel loopers2
                 } 
               }
             }
           }
       }

       

      Attachments

        Activity

          Someone showed me this: https://jenkins.io/doc/pipeline/examples/#parallel-from-list which explains that this is not a bug but a feature (loopIndex1 byRef not byVal)). Still, I think that intuitively it looks like a bug since I expect it to behave in one way while it acts differently.

          yorammi Yoram Michaeli added a comment - Someone showed me this: https://jenkins.io/doc/pipeline/examples/#parallel-from-list  which explains that this is not a bug but a feature (loopIndex1 byRef not byVal)). Still, I think that intuitively it looks like a bug since I expect it to behave in one way while it acts differently.
          jglick Jesse Glick added a comment -

          This is just how Groovy works I am afraid. Java 8 allows for effectively-final variables, and signals a compilation error if the variable is not effectively final, but Groovy merrily compiles an incorrect program.

          jglick Jesse Glick added a comment - This is just how Groovy works I am afraid. Java 8 allows for effectively- final variables, and signals a compilation error if the variable is not effectively final, but Groovy merrily compiles an incorrect program.

          People

            Unassigned Unassigned
            yorammi Yoram Michaeli
            Votes:
            7 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: