Browse Source

Fix X-Pack SchedulerEngine Shutdown (#48951)

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
Armin Braun 6 years ago
parent
commit
b8d85cf6a8

+ 9 - 1
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java

@@ -25,6 +25,7 @@ import java.util.Objects;
 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 @@ public class SchedulerEngine {
             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;
+                    }
+                }
             }
         }