Skip to content

Commit b31550f

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-32342
1 parent 701e9e4 commit b31550f

File tree

3 files changed

+126
-12
lines changed

3 files changed

+126
-12
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,11 +28,12 @@
2828
import javax.servlet.http.HttpServletRequest;
2929
import javax.servlet.http.HttpServletResponse;
3030

31+
import org.springframework.lang.Nullable;
3132
import org.springframework.util.Assert;
3233
import org.springframework.web.context.request.ServletWebRequest;
3334

3435
/**
35-
* A Servlet 3.0 implementation of {@link AsyncWebRequest}.
36+
* A Servlet implementation of {@link AsyncWebRequest}.
3637
*
3738
* <p>The servlet and all filters involved in an async request must have async
3839
* support enabled using the Servlet API or by adding an
@@ -44,18 +45,20 @@
4445
*/
4546
public class StandardServletAsyncWebRequest extends ServletWebRequest implements AsyncWebRequest, AsyncListener {
4647

47-
private Long timeout;
48-
49-
private AsyncContext asyncContext;
50-
51-
private AtomicBoolean asyncCompleted = new AtomicBoolean();
48+
private final AtomicBoolean asyncCompleted = new AtomicBoolean();
5249

5350
private final List<Runnable> timeoutHandlers = new ArrayList<>();
5451

5552
private final List<Consumer<Throwable>> exceptionHandlers = new ArrayList<>();
5653

5754
private final List<Runnable> completionHandlers = new ArrayList<>();
5855

56+
@Nullable
57+
private Long timeout;
58+
59+
@Nullable
60+
private AsyncContext asyncContext;
61+
5962

6063
/**
6164
* Create a new instance for the given request/response pair.

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

Lines changed: 19 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 javax.servlet.http.HttpServletRequest;
2827

@@ -36,6 +35,7 @@
3635
import org.springframework.util.Assert;
3736
import org.springframework.web.context.request.RequestAttributes;
3837
import org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler;
38+
import org.springframework.web.util.DisconnectedClientHelper;
3939

4040
/**
4141
* The central class for managing asynchronous request processing, mainly intended
@@ -69,6 +69,16 @@ public final class WebAsyncManager {
6969

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

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

@@ -351,10 +361,9 @@ public void startCallableProcessing(final WebAsyncTask<?> webAsyncTask, Object..
351361
});
352362
interceptorChain.setTaskFuture(future);
353363
}
354-
catch (RejectedExecutionException ex) {
364+
catch (Throwable ex) {
355365
Object result = interceptorChain.applyPostProcess(this.asyncWebRequest, callable, ex);
356366
setConcurrentResultAndDispatch(result);
357-
throw ex;
358367
}
359368
}
360369

@@ -395,9 +404,14 @@ private void setConcurrentResultAndDispatch(@Nullable Object result) {
395404
return;
396405
}
397406

407+
if (result instanceof Exception) {
408+
if (disconnectedClientHelper.checkAndLogClientDisconnectedException((Exception) result)) {
409+
return;
410+
}
411+
}
412+
398413
if (logger.isDebugEnabled()) {
399-
boolean isError = result instanceof Throwable;
400-
logger.debug("Async " + (isError ? "error" : "result set") +
414+
logger.debug("Async " + (this.errorHandlingInProgress ? "error" : "result set") +
401415
", dispatch to " + formatUri(this.asyncWebRequest));
402416
}
403417
this.asyncWebRequest.dispatch();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2002-2024 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.Arrays;
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
25+
26+
import org.springframework.core.NestedExceptionUtils;
27+
import org.springframework.util.Assert;
28+
29+
/**
30+
* Utility methods to assist with identifying and logging exceptions that indicate
31+
* the client has gone away. Such exceptions fill logs with unnecessary stack
32+
* traces. The utility methods help to log a single line message at DEBUG level,
33+
* and a full stacktrace at TRACE level.
34+
*
35+
* @author Rossen Stoyanchev
36+
* @since 5.3.33
37+
*/
38+
public class DisconnectedClientHelper {
39+
40+
private static final Set<String> EXCEPTION_PHRASES =
41+
new HashSet<>(Arrays.asList("broken pipe", "connection reset by peer"));
42+
43+
private static final Set<String> EXCEPTION_TYPE_NAMES =
44+
new HashSet<>(Arrays.asList("AbortedException", "ClientAbortException", "EOFException", "EofException"));
45+
46+
private final Log logger;
47+
48+
49+
public DisconnectedClientHelper(String logCategory) {
50+
Assert.notNull(logCategory, "'logCategory' is required");
51+
this.logger = LogFactory.getLog(logCategory);
52+
}
53+
54+
55+
/**
56+
* Check via {@link #isClientDisconnectedException} if the exception
57+
* indicates the remote client disconnected, and if so log a single line
58+
* message when DEBUG is on, and a full stacktrace when TRACE is on for
59+
* the configured logger.
60+
*/
61+
public boolean checkAndLogClientDisconnectedException(Throwable ex) {
62+
if (isClientDisconnectedException(ex)) {
63+
if (logger.isTraceEnabled()) {
64+
logger.trace("Looks like the client has gone away", ex);
65+
}
66+
else if (logger.isDebugEnabled()) {
67+
logger.debug("Looks like the client has gone away: " + ex +
68+
" (For a full stack trace, set the log category '" + logger + "' to TRACE level.)");
69+
}
70+
return true;
71+
}
72+
return false;
73+
}
74+
75+
/**
76+
* Whether the given exception indicates the client has gone away.
77+
* Known cases covered:
78+
* <ul>
79+
* <li>ClientAbortException or EOFException for Tomcat
80+
* <li>EofException for Jetty
81+
* <li>IOException "Broken pipe" or "connection reset by peer"
82+
* </ul>
83+
*/
84+
public static boolean isClientDisconnectedException(Throwable ex) {
85+
String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
86+
if (message != null) {
87+
String text = message.toLowerCase();
88+
for (String phrase : EXCEPTION_PHRASES) {
89+
if (text.contains(phrase)) {
90+
return true;
91+
}
92+
}
93+
}
94+
return EXCEPTION_TYPE_NAMES.contains(ex.getClass().getSimpleName());
95+
}
96+
97+
}

0 commit comments

Comments
 (0)