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

Workflow script fails if CPS-transformed methods are called from constructors

    XMLWordPrintable

    Details

    • Similar Issues:
    • Sprint:
      Pipeline - April 2018

      Description

      When a CPS-transformed method is called from a Groovy class constructor, the CpsCallableInvocation is not caught and aborts the build.

      Here is a minimally-reproducible test case:

      class B { 
          static def sqr(x) { x*x } 
      }
      
      class A { 
           A(y) { 
              B.sqr(y)
           }
      }
      
      def a = new A(42)
      

      The resulting CpsCallableInvocation is not caught and the build aborts:

      Running: End of Workflow
      com.cloudbees.groovy.cps.impl.CpsCallableInvocation
      	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      	at java.lang.reflect.Constructor.newInstance(Constructor.java:534)
      	at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
      	at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:71)
      	at org.codehaus.groovy.runtime.callsite.ConstructorSite.callConstructor(ConstructorSite.java:42)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:54)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:198)
      	at B.sqr(WorkflowScript)
      	at B$sqr.call(Unknown Source)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
      	at A.<init>(WorkflowScript:11)
      	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      	at java.lang.reflect.Constructor.newInstance(Constructor.java:534)
      	at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
      	at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:54)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
      	at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.constructorCall(DefaultInvoker.java:20)
      	at WorkflowScript.run(WorkflowScript:15)
      	at ___cps.transform___(Native Method)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:90)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:76)
      	at sun.reflect.GeneratedMethodAccessor498.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:622)
      	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
      	at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
      	at com.cloudbees.groovy.cps.Next.step(Next.java:58)
      	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:145)
      	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:267)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:70)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:176)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:174)
      	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
      	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:111)
      	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
      	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
      	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      	at java.lang.Thread.run(Thread.java:701)
      Finished: FAILURE
      

        Attachments

          Issue Links

            Activity

            Hide
            tracetronic TraceTronic GmbH added a comment -

            Is there any news on this problem? We are encountering the same CpsCallableInvocation message when calling a static method from inside a Groovy class constructor.

            Show
            tracetronic TraceTronic GmbH added a comment - Is there any news on this problem? We are encountering the same CpsCallableInvocation message when calling a static method from inside a Groovy class constructor.
            Hide
            abayer Andrew Bayer added a comment -

            So I guess the question here is why are constructors not transformed? Same with object initializer statements. I assume there's a good reason, but off the top of my head I'm not sure what that is.

            Show
            abayer Andrew Bayer added a comment - So I guess the question here is why are constructors not transformed? Same with object initializer statements. I assume there's a good reason, but off the top of my head I'm not sure what that is.
            Hide
            abayer Andrew Bayer added a comment -

            Ok, I now know why the constructors aren't transformed. If we throw a CpsCallableInvocation within a constructor (or an object initializer), the result is that we haven't actually created the object, so there's no way back to the continuation. I've opened a PR (https://github.com/cloudbees/groovy-cps/pull/83) adding some javadoc explaining this so that I and others get reminded of this in the future if needed. But that does mean this is a won't fix - without some theoretical and super-gnarly low-level magic, we just can't do this, and that magic is probably a bad idea even before taking into account the added complexity and fragility. Sorry!

            Show
            abayer Andrew Bayer added a comment - Ok, I now know why the constructors aren't transformed. If we throw a CpsCallableInvocation within a constructor (or an object initializer), the result is that we haven't actually created the object, so there's no way back to the continuation. I've opened a PR ( https://github.com/cloudbees/groovy-cps/pull/83 ) adding some javadoc explaining this so that I and others get reminded of this in the future if needed. But that does mean this is a won't fix - without some theoretical and super-gnarly low-level magic, we just can't do this, and that magic is probably a bad idea even before taking into account the added complexity and fragility. Sorry!
            Hide
            yngvark Yngvar Kristiansen added a comment -

            Is there any way of putting a nice error message in the jenkins log? It took some time to figure this out. In fact, all of my @NonCps-related problems have been time consuming, especially when you have never heard of it before.

             

            Show
            yngvark Yngvar Kristiansen added a comment - Is there any way of putting a nice error message in the jenkins log? It took some time to figure this out. In fact, all of my @NonCps-related problems have been time consuming, especially when you have never heard of it before.  
            Hide
            jglick Jesse Glick added a comment -

            Is there any way of putting a nice error message in the jenkins log?

            JENKINS-31314

            Show
            jglick Jesse Glick added a comment - Is there any way of putting a nice error message in the jenkins log? JENKINS-31314

              People

              Assignee:
              abayer Andrew Bayer
              Reporter:
              akbertram Alexander Bertram
              Votes:
              6 Vote for this issue
              Watchers:
              16 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: