Skip to content

Commit b555381

Browse files
committed
[JENKINS-68849] Restore EnvActionImpl.metaClass during deserialization to prevent NPEs in Declarative when conditions in Pipelines resumed after a Jenkins restart
1 parent 8fa6245 commit b555381

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/main/java/org/jenkinsci/plugins/workflow/cps/EnvActionImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ private FlowNode getNode() throws IOException {
155155
owner = r;
156156
}
157157

158+
private Object readResolve() {
159+
// We need to restore the transient MetaClass field when this class is deserialized by XStream to prevent NPEs in Groovy code that calls methods on this class.
160+
setMetaClass(null);
161+
return this;
162+
}
163+
158164
/**
159165
* Gets the singleton instance for a given build, creating it on demand.
160166
*/

src/test/java/org/jenkinsci/plugins/workflow/cps/CpsFlowExecutionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,15 @@ public boolean isAllowed(String groovyResourceUrl) {
530530
}
531531
}
532532

533-
@Issue("JENKINS-45327")
533+
@Issue({ "JENKINS-45327", "JENKINS-68849" })
534534
@Test public void envActionImplPickle() throws Throwable {
535535
sessions.then(r -> {
536536
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
537537
p.setDefinition(new CpsFlowDefinition(
538538
"def e = env\n" +
539539
"semaphore('wait')\n" + // An instance of EnvActionImpl is part of the program's state at this point.
540-
"e.foo = 'bar'\n", true)); // Without EnvActionImplPickle, this throws an NPE in EnvActionImpl.setProperty because owner is null.
540+
"e.foo = 'bar'\n" + // Without EnvActionImplPickle, this throws an NPE in EnvActionImpl.setProperty because owner is null.
541+
"env.getProperty('foo')\n", true)); // Without EnvActionImpl.readResolve, this throws an NPE in PogoMetaClassSite.call
541542
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
542543
SemaphoreStep.waitForStart("wait/1", b);
543544
});

0 commit comments

Comments
 (0)