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

ClassCastException when using Shared Libraries defined in Folder

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      Issue

      When SharedLibraries are defined at the folder level, pipeline script fails when instantiating a library object in some circumstances (scenario below).

      It results in a *ClassCastException*:

      Started by user admin
      Loading library jenkins-groovy-in-folder@zendesk/46810-simple
       > git rev-parse --is-inside-work-tree # timeout=10
      Fetching changes from the remote Git repository
       > git config remote.origin.url https://github/myuser/shared-librairies.git # timeout=10
      Fetching upstream changes from https://github/myuser/shared-librairies.git
       > git --version # timeout=10
      using GIT_ASKPASS to set credentials myuser-up
       > git fetch --tags --progress https://github/myuser/shared-librairies.git +refs/heads/*:refs/remotes/origin/*
       > git rev-parse refs/remotes/origin/zendesk/46810-simple^{commit} # timeout=10
       > git rev-parse refs/remotes/origin/origin/zendesk/46810-simple^{commit} # timeout=10
      Checking out Revision aa272aa786709d62813c65ae0b743899f0236c63 (refs/remotes/origin/zendesk/46810-simple)
       > git config core.sparsecheckout # timeout=10
       > git checkout -f aa272aa786709d62813c65ae0b743899f0236c63
      First time build. Skipping changelog.
      [Pipeline] node
      Running on master in /tmp/je-1-home/workspace/Folder/PipelineFromSCM
      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (Test)
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      hudson.remoting.ProxyException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.dohbedoh.example.MyObject@67907171' with class 'com.dohbedoh.example.MyObject' to class 'com.dohbedoh.example.MyObject'
      	at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
      	at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
      	at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
      	at Unknown.Unknown(Unknown)
      	at ___cps.transform___(Native Method)
      	at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.set(LocalVariableBlock.java:45)
      	at com.cloudbees.groovy.cps.impl.AssignmentBlock$ContinuationImpl.assignAndDone(AssignmentBlock.java:70)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
      	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:76)
      	at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
      	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	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:74)
      	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
      	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:328)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:240)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:228)
      	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
      	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
      	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at java.lang.Thread.run(Thread.java:745)
      Finished: FAILURE
      

      Scneario:

      Shared Library code

      Object definition in `com.dohbedoh.example.MyObject`

      package com.dohbedoh.example;
      
      public class MyObject implements Serializable {
      
        public MyObject(){ } 
        
      }
      

      Global variables: `vars/myobjects.groovy`

      import groovy.transform.Field;
      import com.dohbedoh.example.MyObject;
      
      @Field final MyObject MY_OBJECT_1 = new MyObject();
      @Field final MyObject MY_OBJECT_2 = new MyObject();
      

      Jenkins: Add Shared Libraries to Folder

      Jenkins: Pipeline Script

      Here is the pipeline script:

      @Library("jenkins-groovy-in-folder@zendesk/46810-simple") _
      node {
        stage("Test"){
          com.dohbedoh.example.MyObject testObject = myobjects.MY_OBJECT_1
        }
      }
      

      This results in the ClassCastException shown above.

      Workaround

      I tried with Modern/Legacy SCMs. Also tried with Multibranch project. I am always hitting the same exception.

      • A workaround is to define the Shared Library globally under *Manage Jenkins > Configure System*.
      • Another workaround is to use def testObject = myobjects.MY_OBJECT_1 instead of com.dohbedoh.example.MyObject testObject = myobjects.MY_OBJECT_1.

        Attachments

          Activity

          Hide
          jglick Jesse Glick added a comment -

          Something is wrong with the way InnerLoader is used. A new one is created by GroovyClassLoader.createCollector, called by doParseClass. In all cases a different InnerLoader defines the global variable as opposed to the package. In most cases the field in the variable shares the same loader as the explicit class reference; sometimes it instead shares the loader as the variable, in which case the error arises. I have not managed to identify any nondeterministic code which would explain the difference, but it seems that suppressing this whole system fixes the problem.

          Show
          jglick Jesse Glick added a comment - Something is wrong with the way InnerLoader is used. A new one is created by GroovyClassLoader.createCollector , called by doParseClass . In all cases a different InnerLoader defines the global variable as opposed to the package. In most cases the field in the variable shares the same loader as the explicit class reference; sometimes it instead shares the loader as the variable, in which case the error arises. I have not managed to identify any nondeterministic code which would explain the difference, but it seems that suppressing this whole system fixes the problem.
          Hide
          jglick Jesse Glick added a comment -

          workflow-cps PR 120 is the actual fix.

          Show
          jglick Jesse Glick added a comment - workflow-cps PR 120 is the actual fix.
          Hide
          jglick Jesse Glick added a comment -

          Also passes tests in workflow-cps-checkpoint, and WorkflowPluginTest in acceptance-test-harness with the exception of this issue.

          Show
          jglick Jesse Glick added a comment - Also passes tests in workflow-cps-checkpoint , and WorkflowPluginTest in acceptance-test-harness with the exception of  this issue .
          Hide
          scm_issue_link SCM/JIRA link daemon added a comment -

          Code changed in jenkins
          User: Jesse Glick
          Path:
          src/main/java/org/jenkinsci/plugins/workflow/cps/CpsGroovyShell.java
          http://jenkins-ci.org/commit/workflow-cps-plugin/06e2dbba5258484619489f0796ae8d49b0a54d1b
          Log:
          JENKINS-43019 Suppressing GroovyClassLoader.InnerLoader fixes a subtle CCE observed in libraries.

          Show
          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Jesse Glick Path: src/main/java/org/jenkinsci/plugins/workflow/cps/CpsGroovyShell.java http://jenkins-ci.org/commit/workflow-cps-plugin/06e2dbba5258484619489f0796ae8d49b0a54d1b Log: JENKINS-43019 Suppressing GroovyClassLoader.InnerLoader fixes a subtle CCE observed in libraries.
          Hide
          scm_issue_link SCM/JIRA link daemon added a comment -

          Code changed in jenkins
          User: Jesse Glick
          Path:
          src/main/java/org/jenkinsci/plugins/workflow/cps/CpsGroovyShell.java
          http://jenkins-ci.org/commit/workflow-cps-plugin/651e986861dfb13bee3eff39ec3e91d6cec2ae65
          Log:
          Merge pull request #120 from jglick/CleanGroovyClassLoader-JENKINS-43019

          JENKINS-43019 Suppressing GroovyClassLoader.InnerLoader fixes stuff

          Compare: https://github.com/jenkinsci/workflow-cps-plugin/compare/b904ffb20053...651e986861df

          Show
          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Jesse Glick Path: src/main/java/org/jenkinsci/plugins/workflow/cps/CpsGroovyShell.java http://jenkins-ci.org/commit/workflow-cps-plugin/651e986861dfb13bee3eff39ec3e91d6cec2ae65 Log: Merge pull request #120 from jglick/CleanGroovyClassLoader- JENKINS-43019 JENKINS-43019 Suppressing GroovyClassLoader.InnerLoader fixes stuff Compare: https://github.com/jenkinsci/workflow-cps-plugin/compare/b904ffb20053...651e986861df

            People

            • Assignee:
              jglick Jesse Glick
              Reporter:
              allan_burdajewicz Allan BURDAJEWICZ
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: