IllegalStateException When Calling sendError() After Response Commit and StackOverflowError During JSON Conversion
krkarma777 opened this issue · comments
Problem Description
In our Spring Boot application, we're encountering an IllegalStateException
when attempting to call sendError()
after the HTTP response has already been committed. Additionally, during the processing of a request, a HttpMessageNotWritableException
triggers a StackOverflowError
due to infinite recursion during JSON conversion.
Error Log
2024-03-09T23:09:33.425+09:00 WARN 13960 --- [nio-9090-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.checkCommitted(ResponseFacade.java:485) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:337) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
at jakarta.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:97) ~[tomcat-embed-core-10.1.18.jar:6.0]
at jakarta.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:97) ~[tomcat-embed-core-10.1.18.jar:6.0]
2024-03-09T23:09:33.434+09:00 ERROR 13960 --- [nio-9090-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError)] with root cause
java.lang.StackOverflowError: null
Reproduction Steps
The issue appears to manifest when handling specific requests that lead to an infinite recursion due to cyclic references during JSON conversion. Also, attempting to call sendError()
after the response has already been committed results in an IllegalStateException
.
Suspected Causes
StackOverflowError
: Arises from cyclic references among objects being converted to JSON, leading to infinite recursion.IllegalStateException
: Results from attempting to callsendError()
after the response has already been committed due to incorrect handling flow.