Groovy bug when creating Closure in loop

This issue is archived. You can view it, but you can't modify it. Learn more

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.

            Assignee:
            Jesse Glick
            Reporter:
            Mark Lagendijk
            Archiver:
            Jenkins Service Account

              Created:
              Updated:
              Resolved:
              Archived: