You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2019/12/07 23:15:10 UTC

[logging-log4j2] branch release-2.x updated: Changelog and test coverage for LOG4J2-2725

This is an automated email from the ASF dual-hosted git repository.

ckozak pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 26a7e3b  Changelog and test coverage for LOG4J2-2725
26a7e3b is described below

commit 26a7e3b0f9287e621f1251c0c567caa425c1d3b2
Author: Carter Kozak <c4...@gmail.com>
AuthorDate: Sat Dec 7 18:07:14 2019 -0500

    Changelog and test coverage for LOG4J2-2725
---
 .../log4j/core/GarbageCollectionHelper.java        |   2 +-
 .../async/AsyncLoggerTestArgumentFreedOnError.java | 109 +++++++++++++++++++++
 src/changes/changes.xml                            |   3 +
 3 files changed, 113 insertions(+), 1 deletion(-)

diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/GarbageCollectionHelper.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/GarbageCollectionHelper.java
index 0e0c9ad..ceb652e 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/GarbageCollectionHelper.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/GarbageCollectionHelper.java
@@ -27,7 +27,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 
 import static org.junit.Assert.assertTrue;
 
-final class GarbageCollectionHelper implements Closeable, Runnable {
+public final class GarbageCollectionHelper implements Closeable, Runnable {
     private static final OutputStream sink = ByteStreams.nullOutputStream();
     public final AtomicBoolean running = new AtomicBoolean();
     private final CountDownLatch latch = new CountDownLatch(1);
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestArgumentFreedOnError.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestArgumentFreedOnError.java
new file mode 100644
index 0000000..5f9f4b5
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestArgumentFreedOnError.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.async;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.categories.AsyncLoggers;
+import org.apache.logging.log4j.core.GarbageCollectionHelper;
+import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.util.StringBuilderFormattable;
+import org.apache.logging.log4j.util.Strings;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertTrue;
+
+@Category(AsyncLoggers.class)
+public class AsyncLoggerTestArgumentFreedOnError {
+
+    @BeforeClass
+    public static void beforeClass() {
+        System.setProperty("log4j2.enable.threadlocals", "true");
+        System.setProperty("log4j2.enable.direct.encoders", "true");
+        System.setProperty("log4j2.is.webapp", "false");
+        System.setProperty("log4j.format.msg.async", "true");
+        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR,
+                AsyncLoggerContextSelector.class.getName());
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, Strings.EMPTY);
+    }
+
+    // LOG4J2-2725: events are cleared even after failure
+    @Test
+    public void testMessageIsGarbageCollected() throws Exception {
+        final AsyncLogger log = (AsyncLogger) LogManager.getLogger("com.foo.Bar");
+        CountDownLatch garbageCollectionLatch = new CountDownLatch(1);
+        log.fatal(new ThrowingMessage(garbageCollectionLatch));
+        GarbageCollectionHelper gcHelper = new GarbageCollectionHelper();
+        gcHelper.run();
+        try {
+            assertTrue("Parameter should have been garbage collected",
+                    garbageCollectionLatch.await(30, TimeUnit.SECONDS));
+        } finally {
+            gcHelper.close();
+        }
+    }
+
+    private static class ThrowingMessage implements Message, StringBuilderFormattable {
+
+        private final CountDownLatch latch;
+
+        ThrowingMessage(CountDownLatch latch) {
+            this.latch = latch;
+        }
+
+        @Override
+        protected void finalize() throws Throwable {
+            latch.countDown();
+            super.finalize();
+        }
+
+        @Override
+        public String getFormattedMessage() {
+            throw new Error("Expected");
+        }
+
+        @Override
+        public String getFormat() {
+            return "";
+        }
+
+        @Override
+        public Object[] getParameters() {
+            return new Object[0];
+        }
+
+        @Override
+        public Throwable getThrowable() {
+            return null;
+        }
+
+        @Override
+        public void formatTo(StringBuilder buffer) {
+            throw new Error("Expected");
+        }
+    }
+}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9ed6ca2..ea001f2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -88,6 +88,9 @@
       <action issue="LOG4J2-2673" dev="ggregory" type="fix" due-to="Yuichi Sugimura">
         OutputStreamAppender.Builder ignores setFilter().
       </action>
+      <action issue="LOG4J2-2725" dev="ckozak" type="fix" due-to="Dzmitry Anikechanka">
+        Prevent a memory leak when async loggers throw errors.
+      </action>
     </release>
     <release version="2.12.1" date="2019-08-06" description="GA Release 2.12.1">
       <action issue="LOG4J2-1946" dev="rgoers" type="fix" due-to="Igor Perelyotov">