Skip to content

Comments

chore: close as soon as possible handshake stream#5378

Closed
janos wants to merge 1 commit intomasterfrom
close-early-handshake-stream
Closed

chore: close as soon as possible handshake stream#5378
janos wants to merge 1 commit intomasterfrom
close-early-handshake-stream

Conversation

@janos
Copy link
Member

@janos janos commented Feb 19, 2026

Checklist

  • I have read the coding guide.
  • My change requires a documentation update, and I have done it.
  • I have added tests to cover my changes.
  • I have filled out the description and linked the related issues.

Description

@gacevicljubisa noticed that handshake.FullClose() can be performed earlier. This PR optimizes and cleans the code related to the handshake stream.

Open API Spec Version Changes (if applicable)

Motivation and Context (Optional)

Related Issue (Optional)

Screenshots (if appropriate):

return
}

if exists := s.peers.addIfNotExists(stream.Conn(), overlay, i.FullNode); exists {
Copy link
Contributor

Choose a reason for hiding this comment

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

consider (not sure if this is problematic):
if you close the stream earlier before the peer has been added to this peers struct, the other peer will progress with the handshake protocol (continues execution) and may fire a request before the peer was added (the call on this line). i'm not sure if this can cause downstream problems (maybe a protocol already tries to send a message, and some check on the libp2p abstraction that tries to check if the peer exists on this list happens before the peer was added

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, interesting observation. The question is, is handshake stream keeping this synchronization. I am not sure that I can see that by keeping the handshake stream active, other protocols are blocked from sending messages. Am interested to see if I am missing something. Could you elaborate more on your assumptions, or try to see if there could be problems?

I even cannot see why handshake stream is even created earlier than it is needed. This can also be changed.

Copy link
Contributor

Choose a reason for hiding this comment

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

iirc the FullClose usage was meant as a means of synchronizing both peers and making sure both peers are at the same stage of the handshake. Previously they would be reading on the stream, waiting for something, and when we would close the stream they could then continue execution.

For example on the connecting side, the protocols start firing here.
The receiver here.

The question is whether under some lock contention on that peer registry or under circumstances will we see one peer finishing the handshake on the other before the other one making it to the line where that peer gets onto the peer registry. If yes then there might be some downstream errors or some weird halfway state (peer is "halfway" connected). Whether the current handshake protocol prevents that fully - can't say, need to have a further look.

@martinconic
Copy link
Contributor

I have also tried these and the issues from connections_test.go seems to get worse. Investigating this I am getting to a similar opinion as Elad. I am not sure about Closing early.

@janos
Copy link
Member Author

janos commented Feb 20, 2026

I am closing the PR as it appears that the stream is kept active in order to synchronize node internals. There is a possibility to improve the code to encapsulate the required operations on both sides, to be more obvious why the stream is kept open, but it is just code maintenance, not a functionality improvement. There is other PR that deals with the improvements of the tests that may be related to this part of the code as well.

@janos janos closed this Feb 20, 2026
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.

3 participants