diff --git a/spring-web/src/main/java/org/springframework/web/util/DisconnectedClientHelper.java b/spring-web/src/main/java/org/springframework/web/util/DisconnectedClientHelper.java index b2b16f135ad..9d49f1b1a30 100644 --- a/spring-web/src/main/java/org/springframework/web/util/DisconnectedClientHelper.java +++ b/spring-web/src/main/java/org/springframework/web/util/DisconnectedClientHelper.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. @@ -36,8 +36,11 @@ import org.springframework.util.Assert; */ public class DisconnectedClientHelper { + // Look for server response connection issues (aborted), not onward connections + // to other servers (500 errors). + private static final Set EXCEPTION_PHRASES = - Set.of("broken pipe", "connection reset"); + Set.of("broken pipe", "connection reset by peer"); private static final Set EXCEPTION_TYPE_NAMES = Set.of("AbortedException", "ClientAbortException", @@ -79,7 +82,6 @@ public class DisconnectedClientHelper { *
  • ClientAbortException or EOFException for Tomcat *
  • EofException for Jetty *
  • IOException "Broken pipe" or "connection reset by peer" - *
  • SocketException "Connection reset" * */ public static boolean isClientDisconnectedException(Throwable ex) { diff --git a/spring-web/src/test/java/org/springframework/web/util/DisconnectedClientHelperTests.java b/spring-web/src/test/java/org/springframework/web/util/DisconnectedClientHelperTests.java new file mode 100644 index 00000000000..02fbd9a5ab8 --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/util/DisconnectedClientHelperTests.java @@ -0,0 +1,69 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.util; + +import java.io.EOFException; +import java.io.IOException; +import java.util.List; + +import org.apache.catalina.connector.ClientAbortException; +import org.eclipse.jetty.io.EofException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import reactor.netty.channel.AbortedException; + +import org.springframework.web.context.request.async.AsyncRequestNotUsableException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link DisconnectedClientHelper}. + * @author Rossen Stoyanchev + */ +public class DisconnectedClientHelperTests { + + @ParameterizedTest + @ValueSource(strings = {"broKen pipe", "connection reset By peer"}) + void exceptionPhrases(String phrase) { + Exception ex = new IOException(phrase); + assertThat(DisconnectedClientHelper.isClientDisconnectedException(ex)).isTrue(); + + ex = new IOException(ex); + assertThat(DisconnectedClientHelper.isClientDisconnectedException(ex)).isTrue(); + } + + @Test + void connectionResetExcluded() { + Exception ex = new IOException("connection reset"); + assertThat(DisconnectedClientHelper.isClientDisconnectedException(ex)).isFalse(); + } + + @ParameterizedTest + @MethodSource("disconnectedExceptions") + void name(Exception ex) { + assertThat(DisconnectedClientHelper.isClientDisconnectedException(ex)).isTrue(); + } + + static List disconnectedExceptions() { + return List.of( + new AbortedException(""), new ClientAbortException(""), + new EOFException(), new EofException(), new AsyncRequestNotUsableException("")); + } + +}