Skip to content

Commit

Permalink
Fix on handshake failure not disconnecting connection (#6179)
Browse files Browse the repository at this point in the history
* Fix on handshake failure not disconnecting

* Addressing comment
  • Loading branch information
asdacap authored and brbrr committed Oct 23, 2023
1 parent 645717b commit 7a43ff0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Nethermind.Network.Rlpx;
using Nethermind.Network.Rlpx.Handshake;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using NUnit.Framework;

namespace Nethermind.Network.Test.Rlpx.Handshake
Expand Down Expand Up @@ -64,6 +65,11 @@ public void Setup()

private NettyHandshakeHandler CreateHandler(HandshakeRole handshakeRole = HandshakeRole.Recipient)
{
if (handshakeRole == HandshakeRole.Recipient)
{
_session.Node.Throws(new InvalidOperationException("property throw on incoming connection before handshake"));
_session.RemoteNodeId.Returns((PublicKey)null); // Incoming connection have null remote node id until handshake finished
}
return new NettyHandshakeHandler(_serializationService, _handshakeService, _session, handshakeRole, _logger, _group, TimeSpan.Zero);
}

Expand Down Expand Up @@ -211,5 +217,16 @@ public void Recipient_sends_ack_on_receiving_auth()
received.Should().BeTrue();
_handshakeService.Received(1).Ack(Arg.Any<EncryptionHandshake>(), Arg.Any<Packet>());
}

[Test]
public async Task Handler_disconnect_on_exception()
{
NettyHandshakeHandler handler = CreateHandler();

IChannelHandlerContext context = Substitute.For<IChannelHandlerContext>();
handler.ExceptionCaught(context, new Exception("any exception"));

await context.Received().DisconnectAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public override void ChannelRegistered(IChannelHandlerContext context)

public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
{
string clientId = _session?.Node?.ToString(Node.Format.Console) ?? $"unknown {_session?.RemoteHost}";
string clientId = $"unknown {_session?.RemoteHost}";
if (_session.RemoteNodeId != null) clientId = _session?.Node?.ToString(Node.Format.Console);

//In case of SocketException we log it as debug to avoid noise
if (exception is SocketException)
{
Expand All @@ -127,7 +129,7 @@ public override void ExceptionCaught(IChannelHandlerContext context, Exception e
}
}

base.ExceptionCaught(context, exception);
_ = context.DisconnectAsync();
}

protected override void ChannelRead0(IChannelHandlerContext context, IByteBuffer input)
Expand Down

0 comments on commit 7a43ff0

Please sign in to comment.