[Pipeline] @NonCPS causes "sh" to hang with parallel steps

1,571 views
Skip to first unread message

Jørgen Sølvernes Sandnes

unread,
Jan 4, 2017, 6:40:10 AM1/4/17
to Jenkins Users
Hi!

I struggled for a long time trying to figure out why my parallel steps kept hanging when running shell commands (sh). After trying a lot of different things, I found out that the @NonCPS annotation caused this behavior to happen. See the sample Jenkinsfile below. When I commented the @NonCPS line out it started working again. I don't know enough about (Non)CPS stuff to figure out if this is the way it's supposed to work, or if it's a bug of some sort. Are there any experts out there who know? :)

Best regards,
Jørgen

Jenkinsfile: 

#!groovy​
/*
* Reproduction of the "hanging sh" problem
*/
timestamps {
node {

stage ("Running in sequence") {
echo "Start of stage running in sequence"

sh "pwd"

echo "End of stage running in sequence"
}

stage ("Running in parallel") {
echo "Start of stage running in parallel"

def parallelSteps = [:]
parallelSteps["step1"] = createParallelStep()

parallel parallelSteps

echo "End of stage running in parallel"
}
}
}

@NonCPS
def createParallelStep() {
echo "Creates parallel step"
return {
node {
echo "Running parallel step"

sh "pwd"

echo "Parallel step complete"

}
}
}

David Karr

unread,
Jan 4, 2017, 3:52:49 PM1/4/17
to jenkins...@googlegroups.com
In short, you can't execute "pipeline steps", and "sh" is a pipeline
step, from a @NonCPS method.
> --
> 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/ea0445c6-d1f3-4dea-bab3-4f997e1c147c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jørgen Sølvernes Sandnes

unread,
Jan 5, 2017, 3:29:21 AM1/5/17
to Jenkins Users
Thanks! Would it be possible to improve the framework so that an exception would be thrown when attempting to do pipeline steps from a @NonCPS method? (I assume other people than me have spent hours and hours of debugging before realizing what is wrong)

Ann B

unread,
Oct 23, 2017, 8:43:51 PM10/23/17
to Jenkins Users
Very helpful Jorgen!  I have also spent hours on this issue. 

Ann
Reply all
Reply to author
Forward
0 new messages