dmvk commented on a change in pull request #10175: [FLINK-14746][web] Handle uncaught exceptions in HistoryServerArchive… URL: https://github.com/apache/flink/pull/10175#discussion_r346160519
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/util/ScheduledFutures.java ########## @@ -0,0 +1,58 @@ +package org.apache.flink.runtime.util; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ThreadFactory; +import org.apache.flink.shaded.guava18.com.google.common.util.concurrent.ThreadFactoryBuilder; + +/** + * Utils related to {@link ScheduledFuture scheduled futures}. + */ +public class ScheduledFutures { + + private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder() + .setNameFormat("Flink-Scheduled-Future-SafeGuard") + .setDaemon(true) + .build(); + + /** + * Guard {@link ScheduledFuture} with scheduled future uncaughtException handler, because + * {@link java.util.concurrent.ScheduledExecutorService} does not respect the one assigned to + * executing {@link Thread} instance. + * + * @param scheduledFuture Scheduled future to guard. + * @param uncaughtExceptionHandler Handler to call in case of uncaught exception. + * @param <T> Type the future returns. + * @return Future with handler. + */ + public static <T> ScheduledFuture<T> withUncaughtExceptionHandler( + ScheduledFuture<T> scheduledFuture, + Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { + final Thread safeguardThread = THREAD_FACTORY.newThread(() -> { Review comment: We need a separate thread to `get()` the result of `ScheduledFuture`, which is blocking op. Wrapping the scheduled `Runnable` (in the one that try/catches throwable and calls UEH) instead of `ScheduledFuture` should get us the same result. I'm OK with either approach. Which one do you prefer? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services