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

Groovy bug when creating Closure in loop

    Details

    • Similar Issues:

      Description

      The bug occurs with the latest version of Jenkins (2.64) and all the plugins. Whenever you create a Closure in a loop the latest instance of the iteratee will be used when the closure is executed.
      This happens with all loops: for(int i = 0), for(Item item: items), each, collect, etc.

      Minimal example, reproducing the issue:

      List<String> values = ['value1', 'value2']
      
      // Works correctly, prints 'value1' and 'value2'
      values.each { value -> echo value }
      
      // Doesn't work correctly, prints 'value2' and 'value2'
      List<Closure> steps = values.collect { value -> return { echo value } }
      steps.each { step -> step() }
      

      This bug is usually encountered when trying to create parallel steps out of a collection. But since I was able to reproduce the issue without using 'parallel', I hope this might shed new light on the underlying problem.

        Attachments

          Issue Links

            Activity

            Hide
            markl_lagendijk Mark Lagendijk added a comment -

            I added JENKINS-38268 as related issue, because it appears to be similar, but not quite the same.
            Feel free to remove the link, if it is not actually related.

            Show
            markl_lagendijk Mark Lagendijk added a comment - I added JENKINS-38268 as related issue, because it appears to be similar, but not quite the same. Feel free to remove the link, if it is not actually related.
            Hide
            jglick Jesse Glick added a comment -

            Sounds like it is probably the same.

            Show
            jglick Jesse Glick added a comment - Sounds like it is probably the same.

              People

              • Assignee:
                jglick Jesse Glick
                Reporter:
                markl_lagendijk Mark Lagendijk
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: