From 7d8c0082a3f64f77b1853c2e70c7b6bccb1f686a Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Fri, 25 Sep 2020 13:19:59 -0400 Subject: [PATCH] [JENKINS-41037] Prevent ConcurrentModificationException serializing LibrariesAction.libraries --- .../org/jenkinsci/plugins/workflow/libs/LibrariesAction.java | 3 ++- .../org/jenkinsci/plugins/workflow/libs/LibraryStep.java | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/workflow/libs/LibrariesAction.java b/src/main/java/org/jenkinsci/plugins/workflow/libs/LibrariesAction.java index 367e406a..86b029d9 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/libs/LibrariesAction.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/libs/LibrariesAction.java @@ -31,6 +31,7 @@ import hudson.model.Run; import hudson.model.TaskListener; import java.io.IOException; +import java.util.Collections; import java.util.List; /** @@ -48,7 +49,7 @@ class LibrariesAction extends InvisibleAction { * A list of libraries in use. */ public List getLibraries() { - return libraries; + return Collections.unmodifiableList(libraries); } @Extension public static class LibraryEnvironment extends EnvironmentContributor { diff --git a/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java b/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java index cad49424..9984d5b5 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java @@ -48,6 +48,7 @@ import java.net.URL; import java.nio.file.Paths; import java.security.CodeSource; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -197,7 +198,9 @@ public static class Execution extends AbstractSynchronousNonBlockingStepExecutio return new LoadedClasses(name, trusted, changelog, run); } } - libraries.add(record); + List newLibraries = new ArrayList<>(libraries); + newLibraries.add(record); + run.replaceAction(new LibrariesAction(newLibraries)); } listener.getLogger().println("Loading library " + record.name + "@" + record.version); CpsFlowExecution exec = (CpsFlowExecution) getContext().get(FlowExecution.class);