diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/TimeoutStepExecution.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/TimeoutStepExecution.java index 677dc541..994d5d22 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/TimeoutStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/TimeoutStepExecution.java @@ -17,9 +17,12 @@ import java.io.Serializable; import java.lang.ref.Reference; import java.lang.ref.WeakReference; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.List; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -188,7 +191,7 @@ private void cancel() { LOGGER.log(Level.WARNING, null, x); } } - }, MoreExecutors.sameThreadExecutor()); + }, newExecutorService()); } } else { listener().getLogger().println("Cancelling nested steps due to timeout"); @@ -199,6 +202,30 @@ private void cancel() { } } + /** + * Returns an {@link ExecutorService} to be used as a parameter in other methods. It calls + * {@code MoreExecutors#newDirectExecutorService} or falls back to {@code + * MoreExecutors#sameThreadExecutor} for compatibility with older (< 18.0) versions of Guava. + * + * @since TODO + */ + private static ExecutorService newExecutorService() { + try { + try { + // Guava older than 18 + Method method = MoreExecutors.class.getMethod("sameThreadExecutor"); + return (ExecutorService) method.invoke(null); + } catch (NoSuchMethodException e) { + // TODO Invert this to prefer the newer Guava method once Guava is upgraded in + // Jenkins core. + Method method = MoreExecutors.class.getMethod("newDirectExecutorService"); + return (ExecutorService) method.invoke(null); + } + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + @Override public String getStatus() { if (killer == null) { return "killer task nowhere to be found";