This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit a224cbdb603c29a63c14a3f8b921b26870222f86 Author: Mark Thomas <ma...@apache.org> AuthorDate: Thu Nov 25 14:04:55 2021 +0000 Alternative fix for PR #453 - lost log messages on shutdown --- java/org/apache/juli/AsyncFileHandler.java | 40 ++++++++++++++++++++++++++++++ webapps/docs/changelog.xml | 4 +++ 2 files changed, 44 insertions(+) diff --git a/java/org/apache/juli/AsyncFileHandler.java b/java/org/apache/juli/AsyncFileHandler.java index e947456..e1ac89b 100644 --- a/java/org/apache/juli/AsyncFileHandler.java +++ b/java/org/apache/juli/AsyncFileHandler.java @@ -18,6 +18,7 @@ package org.apache.juli; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.LogRecord; /** * A {@link FileHandler} implementation that uses a queue of log entries. @@ -95,6 +96,7 @@ public class AsyncFileHandler extends FileHandler { } closed = true; } + LoggerThread.deregisterHandler(); super.close(); } @@ -109,6 +111,7 @@ public class AsyncFileHandler extends FileHandler { } closed = false; } + LoggerThread.registerHandler(); super.open(); } @@ -158,6 +161,43 @@ public class AsyncFileHandler extends FileHandler { } protected static class LoggerThread extends Thread { + + /* + * Implementation note: Use of this count could be extended to + * start/stop the LoggerThread but that would require careful locking as + * the current size of the queue also needs to be taken into account and + * there are lost of edge cases when rapidly starting and stopping + * handlers. + */ + private static final AtomicInteger handlerCount = new AtomicInteger(); + + public static void registerHandler() { + handlerCount.incrementAndGet(); + } + + public static void deregisterHandler() { + int newCount = handlerCount.decrementAndGet(); + if (newCount == 0) { + try { + Thread dummyHook = new Thread(); + Runtime.getRuntime().addShutdownHook(dummyHook); + Runtime.getRuntime().removeShutdownHook(dummyHook); + } catch (IllegalStateException ise) { + // JVM is shutting down. + // Allow up to 10s for for the queue to be emptied + int sleepCount = 0; + while (!AsyncFileHandler.queue.isEmpty() && sleepCount < 10000) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + // Ignore + } + sleepCount++; + } + } + } + } + public LoggerThread() { this.setDaemon(true); this.setName("AsyncFileHandlerWriter-" + System.identityHashCode(this)); diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index c163c15..cc1ff07 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -119,6 +119,10 @@ Document conditions under which the <code>AprLifecycleListener</code> can be used to avoid JVM crashes. (michaelo) </docs> + <fix> + Refactor the <code>AsyncFileHandler</code> to reduce the possibility of + log messages being lost on shutdown. (markt) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org