From 73950efc8828897c827a99e627789dc92a93ca41 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 11 Nov 2019 17:07:23 +0100 Subject: [PATCH] Fix X-Pack SchedulerEngine Shutdown We can have a race here where `scheduleNextRun` executes concurrently to `stop` and so we run into a `RejectedExecutionException` that we don't catch and thus it fails tests. => Fixed by ignoring these so long as they coincide with a scheduler shutdown --- .../xpack/core/scheduler/SchedulerEngine.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java index 0aee254a81cfa..6c4ccdbd43ee5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java @@ -25,6 +25,7 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -226,7 +227,14 @@ private void scheduleNextRun(long currentTime) { this.scheduledTime = schedule.nextScheduledTimeAfter(startTime, currentTime); if (scheduledTime != -1) { long delay = Math.max(0, scheduledTime - currentTime); - future = scheduler.schedule(this, delay, TimeUnit.MILLISECONDS); + try { + future = scheduler.schedule(this, delay, TimeUnit.MILLISECONDS); + } catch (RejectedExecutionException e) { + // ignoring rejections if the scheduler has been shut down already + if (scheduler.isShutdown() == false) { + throw e; + } + } } }