From 448e465f3fd97bdfe56add41213133eeb017d342 Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Fri, 23 Nov 2018 07:06:31 +1000 Subject: [PATCH] Check if the connectFuture has completed successfully properly. (#293) Avoids throwing java.util.concurrent.ExecutionException when handling other framing errors in DeFramer. --- .../pantheon/ethereum/p2p/netty/DeFramer.java | 4 ++-- .../ethereum/p2p/netty/DeFramerTest.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/DeFramer.java b/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/DeFramer.java index c3f356fa28..c3fd4243b7 100644 --- a/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/DeFramer.java +++ b/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/netty/DeFramer.java @@ -125,7 +125,7 @@ final class DeFramer extends ByteToMessageDecoder { : throwable; if (cause instanceof FramingException) { LOG.debug("Invalid incoming message", throwable); - if (connectFuture.isDone()) { + if (connectFuture.isDone() && !connectFuture.isCompletedExceptionally()) { connectFuture.get().disconnect(DisconnectReason.BREACH_OF_PROTOCOL); return; } @@ -135,7 +135,7 @@ final class DeFramer extends ByteToMessageDecoder { } else { LOG.error("Exception while processing incoming message", throwable); } - if (connectFuture.isDone()) { + if (connectFuture.isDone() && !connectFuture.isCompletedExceptionally()) { connectFuture.get().terminateConnection(DisconnectReason.TCP_SUBSYSTEM_ERROR, false); } else { connectFuture.completeExceptionally(throwable); diff --git a/ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/DeFramerTest.java b/ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/DeFramerTest.java index 4700311e30..d85feed798 100644 --- a/ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/DeFramerTest.java +++ b/ethereum/p2p/src/test/java/tech/pegasys/pantheon/ethereum/p2p/netty/DeFramerTest.java @@ -52,4 +52,22 @@ public class DeFramerTest { verify(peerConnection).disconnect(DisconnectReason.BREACH_OF_PROTOCOL); } + + @Test + public void shouldHandleFramingExceptionWhenFutureCompletedExceptionally() throws Exception { + connectFuture.completeExceptionally(new Exception()); + + deFramer.exceptionCaught(ctx, new DecoderException(new FramingException("Test"))); + + verify(ctx).close(); + } + + @Test + public void shouldHandleGenericExceptionWhenFutureCompletedExceptionally() throws Exception { + connectFuture.completeExceptionally(new Exception()); + + deFramer.exceptionCaught(ctx, new DecoderException(new RuntimeException("Test"))); + + verify(ctx).close(); + } }