ERROR 2798 --- [nio-5000-exec-5] c.j.j.c.e.h.GlobalExceptionHandler : RuntimeException :
ResponseBodyEmitter has already completed with error: org.springframework.web.context.request.async.AsyncRequestNotUsableException: Response not usable after response errors.
sse 개발 중, api는 모두 만들어져 있고 로컬 테스트는 정상적으로 작동한 후였다.
클라이언트와 연결하고 프론트 개발 중 일어난 일인데,
잘 작동되어야 했던 좋아요, 댓글 추가와 같은 api 호출 및 알림이 발생할 때 아래와 같은 에러가 터졌다.
{
"success": false,
"message": "내부 서버 오류가 발생하였습니다.",
"result": "ResponseBodyEmitter has already completed with error: org.springframework.web.context.request.async.AsyncRequestNotUsableException: ServletOutputStream failed to flush: java.io.IOException: Broken pipe"
}
진짜 오랫동안 서칭만 했지만 마땅한 해결책은 나오지 않았다.
Nginx 설정을 아래와 같이 변경하는 것
proxy_set_header Connection '';
proxy_http_version 1.1;
그리고 마찬가지로 Nginx 설정에 아래처럼 추가하는 것
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
keepalive_timeout 3600s;
그리고 구독 및 연결 api에
HttpServletResponse response
를 추가한 후, 서비스 로직에 아래 코드를 추가하는 것
private void configureSse(HttpServletResponse response) {
response.setHeader("Content-Type", "text/event-stream");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
response.setHeader("Access-Control-Allow-Origin", "*");
}
크게는 이렇게 수정해봤지만 오히려 다른 에러가 나타났었다.
그 에러는 다음과 같다.
ERROR 2798 --- [nio-5000-exec-5] c.j.j.c.e.h.GlobalExceptionHandler : RuntimeException :
ResponseBodyEmitter has already completed with error: org.springframework.web.context.request.async.AsyncRequestNotUsableException: Response not usable after response errors.
HttpServletResponse 객체 응답 보낸 후 해당 응답 객체로 Emit 해서 나는 에러라고도 하고,
Spring에서 ResponseBodyEmitter를 사용하는 비동기 요청 처리 중에 응답이 이미 완료된 상태에서 추가로 데이터를 전송하려고 시도했을 때 발생하는 에러라고 한다.
이 부분에 관련해서 또 한참을 검색해봤더니,
아래 링크를 발견했다.
https://github.com/spring-projects/spring-framework/issues/32509
AsyncRequestNotUsableException thrown and logged after upgrade from Spring Boot 3.2.3 to Spring Boot 3.2.4 · Issue #32509 · s
Hi after upgrading to spring boot 3.2.4 from 3.2.3 our applications start logging this exception over and over (I suspect on every request): org.springframework.web.context.request.async.AsyncReque...
github.com
Spring Boot 버전 3.2.3에서 3.2.4로 업그레이드 한 후에 발생하지 않던 에러가 터진다는 내용이다..?!
다른 분들의 댓글도 확인해보니 이런 에러를 겪으신 분들이 많은 것 같다.
바로 우리 서비스의 버전을 확인해봤더니, 기가 막히게 3.2.4 이후의 버전이었다.
그래서 3.2.3 버전과 우리가 사용하던 버전의 호환성을 검색해보고 혹시라도 버전이 낮아지면서 에러가 발생할 수 있으니 테스트도 전부 다시 돌려봤다.
다행히 문제가 발생하지 않아서, 빌드 변경 후 배포해봤다.
에러가 해결됐다......!!!!!!!!!!!
그렇다면 이후에 업그레이드 버전을 사용한다면 어떻게 해결할 것인가 하면..
그 부분은 추가적으로 공부가 필요하다.
아마 아직까지는 레퍼런스가 많이 존재하지 않아서 더 깊은 부분은 탐색하기 어려울 것 같다.
결론 : Spring Boot 버전을 3.2.3 이전 버전으로 변경할 것