Skip to content

Add null check for channel when generating error message #3574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-c258b00.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "sigurjonviktorsson",
"description": "Prevent NullPointerException in Netty exception handler"
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public final class NettyUtils {
*/
public static final SucceededFuture<?> SUCCEEDED_FUTURE = new SucceededFuture<>(null, null);

public static final String CLOSED_CHANNEL_ERROR_MESSAGE = "The connection was closed during the request. The request will "
+ "usually succeed on a retry, but if it does not: consider "
+ "disabling any proxies you have configured, enabling debug "
+ "logging, or performing a TCP dump to identify the root cause. "
+ "If this is a streaming operation, validate that data is being "
+ "read or written in a timely manner.";
private static final Logger log = Logger.loggerFor(NettyUtils.class);

private NettyUtils() {
Expand Down Expand Up @@ -133,15 +139,14 @@ private static String getMessageForTooManyAcquireOperationsError() {
}

public static String closedChannelMessage(Channel channel) {
ChannelDiagnostics channelDiagnostics = channel.attr(CHANNEL_DIAGNOSTICS).get();
Copy link
Contributor

@joviegas joviegas Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about updating it to below code

        ChannelDiagnostics channelDiagnostics = channel != null && channel.attr(CHANNEL_DIAGNOSTICS) != null ?
                                                channel.attr(CHANNEL_DIAGNOSTICS).get() : null;
        ChannelDiagnostics parentChannelDiagnostics = channel != null && channel.parent() != null ?
                                                      channel.parent().attr(CHANNEL_DIAGNOSTICS).get() : null;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good to me, I added a null check for the CHANNEL_DIAGNOSTICS attribute on the parent as well. Not sure if this passes style rules.

ChannelDiagnostics parentChannelDiagnostics = channel.parent() != null ? channel.parent().attr(CHANNEL_DIAGNOSTICS).get()
: null;
ChannelDiagnostics channelDiagnostics = channel != null && channel.attr(CHANNEL_DIAGNOSTICS) != null ?
channel.attr(CHANNEL_DIAGNOSTICS).get() : null;
ChannelDiagnostics parentChannelDiagnostics = channel != null && channel.parent() != null &&
channel.parent().attr(CHANNEL_DIAGNOSTICS) != null ?
channel.parent().attr(CHANNEL_DIAGNOSTICS).get() : null;

StringBuilder error = new StringBuilder();
error.append("The connection was closed during the request. The request will usually succeed on a retry, but if it does"
+ " not: consider disabling any proxies you have configured, enabling debug logging, or performing a TCP"
+ " dump to identify the root cause. If this is a streaming operation, validate that data is being read or"
+ " written in a timely manner.");
error.append(CLOSED_CHANNEL_ERROR_MESSAGE);

if (channelDiagnostics != null) {
error.append(" Channel Information: ").append(channelDiagnostics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
Expand All @@ -41,6 +42,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import software.amazon.awssdk.http.nio.netty.internal.MockChannel;

Expand Down Expand Up @@ -222,4 +224,48 @@ public void runAndLogError_runnableThrows_loggerInvoked() {

verify(delegateLogger).error(msg, error);
}

@Test
public void closedChannelMessage_with_nullChannelAttribute() throws Exception {

Channel channel = Mockito.mock(Channel.class);
when(channel.parent()).thenReturn(null);

assertThat(NettyUtils.closedChannelMessage(channel))
.isEqualTo(NettyUtils.CLOSED_CHANNEL_ERROR_MESSAGE);
}

@Test
public void closedChannelMessage_with_nullChannel() throws Exception {
Channel channel = null;
assertThat(NettyUtils.closedChannelMessage(channel))
.isEqualTo(NettyUtils.CLOSED_CHANNEL_ERROR_MESSAGE);
}


@Test
public void closedChannelMessage_with_nullParentChannel() throws Exception {

Channel channel = mock(Channel.class);
Attribute attribute = mock(Attribute.class);
when(channel.parent()).thenReturn(null);
when(channel.attr(any())).thenReturn(attribute);

assertThat(NettyUtils.closedChannelMessage(channel))
.isEqualTo(NettyUtils.CLOSED_CHANNEL_ERROR_MESSAGE);
}

@Test
public void closedChannelMessage_with_nullParentChannelAttribute() throws Exception {

Channel channel = mock(Channel.class);
Attribute attribute = mock(Attribute.class);
Channel parentChannel = mock(Channel.class);
when(channel.parent()).thenReturn(parentChannel);
when(channel.attr(any())).thenReturn(attribute);
when(parentChannel.attr(any())).thenReturn(null);

assertThat(NettyUtils.closedChannelMessage(channel))
.isEqualTo(NettyUtils.CLOSED_CHANNEL_ERROR_MESSAGE);
}
}