Serialization error moving @NonCPS method from pipeline script to shared library

1,157 views
Skip to first unread message

David Karr

unread,
Feb 6, 2017, 12:23:18 PM2/6/17
to Jenkins Users
I'm trying to move duplicated methods in several pipeline scripts to a shared library.  My first attempt, moving an enum type, worked fine.  My next test is moving a method annotated with "@NonCPS".  I noticed in the doc about writing shared libraries (https://jenkins.io/doc/book/pipeline/shared-libraries/), it had the following statement:

The Groovy source files in these directories get the same “CPS transformation” as in Scripted Pipeline.

I have no idea what that means, but I suppose it might have some relevant to my problem.

The method I'm trying to move is simply this:
@NonCPS
def computeCauseData() {
   
def result    = ""
   
def causeActions = currentBuild.rawBuild.getAction(hudson.model.CauseAction)
   
for (action in causeActions) {
        println
"action[${action}] displayName[${action.displayName}]"
       
for (cause in action.causes) {
            println
"cause[${cause}] shortDescription[${cause.shortDescription}]"
            result    
= "${cause.shortDescription}."
           
if (cause instanceof Cause.LegacyCodeCause) {
           
}
           
else if (cause instanceof TimerTrigger.TimerTriggerCause) {
           
}
           
else if (cause instanceof Cause.RemoteCause) {
               
Cause.RemoteCause    remoteCause    = (Cause.RemoteCause) cause
                result    
= "${cause.shortDescription}: Addr[${remoteCause.addr}] Note[${remoteCause.note}]."
           
}
           
else if (cause instanceof Cause.UserIdCause) {
           
}
           
else if (cause instanceof SCMTrigger.SCMTriggerCause) {
               
SCMTrigger.SCMTriggerCause    scmTriggerCause    = (SCMTrigger.SCMTriggerCause) cause
                println
"scmTriggerCause[${scmTriggerCause}]"
           
}
           
else if (cause instanceof GitStatus.CommitHookCause) {
               
GitStatus.CommitHookCause    commitHookCause    = (GitStatus.CommitHookCause) cause
               
String    sha1    = commitHookCause.sha1
                println
"sha1[${sha1}]"
           
}
           
else {
   
           
}
       
}
   
}
   
   
return result
}

This works fine in the pipeline script. When I moved it to my shared library (in "vars/computeCauseData.groovy") and commented out the definition in the pipeline script, and then ran my test, I got the following:
java.io.NotSerializableException: org.codehaus.groovy.control.ErrorCollector
        at org
.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
        at org
.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
        at org
.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
        at org
.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
        at org
.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
        at org
.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)

In the past, I've avoid serialization errors by nulling out variables before they cross a scope. I tried modifying this method, simply nulling out everything but "result" right after their last use, and that made no difference.  Same error.


Martín Pianello

unread,
Mar 15, 2018, 4:30:33 PM3/15/18
to Jenkins Users
Hi David,


A year later, you may have solved the issue or switched to a different solution altogether. Anyway, I stumbled across the same issue and coudn't find a definitive solution.

So, after some trial & error, I wanted to post how I solved this.

So, the code in "vars/myClass.groovy" is just glue code calling whatever classes there is in "src/....groovy" witch implements the real logic.
Also, according https://github.com/mkobit/jenkins-pipeline-shared-libraries-gradle-plugin/issues/17 I added the necessary @NonCPS annotations above the methods in classes located under src/, not in var/ (didn't work)

David Karr

unread,
Mar 15, 2018, 4:59:41 PM3/15/18
to Jenkins Users
Thanks for the update. I don't even remember the specific problem I
had, but my shared library is working fine, so I must have figured it
out. :)
> --
> You received this message because you are subscribed to the Google Groups
> "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jenkinsci-users/9269dc02-0783-4101-8739-1e3f6e9edd15%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages