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

OutOfMemoryError caused by nested UnsatisfiedDependencyExceptions [SPR-14607] #19175

Closed
spring-projects-issues opened this issue Aug 19, 2016 · 1 comment
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 19, 2016

Stephan Neumann opened SPR-14607 and commented

The new error logging format of UnsatisfiedDependencyInjection introduced in Spring 4.3 can cause a OutOfMemoryError. When the constructor

public UnsatisfiedDependencyException(String resourceDescription, String beanName, String propertyName, BeansException ex) {
  this(resourceDescription, beanName, propertyName, (ex != null ? ex.getMessage() : ""));
  initCause(ex);
}

is used (for example when a field with an @Inject annotation can't be resolved), the exception message of the causing exception is included twice in the current exception message, once by UnsatisfiedDependencyException, once by NestedRuntimeException which is a superclass of UnsatisfiedDependencyException. The exception message format looks like this:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '<NAME>': Unsatisfied dependency expressed through field '<FIELD>': <NESTED EXCEPTION MESSAGE>; nested exception is <NESTED EXCEPTION MESSAGE>

This makes it more than double the size of the message of the causing exception and causes exponential growth of memory consumption. If the nesting level is high enough the application will crash with an OutOfMemoryError instead of logging the root cause of the context creation failure, which is hardly the goal of the new logging format.

I suggest to change the constructor definition to:

public UnsatisfiedDependencyException(String resourceDescription, String beanName, String propertyName, BeansException ex) {
  this(resourceDescription, beanName, propertyName, "");
  initCause(ex);
}

This makes the causing exception message only be included once as nested exception text.

Example project to reproduce error & logoutput is attached.


Affects: 4.3 GA

Attachments:

Issue Links:

  • #8389 Condense exception messages accumulated through recursion

1 votes, 3 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2016

Juergen Hoeller commented

Good point, that duplication of the nested exception message is a waste in any case. I've removed it for 4.3.3, also fine-tuning our exception message building to avoid an empty ": " clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants