Fix: prevent ThrowableStackTraceRenderer from throwing NPEs#3934
Merged
vy merged 5 commits intoapache:2.xfrom Oct 3, 2025
Merged
Fix: prevent ThrowableStackTraceRenderer from throwing NPEs#3934vy merged 5 commits intoapache:2.xfrom
vy merged 5 commits intoapache:2.xfrom
Conversation
The `ThrowableStackTraceRenderer` class can throw a `NullPointerException` if the suppressed exceptions associated with the `Throwable` it is rendering are being concurrently mutated. This happens because `ThrowableStackTraceRenderer` invokes `Throwable#getSuppressed()` twice: once in `ThrowableStackTraceRenderer.Context.Metadata#populateMetadata()`, and a second time in `ThrowableStackTraceRenderer#renderThrowable()`, ahead of invoking `ThrowableStackTraceRenderer#renderSuppressed()`. If a racing thread manages to add a new suppressed exception to the being-logged exception between these two invocations, then the `Map<Throwable, Context.Metadata>` constructed by `populateMetadata()` will contain no mapping for the newly-added suppression, and as a result the dereference performed on line 168 explodes. Note: the unit test I am adding here requires the ability to mock `Throwable#getSuppressed()`. Since this method is `final`, I had to add a dependency on `mockito-inline`, which then required that I fix up some unrelated unit tests that had been relying on the lack of support for mocking `final` methods. This fixes apache#3929
vy
requested changes
Sep 26, 2025
Member
vy
left a comment
There was a problem hiding this comment.
Thanks so much for the contribution! Would you mind also extending following tests to reproduce the issue, please?
ExtendedThrowablePatternConverterTestRootThrowablePatternConverterTestThrowablePatternConverterTest
...st/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseAppenderTest.java
Show resolved
Hide resolved
...est/src/test/java/org/apache/logging/log4j/core/appender/db/AbstractDatabaseManagerTest.java
Show resolved
Hide resolved
.../java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java
Show resolved
Hide resolved
log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/ThrowableStackTraceRenderer.java
Outdated
Show resolved
Hide resolved
See conversation: apache#3934
Contributor
Author
Instead of modifying these tests, I added a new unit test specifically for |
vy
requested changes
Oct 3, 2025
Member
vy
left a comment
There was a problem hiding this comment.
Would you mind removing ThrowableStackTraceRendererTest, the newly mockito-inline dependency, and changes necessitated due to that, please?
vy
approved these changes
Oct 3, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
ThrowableStackTraceRendererclass can throw aNullPointerExceptionif the suppressed exceptions associated with theThrowableit is rendering are being concurrently mutated. This happens becauseThrowableStackTraceRendererinvokesThrowable#getSuppressed()twice: once inThrowableStackTraceRenderer.Context.Metadata#populateMetadata(), and a second time inThrowableStackTraceRenderer#renderThrowable(), ahead of invokingThrowableStackTraceRenderer#renderSuppressed(). If a racing thread manages to add a new suppressed exception to the being-logged exception between these two invocations, then theMap<Throwable, Context.Metadata>constructed bypopulateMetadata()will contain no mapping for the newly-added suppression, and as a result the dereference performed on line 168 explodes.Note: the unit test I am adding here requires the ability to mock
Throwable#getSuppressed(). Since this method isfinal, I had to add a dependency onmockito-inline, which then required that I fix up some unrelated unit tests that had been relying on the lack of support for mockingfinalmethods.This fixes #3929