From 5ed683538d0378dffd70e573f7e7e3bf1e0323f2 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 11 Jul 2017 16:05:07 -0400 Subject: [PATCH] Name the log tail thread and mark it as a daemon This commit provides an explicit name for the log tail thread, otherwise the thread shows up with a generic name like Thread-12 in stack dumps. While the stack trace provides enough information to conclude what this thread is doing, it is better to be more explicit about the purpose of this thread so that that can be discerned directly from the name of the thread. Additionally, we mark this thread as a daemon thread. Since this thread is created by the main thread during node initialization it defaults to being a user thread. Since the JVM only exits when the only threads running are not user threads, if this thread were to somehow block then it could prevent the server JVM from exiting. As such, this thread should be marked as a daemon thread. Relates elastic/x-pack-elasticsearch#1969 Original commit: elastic/x-pack-elasticsearch@b3761c7d91e446f034f32b28bb09cefab82eb1d4 --- .../ml/job/process/NativeController.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/NativeController.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/NativeController.java index 508085162cf..63af80df892 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/NativeController.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/NativeController.java @@ -50,7 +50,6 @@ public class NativeController { private final CppLogMessageHandler cppLogHandler; private final OutputStream commandStream; - private Thread logTailThread; NativeController(Environment env, NamedPipeHelper namedPipeHelper) throws IOException { ProcessPipes processPipes = new ProcessPipes(env, namedPipeHelper, ProcessCtrl.CONTROLLER, null, @@ -61,15 +60,22 @@ public class NativeController { } void tailLogsInThread() { - logTailThread = new Thread(() -> { - try { - cppLogHandler.tailStream(); - cppLogHandler.close(); - } catch (IOException e) { - LOGGER.error("Error tailing C++ controller logs", e); - } - LOGGER.info("Native controller process has stopped - no new native processes can be started"); - }); + final Thread logTailThread = new Thread( + () -> { + try { + cppLogHandler.tailStream(); + cppLogHandler.close(); + } catch (IOException e) { + LOGGER.error("Error tailing C++ controller logs", e); + } + LOGGER.info("Native controller process has stopped - no new native processes can be started"); + }, + "ml-cpp-log-tail-thread"); + /* + * This thread is created on the main thread so would default to being a user thread which could prevent the JVM from exiting if + * this thread were to still be running during shutdown. As such, we mark it as a daemon thread. + */ + logTailThread.setDaemon(true); logTailThread.start(); }