Log uncaught exceptions from scheduled once tasks

`ScheduledThreadPoolExecutor` allows you to schedule tasks to run once or periodically at the future. If such a task throws an exception, that exception is caught and reported in the future that `ScheduledThreadPoolExecutor#schedule` returns. However, we typically do not capture the future / do not test it for errors. This results in exception being swallowed and not reported. To mitigate this we now wrap any command in a LoggingRunnable  (already used for periodic tasks).  Also, RunnableCommand is changed not to swallow exception but percolate them further for reporting by the future.

Closes #15824
This commit is contained in:
Boaz Leskes 2016-01-07 13:47:02 +01:00
parent e7f9d685f1
commit d5e6eb58a8
1 changed files with 2 additions and 1 deletions

View File

@ -357,7 +357,7 @@ public class ThreadPool extends AbstractComponent {
if (!Names.SAME.equals(name)) { if (!Names.SAME.equals(name)) {
command = new ThreadedRunnable(command, executor(name)); command = new ThreadedRunnable(command, executor(name));
} }
return scheduler.schedule(command, delay.millis(), TimeUnit.MILLISECONDS); return scheduler.schedule(new LoggingRunnable(command), delay.millis(), TimeUnit.MILLISECONDS);
} }
public void shutdown() { public void shutdown() {
@ -633,6 +633,7 @@ public class ThreadPool extends AbstractComponent {
runnable.run(); runnable.run();
} catch (Throwable t) { } catch (Throwable t) {
logger.warn("failed to run {}", t, runnable.toString()); logger.warn("failed to run {}", t, runnable.toString());
throw t;
} }
} }