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

NonCPS function returns wrong results!

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major Major
    • pipeline
    • None
    • Jenins 2.19.3 and Pipeline 2.4

      We had this issue inside of a Global Library but are able to reproduce it with a simple pipeline job.  

      Steps to Reproduce:

      1. Create a pipeline job with the following script:
        //Not the real function but good enough to demonstrate problem
        def getSlaves() { [
            [name: 'good', isOnline: true ],
            [name: 'bad' , isOnline: false]
        ]}
        
        @NonCPS
        def verifyAllSlavesOnline() {
          def errorList = []
          for (def slave in getSlaves()) {
            if(!slave.isOnline) {
              errorList <<  "Slave is off line: ${slave.name}"
            }
          }
          errorList
        }
        
        println verifyAllSlavesOnline()
      1. Expected output of println is
        [Slave is off line: bad]

        but instead with get:

        [{name=good, isOnline=true}, {name=bad, isOnline=false}]

        Essentially the return value from getSlaves() is being returned instead of the content of errorList.

      In our case, the use of @NonCPS here was in error because the original code that required it had been refactored into the getSlaves() function. So we were able to solve the problem with this rewrite:

      //Not the real function but good enough to demonstrate problem
      def getSlaves() { [
          [name: 'good', isOnline: true ],
          [name: 'bad' , isOnline: false]
      ]}
      
      def verifyAllSlavesOnline() {
        def errorList = []
        def slaves = getDrSlaves()
        for (def s = 0; s < slaves.size; s++ ) {
          def slave = slaves[s]
          if(!slave.isOnline) {
            errorList <<  "Slave is off line: ${slave.name}"
          }
        }
        errorList
      }
      
      println verifyAllSlavesOnline()

      The above produces the expected output.

      Decorating the corrected function with @NonCPS again causes it to return the same wrong results!

            Unassigned Unassigned
            kbaltrinic Kenneth Baltrinic
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: