From d5758ff6a56e8c6b836fd75ecdbab15294148f2f Mon Sep 17 00:00:00 2001 From: kaido207 Date: Thu, 21 Nov 2024 16:51:59 +0900 Subject: [PATCH] Fix the number of Logging pump threads grows infinitely Signed-off-by: kaido207 --- .../main/jul/handler/GlassFishLogHandler.java | 11 +++++++++++ .../main/jul/handler/LoggingOutputStream.java | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/handler/GlassFishLogHandler.java b/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/handler/GlassFishLogHandler.java index 19d768d7523..b948801fafd 100644 --- a/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/handler/GlassFishLogHandler.java +++ b/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/handler/GlassFishLogHandler.java @@ -397,10 +397,21 @@ private void drainLogRecords() { private void initStandardStreamsLogging() { trace(GlassFishLogHandler.class, "initStandardStreamsLogging()"); + + LoggingPrintStream prevStdoutStream = this.stdoutStream; + LoggingPrintStream prevStderrStream = this.stderrStream; + this.stdoutStream = LoggingPrintStream.create(STDOUT_LOGGER, INFO, 5000, configuration.getEncoding()); this.stderrStream = LoggingPrintStream.create(STDERR_LOGGER, SEVERE, 1000, configuration.getEncoding()); System.setOut(this.stdoutStream); System.setErr(this.stderrStream); + + if (prevStdoutStream != null) { + prevStdoutStream.close(); + } + if (prevStderrStream != null) { + prevStderrStream.close(); + } } diff --git a/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/handler/LoggingOutputStream.java b/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/handler/LoggingOutputStream.java index 8a28ab22c31..8b8360e6f29 100644 --- a/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/handler/LoggingOutputStream.java +++ b/nucleus/glassfish-jul-extension/src/main/java/org/glassfish/main/jul/handler/LoggingOutputStream.java @@ -137,6 +137,7 @@ private static final class Pump extends Thread { private final LogRecordBuffer buffer; private final Logger logger; + private volatile boolean pumpClosed; private Pump(final Logger logger, final LogRecordBuffer buffer) { this.buffer = buffer; @@ -151,7 +152,7 @@ private Pump(final Logger logger, final LogRecordBuffer buffer) { @Override public void run() { // the thread will be interrupted by it's owner finally - while (true) { + while (!pumpClosed) { try { logAllPendingRecordsOrWait(); } catch (final Exception e) { @@ -169,6 +170,7 @@ public void run() { * The pump can be locked, waiting */ void shutdown() { + pumpClosed = true; this.interrupt(); // we interrupted waiting or working thread, now we have to process remaining records. logAllPendingRecords();