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

Ensure network connection is closed when handshake with peer fails #1476

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions p2p/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,20 @@ async def handshake(
use_eip8 = False
initiator = HandshakeInitiator(remote, privkey, use_eip8, token)
reader, writer = await initiator.connect()
aes_secret, mac_secret, egress_mac, ingress_mac = await _handshake(
initiator, reader, writer, token)
try:
aes_secret, mac_secret, egress_mac, ingress_mac = await _handshake(
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this could be rewritten using an asynccontextmanager so that becomes:

async with initiator.connect() as reader, writer:
    aes_secret, mac_secret, egress_mac, ingress_mac = await _handshake(
            initiator, reader, writer, token)

Copy link
Member Author

Choose a reason for hiding this comment

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

In theory yes. I've thought about this a bit but I'm unsure enough about it that I'd like to punt, with the compromise being adding a larger comment to the section explaining why the solution isn't ideal.

initiator, reader, writer, token)
except Exception:
# Note: This is one of two places where we manually handle closing the
# reader/writer connection pair in the event of an error during the
# peer connection and handshake process.
# See `p2p.peer.handshake` for the other.
if not reader.at_eof():
reader.feed_eof()
writer.close()
await asyncio.sleep(0)
raise

return aes_secret, mac_secret, egress_mac, ingress_mac, reader, writer


Expand Down
22 changes: 17 additions & 5 deletions p2p/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,21 @@ async def handshake(remote: Node, factory: 'BasePeerFactory') -> 'BasePeer':
connection=connection,
inbound=False,
)
await peer.do_p2p_handshake()
await peer.do_sub_proto_handshake()

try:
await peer.do_p2p_handshake()
await peer.do_sub_proto_handshake()
except Exception:
# Note: This is one of two places where we manually handle closing the
# reader/writer connection pair in the event of an error during the
# peer connection and handshake process.
# See `p2p.auth.handshake` for the other.
if not reader.at_eof():
reader.feed_eof()
writer.close()
await asyncio.sleep(0)
raise

return peer


Expand Down Expand Up @@ -342,9 +355,8 @@ def close(self) -> None:

If the streams have already been closed, do nothing.
"""
if self.reader.at_eof():
return
self.reader.feed_eof()
if not self.reader.at_eof():
self.reader.feed_eof()
self.writer.close()

@property
Expand Down