Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StackOverflowException in Logger.isEnabled with Spring Boot 3 + Tomcat #1430

Closed
ppkarwasz opened this issue Apr 13, 2023 · 2 comments
Closed
Labels
bug Incorrect, unexpected, or unintended behavior of existing code
Milestone

Comments

@ppkarwasz
Copy link
Contributor

Description

Tomcat calls Logger.isEnabled at the beginning of its ClassLoader#loadClass implementation.

If the method requires to load additional classes (e.g. org.apache.logging.log4j.core.Filter.Result) an infinite recursion occurs.

Reproduction

  • configure Tomcat to use Log4j 2.x as server's logging framework,
  • add a Spring Boot 3 application without Log4j2 jars,
  • a stack overflow occurs.

Details

This issue was first reported in this StackOverflow question.

The problem occurs because:

  1. If no logger context exists for a given classloader and configLocation == null the ClassLoaderContextSelector returns the logger context for the parent classloader. This is what happens in most LogManager.getContext calls;
  2. Spring Boot adds a class from the webapp classloader as global filter to the logger context's configuration;
  3. Tomcat calls Logger.isEnabled at the beginning of ClassLoader#loadClass hence it triggers the loading of all classes used by the global filter. This process uses the webapp classloader, hence the recursion.
@rgoers
Copy link
Member

rgoers commented Apr 14, 2023

I think this problem is more general than you are making it out to be. Anytime there is a sequence of
app-> jul -> log4j -> 3rd party -> jul
there will be an infinite loop. We need to prevent that by detecting recursion in ApiLogger.

In this case, isLoggable should detect the recursion and return false.

@ppkarwasz
Copy link
Contributor Author

I am closing this, since it was fixed in Spring Boot 3.1.11 and 3.2.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect, unexpected, or unintended behavior of existing code
Projects
None yet
Development

No branches or pull requests

2 participants