diff --git a/.changes/next-release/bugfix-AWSSDKforJavav2-8c2f4c2.json b/.changes/next-release/bugfix-AWSSDKforJavav2-8c2f4c2.json new file mode 100644 index 000000000000..d27d0339b164 --- /dev/null +++ b/.changes/next-release/bugfix-AWSSDKforJavav2-8c2f4c2.json @@ -0,0 +1,6 @@ +{ + "type": "bugfix", + "category": "AWS SDK for Java v2", + "description": "Map connection reset and aborted socket exceptions in NettyUtils to decorate closed channel exceptions across operating systems", + "contributor": "reginaldalfret" +} diff --git a/http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/utils/NettyUtils.java b/http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/utils/NettyUtils.java index 6a9d0eedb682..0dde196de6e1 100644 --- a/http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/utils/NettyUtils.java +++ b/http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/utils/NettyUtils.java @@ -40,6 +40,7 @@ import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.time.Duration; +import java.util.Locale; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -92,10 +93,17 @@ public static Throwable decorateException(Channel channel, Throwable originalCau } private static boolean isConnectionResetException(Throwable originalCause) { + if (!(originalCause instanceof IOException)) { + return false; + } String message = originalCause.getMessage(); - return originalCause instanceof IOException && - message != null && - message.contains("Connection reset by peer"); + if (message == null) { + return false; + } + String lowerMessage = message.toLowerCase(Locale.ENGLISH); + return lowerMessage.contains("connection reset") || + lowerMessage.contains("forcibly closed by the remote host") || + lowerMessage.contains("connection was aborted"); } private static boolean isAcquireTimeoutException(Throwable originalCause) { diff --git a/http-clients/netty-nio-client/src/test/java/software/amazon/awssdk/http/nio/netty/internal/utils/NettyUtilsTest.java b/http-clients/netty-nio-client/src/test/java/software/amazon/awssdk/http/nio/netty/internal/utils/NettyUtilsTest.java index ce3526ba8efd..ac78cb214741 100644 --- a/http-clients/netty-nio-client/src/test/java/software/amazon/awssdk/http/nio/netty/internal/utils/NettyUtilsTest.java +++ b/http-clients/netty-nio-client/src/test/java/software/amazon/awssdk/http/nio/netty/internal/utils/NettyUtilsTest.java @@ -38,6 +38,7 @@ import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.concurrent.Promise; import java.io.IOException; +import java.net.SocketException; import java.nio.channels.ClosedChannelException; import java.time.Duration; import java.util.concurrent.TimeoutException; @@ -378,4 +379,48 @@ public void decorateException_with_IOException_noMsg() { assertThat(output).isInstanceOf(IOException.class); assertThat(output.getCause()).isNull(); } + + @Test + public void decorateException_with_SocketException_connectionReset() { + Channel channel = mock(Channel.class); + Throwable connectionResetException = new SocketException("Connection reset"); + Throwable output = NettyUtils.decorateException(channel, connectionResetException); + + assertThat(output).isInstanceOf(IOException.class); + assertThat(output.getCause()).isSameAs(connectionResetException); + assertThat(output.getMessage()).contains(NettyUtils.CLOSED_CHANNEL_ERROR_MESSAGE); + } + + @Test + public void decorateException_with_IOException_forciblyClosed() { + Channel channel = mock(Channel.class); + Throwable forciblyClosedException = new IOException("An existing connection was forcibly closed by the remote host"); + Throwable output = NettyUtils.decorateException(channel, forciblyClosedException); + + assertThat(output).isInstanceOf(IOException.class); + assertThat(output.getCause()).isSameAs(forciblyClosedException); + assertThat(output.getMessage()).contains(NettyUtils.CLOSED_CHANNEL_ERROR_MESSAGE); + } + + @Test + public void decorateException_with_IOException_lowercaseReset() { + Channel channel = mock(Channel.class); + Throwable lowerCaseResetException = new IOException("read error: connection reset"); + Throwable output = NettyUtils.decorateException(channel, lowerCaseResetException); + + assertThat(output).isInstanceOf(IOException.class); + assertThat(output.getCause()).isSameAs(lowerCaseResetException); + assertThat(output.getMessage()).contains(NettyUtils.CLOSED_CHANNEL_ERROR_MESSAGE); + } + + @Test + public void decorateException_with_IOException_connectionAborted() { + Channel channel = mock(Channel.class); + Throwable connectionAbortedException = new IOException("An established connection was aborted by the software in your host machine"); + Throwable output = NettyUtils.decorateException(channel, connectionAbortedException); + + assertThat(output).isInstanceOf(IOException.class); + assertThat(output.getCause()).isSameAs(connectionAbortedException); + assertThat(output.getMessage()).contains(NettyUtils.CLOSED_CHANNEL_ERROR_MESSAGE); + } }