From 7a0fe7d14faccc4574c3ee49ae92d657aacaa2fe Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 10 Feb 2025 11:14:02 +0000 Subject: [PATCH] WebAsyncManager wraps disconnected client errors If the Servlet container delegates a disconnected client error via AsyncListener#onError, wrap it as AsyncRequestNotUsableException for more targeted and consistent handling of such errors. Closes gh-34363 --- .../request/async/WebAsyncManager.java | 11 +++++- .../async/WebAsyncManagerErrorTests.java | 34 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java b/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java index 2cb1ae18ea09..00479d483e92 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import org.springframework.util.Assert; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler; +import org.springframework.web.util.DisconnectedClientHelper; /** * The central class for managing asynchronous request processing, mainly intended @@ -350,6 +351,10 @@ public void startCallableProcessing(final WebAsyncTask webAsyncTask, Object.. if (logger.isDebugEnabled()) { logger.debug("Servlet container error notification for " + formatUri(this.asyncWebRequest) + ": " + ex); } + if (DisconnectedClientHelper.isClientDisconnectedException(ex)) { + ex = new AsyncRequestNotUsableException( + "Servlet container error notification for disconnected client", ex); + } Object result = interceptorChain.triggerAfterError(this.asyncWebRequest, callable, ex); result = (result != CallableProcessingInterceptor.RESULT_NONE ? result : ex); setConcurrentResultAndDispatch(result); @@ -442,6 +447,10 @@ public void startDeferredResultProcessing( if (logger.isDebugEnabled()) { logger.debug("Servlet container error notification for " + formatUri(this.asyncWebRequest)); } + if (DisconnectedClientHelper.isClientDisconnectedException(ex)) { + ex = new AsyncRequestNotUsableException( + "Servlet container error notification for disconnected client", ex); + } try { interceptorChain.triggerAfterError(this.asyncWebRequest, deferredResult, ex); synchronized (WebAsyncManager.this) { diff --git a/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerErrorTests.java b/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerErrorTests.java index 542638dcbf2c..d11d6714c0ac 100644 --- a/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerErrorTests.java +++ b/spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerErrorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.web.context.request.async; +import java.io.IOException; import java.util.concurrent.Callable; import jakarta.servlet.AsyncEvent; @@ -152,6 +153,22 @@ void startCallableProcessingAfterException() throws Exception { verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, callable); } + @Test // gh-34363 + void startCallableProcessingDisconnectedClient() throws Exception { + StubCallable callable = new StubCallable(); + this.asyncManager.startCallableProcessing(callable); + + IOException ex = new IOException("broken pipe"); + AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), ex); + this.asyncWebRequest.onError(event); + + MockAsyncContext asyncContext = (MockAsyncContext) this.servletRequest.getAsyncContext(); + assertThat(this.asyncManager.hasConcurrentResult()).isTrue(); + assertThat(this.asyncManager.getConcurrentResult()) + .as("Disconnected client error not wrapped AsyncRequestNotUsableException") + .isOfAnyClassIn(AsyncRequestNotUsableException.class); + } + @Test void startDeferredResultProcessingErrorAndComplete() throws Exception { @@ -259,6 +276,21 @@ public boolean handleError(NativeWebRequest request, DeferredResult resul assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test"); } + @Test // gh-34363 + void startDeferredResultProcessingDisconnectedClient() throws Exception { + DeferredResult deferredResult = new DeferredResult<>(); + this.asyncManager.startDeferredResultProcessing(deferredResult); + + IOException ex = new IOException("broken pipe"); + AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), ex); + this.asyncWebRequest.onError(event); + + assertThat(this.asyncManager.hasConcurrentResult()).isTrue(); + assertThat(deferredResult.getResult()) + .as("Disconnected client error not wrapped AsyncRequestNotUsableException") + .isOfAnyClassIn(AsyncRequestNotUsableException.class); + } + private static final class StubCallable implements Callable { @Override