Skip to content

Commit 814c003

Browse files
committed
Align 5.3.x with 6.1.x
In preparation for a larger update, start by aligning with 6.1.x, which includes changes for gh-32042 and gh-30232. See gh-32341
1 parent 5c34e1d commit 814c003

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Map;
2323
import java.util.concurrent.Callable;
2424
import java.util.concurrent.Future;
25-
import java.util.concurrent.RejectedExecutionException;
2625

2726
import jakarta.servlet.http.HttpServletRequest;
2827
import org.apache.commons.logging.Log;
@@ -35,6 +34,7 @@
3534
import org.springframework.util.Assert;
3635
import org.springframework.web.context.request.RequestAttributes;
3736
import org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler;
37+
import org.springframework.web.util.DisconnectedClientHelper;
3838

3939
/**
4040
* The central class for managing asynchronous request processing, mainly intended
@@ -68,6 +68,16 @@ public final class WebAsyncManager {
6868

6969
private static final Log logger = LogFactory.getLog(WebAsyncManager.class);
7070

71+
/**
72+
* Log category to use for network failure after a client has gone away.
73+
* @see DisconnectedClientHelper
74+
*/
75+
private static final String DISCONNECTED_CLIENT_LOG_CATEGORY =
76+
"org.springframework.web.server.DisconnectedClient";
77+
78+
private static final DisconnectedClientHelper disconnectedClientHelper =
79+
new DisconnectedClientHelper(DISCONNECTED_CLIENT_LOG_CATEGORY);
80+
7181
private static final CallableProcessingInterceptor timeoutCallableInterceptor =
7282
new TimeoutCallableProcessingInterceptor();
7383

@@ -350,10 +360,9 @@ public void startCallableProcessing(final WebAsyncTask<?> webAsyncTask, Object..
350360
});
351361
interceptorChain.setTaskFuture(future);
352362
}
353-
catch (RejectedExecutionException ex) {
363+
catch (Throwable ex) {
354364
Object result = interceptorChain.applyPostProcess(this.asyncWebRequest, callable, ex);
355365
setConcurrentResultAndDispatch(result);
356-
throw ex;
357366
}
358367
}
359368

@@ -394,9 +403,12 @@ private void setConcurrentResultAndDispatch(@Nullable Object result) {
394403
return;
395404
}
396405

406+
if (result instanceof Exception ex && disconnectedClientHelper.checkAndLogClientDisconnectedException(ex)) {
407+
return;
408+
}
409+
397410
if (logger.isDebugEnabled()) {
398-
boolean isError = result instanceof Throwable;
399-
logger.debug("Async " + (isError ? "error" : "result set") +
411+
logger.debug("Async " + (this.errorHandlingInProgress ? "error" : "result set") +
400412
", dispatch to " + formatUri(this.asyncWebRequest));
401413
}
402414
this.asyncWebRequest.dispatch();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.util;
18+
19+
import java.util.Set;
20+
21+
import org.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
23+
24+
import org.springframework.core.NestedExceptionUtils;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* Utility methods to assist with identifying and logging exceptions that indicate
29+
* the client has gone away. Such exceptions fill logs with unnecessary stack
30+
* traces. The utility methods help to log a single line message at DEBUG level,
31+
* and a full stacktrace at TRACE level.
32+
*
33+
* @author Rossen Stoyanchev
34+
* @since 6.1
35+
*/
36+
public class DisconnectedClientHelper {
37+
38+
private static final Set<String> EXCEPTION_PHRASES =
39+
Set.of("broken pipe", "connection reset by peer");
40+
41+
private static final Set<String> EXCEPTION_TYPE_NAMES =
42+
Set.of("AbortedException", "ClientAbortException", "EOFException", "EofException");
43+
44+
private final Log logger;
45+
46+
47+
public DisconnectedClientHelper(String logCategory) {
48+
Assert.notNull(logCategory, "'logCategory' is required");
49+
this.logger = LogFactory.getLog(logCategory);
50+
}
51+
52+
53+
/**
54+
* Check via {@link #isClientDisconnectedException} if the exception
55+
* indicates the remote client disconnected, and if so log a single line
56+
* message when DEBUG is on, and a full stacktrace when TRACE is on for
57+
* the configured logger.
58+
*/
59+
public boolean checkAndLogClientDisconnectedException(Throwable ex) {
60+
if (isClientDisconnectedException(ex)) {
61+
if (logger.isTraceEnabled()) {
62+
logger.trace("Looks like the client has gone away", ex);
63+
}
64+
else if (logger.isDebugEnabled()) {
65+
logger.debug("Looks like the client has gone away: " + ex +
66+
" (For a full stack trace, set the log category '" + logger + "' to TRACE level.)");
67+
}
68+
return true;
69+
}
70+
return false;
71+
}
72+
73+
/**
74+
* Whether the given exception indicates the client has gone away.
75+
* Known cases covered:
76+
* <ul>
77+
* <li>ClientAbortException or EOFException for Tomcat
78+
* <li>EofException for Jetty
79+
* <li>IOException "Broken pipe" or "connection reset by peer"
80+
* </ul>
81+
*/
82+
public static boolean isClientDisconnectedException(Throwable ex) {
83+
String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
84+
if (message != null) {
85+
String text = message.toLowerCase();
86+
for (String phrase : EXCEPTION_PHRASES) {
87+
if (text.contains(phrase)) {
88+
return true;
89+
}
90+
}
91+
}
92+
return EXCEPTION_TYPE_NAMES.contains(ex.getClass().getSimpleName());
93+
}
94+
95+
}

0 commit comments

Comments
 (0)