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

Groovy bug when creating Closure in loop

XMLWordPrintable

      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.

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

              Created:
              Updated:
              Resolved: