Skip to content
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

Fix on handshake failure not disconnecting connection #6179

Merged
merged 2 commits into from Oct 11, 2023

Conversation

asdacap
Copy link
Contributor

@asdacap asdacap commented Oct 11, 2023

  • When the exception handler did not explicitly handle the exception, the exception will pass down to the next handler, which if its the tail, it'll do nothing.
  • This add an explicit disconnect on exception.
  • Also fix the method itself throwing exception.

Changes

  • Add explicit disconnect on ExceptionCaught
  • Wrap trying to get client id string so that on exception, it does nothing.

Types of changes

What types of changes does your code introduce?

  • Bugfix (a non-breaking change that fixes an issue)

Testing

Requires testing

  • Yes

If yes, did you write tests?

  • Yes

Notes on testing

  • Verified with a custom spammer.

string clientId = $"unknown {_session?.RemoteHost}";
try
{
clientId = _session?.Node?.ToString(Node.Format.Console);
Copy link
Member

Choose a reason for hiding this comment

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

why does this throw exceptions? Can we have a version that is guaranteed not to throw?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Node property throw exception because RemoteId is still missing at this point. Changing that to a nullable might make other place more inconviniecne.

Copy link
Member

Choose a reason for hiding this comment

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

Can we handle that in ToString method and not throw exceptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which ToString? The Node property of the _session that is throwing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cosmetic: instead of having empty catch we could move the below checks to session bool property:

              if (_node is null)
                {
                    if (RemoteNodeId is null || RemoteHost is null || RemotePort == 0)
                    {

Copy link
Member

Choose a reason for hiding this comment

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

Ah can we introduce Session.TryGetNode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just add if (_session.RemoteNodeId != null) in front of it.

@@ -127,7 +135,7 @@ public override void ExceptionCaught(IChannelHandlerContext context, Exception e
}
}

base.ExceptionCaught(context, exception);
_ = context.DisconnectAsync();
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we need to call base class anymore? Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling base class does nothing. It just pass the exception away to the tail.

string clientId = $"unknown {_session?.RemoteHost}";
try
{
clientId = _session?.Node?.ToString(Node.Format.Console);
Copy link
Contributor

Choose a reason for hiding this comment

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

Cosmetic: instead of having empty catch we could move the below checks to session bool property:

              if (_node is null)
                {
                    if (RemoteNodeId is null || RemoteHost is null || RemotePort == 0)
                    {

@asdacap asdacap merged commit e047102 into master Oct 11, 2023
61 checks passed
@asdacap asdacap deleted the fix/not-disconnecting-on-rlpx-handshake-failure branch October 11, 2023 12:48
kamilchodola pushed a commit that referenced this pull request Oct 13, 2023
* Fix on handshake failure not disconnecting

* Addressing comment
brbrr pushed a commit that referenced this pull request Oct 23, 2023
* Fix on handshake failure not disconnecting

* Addressing comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants