Catch LinkageError in ThrowableExtendedStackTraceRenderer#loadClass (#4028)#4049
Catch LinkageError in ThrowableExtendedStackTraceRenderer#loadClass (#4028)#4049chanani wants to merge 2 commits into
LinkageError in ThrowableExtendedStackTraceRenderer#loadClass (#4028)#4049Conversation
…s` (apache#4028) Signed-off-by: CHANHAN <130114269+chanani@users.noreply.github.com>
|
Hi @vy, thanks for the encouragement! Here's what this PR includes:
Let me know if any changes are needed! |
| https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||
| type="fixed"> | ||
| <issue id="4028" link="https://github.com/apache/logging-log4j2/issues/4028"/> | ||
| <description format="asciidoc">Catch `LinkageError` in `ThrowableExtendedStackTraceRenderer#loadClass` to prevent `NoClassDefFoundError` from breaking logging</description> |
There was a problem hiding this comment.
| <description format="asciidoc">Catch `LinkageError` in `ThrowableExtendedStackTraceRenderer#loadClass` to prevent `NoClassDefFoundError` from breaking logging</description> | |
| <description format="asciidoc">Ensure all `Throwable`s are handled while rendering stack traces</description> |
…che#4028) - Changed catch (Exception) to catch (Throwable) in loadClass method - Moved tests into ThrowablePatternConverterTest.StackTraceTest - Removed standalone ThrowableExtendedStackTraceRendererTest - Updated changelog description and issue link per review feedback Signed-off-by: CHANHAN <130114269+chanani@users.noreply.github.com>
|
Hi @vy, thank you for the review! I've addressed all the feedback.
Please let me know if any further changes are needed! |
|
@chanani, |
|
Hi @vy, The reason
So the existing test with Please let me know if any further changes are needed ! |
| */ | ||
| @Test | ||
| @Issue("https://github.com/apache/logging-log4j2/issues/4028") | ||
| void rendering_should_succeed_with_nonexistent_class_in_stack_trace() { |
There was a problem hiding this comment.
I've put some more thought into this issue, and this particular test feels like an after-thought. Would you mind removing this particular test method, extending TestFriendlyException with this patch to contain a stack trace element of a non-existent class, and adapt ThrowablePatternConverterTest and its subclasses to make sure they pass, please?
| return clazz; | ||
| } | ||
| } catch (final Exception ignored) { | ||
| } catch (final Throwable ignored) { |
There was a problem hiding this comment.
Catching Throwable might be excessive, let's limit ourselves to LinkageError.
| } catch (final Throwable ignored) { | |
| } catch (final Exception | LinkageError ignored) { |
There was a problem hiding this comment.
Sorry, I just realized this was @vy's suggestion (#4049 (comment)), so this merits a longer discussion.
I think that catching Throwable should always be used sparingly. We don't want to catch critical exception, such as anOutOfMemoryError, especially since we ignore the exception.
There was a problem hiding this comment.
Catching
Throwablemight be excessive
This is precisely what we do in util.Loader, and I'm inclined to keep similar functionalities aligned. If you think they need to change, maybe we should carry that out in a separate PR focusing on that change.
Summary
The
loadClassmethod inThrowableExtendedStackTraceRendereronly catchesException,but
ClassLoadercan throwNoClassDefFoundError, which extendsLinkageError(anErrorsubclass). This causes the entire log event to fail with
AppenderLoggingExceptioninsteadof gracefully degrading when a class in the stack trace cannot be resolved.
Changes
ThrowableExtendedStackTraceRenderer.java: Changedcatch (Exception)tocatch (Exception | LinkageError)in theloadClassmethodThrowableExtendedStackTraceRendererTest.java: Added two test cases:ClassLoaderthrowsNoClassDefFoundError4028_fix_catch_LinkageError_in_ThrowableExtendedStackTraceRenderer.xmlRoot Cause
Java's
Throwablehierarchy:Fixes #4028