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(); + } }