From 678d760a0c402bfa7f1fcc70ab897dffa6110e7a Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Mon, 21 Sep 2020 15:36:01 -0400 Subject: [PATCH] try our best to do verbose output, but in a termination situation we may get a NoClassDefFoundError, if so, log it --- .../org/cobbzilla/util/daemon/ZillaRuntime.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cobbzilla/util/daemon/ZillaRuntime.java b/src/main/java/org/cobbzilla/util/daemon/ZillaRuntime.java index ca21645..cb969b2 100644 --- a/src/main/java/org/cobbzilla/util/daemon/ZillaRuntime.java +++ b/src/main/java/org/cobbzilla/util/daemon/ZillaRuntime.java @@ -79,7 +79,7 @@ public class ZillaRuntime { public static TerminationRequestResult terminate(Thread thread, long timeout, Function onlyIf, boolean verbose) { if (thread == null || !thread.isAlive()) return TerminationRequestResult.dead; if (onlyIf != null && !onlyIf.apply(thread)) { - if (log.isWarnEnabled()) log.warn("terminate: thread is alive but onlyIf function returned false, not interrupting: " + thread + (verbose ? " with stack " + stacktrace(thread) + "\nfrom: " + stacktrace() : "")); + if (log.isWarnEnabled()) log.warn("terminate: thread is alive but onlyIf function returned false, not interrupting: " + thread + terminateVerbose(thread, verbose)); return TerminationRequestResult.alive; } thread.interrupt(); @@ -89,19 +89,27 @@ public class ZillaRuntime { } if (thread.isAlive()) { if (onlyIf != null && onlyIf.apply(thread)) { - if (log.isWarnEnabled()) log.warn("terminate: thread did not respond to interrupt, killing: " + thread + (verbose ? " with stack " + stacktrace(thread) + "\nfrom: " + stacktrace() : "")); + if (log.isWarnEnabled()) log.warn("terminate: thread did not respond to interrupt, killing: " + thread + terminateVerbose(thread, verbose)); thread.stop(); return TerminationRequestResult.terminated; } else { - if (log.isWarnEnabled()) log.warn("terminate: thread did not respond to interrupt, but onlyIf function returned false, not killing: " + thread + (verbose ? " with stack " + stacktrace(thread) + "\nfrom: " + stacktrace() : "")); + if (log.isWarnEnabled()) log.warn("terminate: thread did not respond to interrupt, but onlyIf function returned false, not killing: " + thread + terminateVerbose(thread, verbose)); return TerminationRequestResult.interrupted_alive; } } else { - if (log.isWarnEnabled()) log.warn("terminate: thread exited after interrupt: "+thread+(thread.getName().startsWith("Thread-") ? " with stack: "+stacktrace(thread) : "")); + if (log.isWarnEnabled()) log.warn("terminate: thread exited after interrupt: "+thread + terminateVerbose(thread, verbose)); return TerminationRequestResult.interrupted_dead; } } + public static String terminateVerbose(Thread thread, boolean verbose) { + try { + return verbose || thread.getName().startsWith("Thread-") ? " with stack " + stacktrace(thread) + "\nfrom: " + stacktrace() : ""; + } catch (NoClassDefFoundError e) { + return "(verbose output error: NoClassDefFoundError: "+e.getMessage()+")"; + } + } + public static String threadName() { return Thread.currentThread().getName(); } public static boolean bool(Boolean b) { return b != null && b; }