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

don't close the connection when opening the identify stream fails #1293

Merged
merged 2 commits into from
Jan 10, 2022
Merged
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
46 changes: 15 additions & 31 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,21 @@ func (ids *idService) IdentifyWait(c network.Conn) <-chan struct{} {
defer ids.connsMu.Unlock()

wait, found = ids.conns[c]

if !found {
wait = make(chan struct{})
ids.conns[c] = wait

// Spawn an identify. The connection may actually be closed
// already, but that doesn't really matter. We'll fail to open a
// stream then forget the connection.
go ids.identifyConn(c, wait)
go func() {
defer close(wait)
if err := ids.identifyConn(c); err != nil {
ids.emitters.evtPeerIdentificationFailed.Emit(event.EvtPeerIdentificationFailed{Peer: c.RemotePeer(), Reason: err})
return
}
ids.emitters.evtPeerIdentificationCompleted.Emit(event.EvtPeerIdentificationCompleted{Peer: c.RemotePeer()})
}()
}

return wait
Expand All @@ -341,48 +347,26 @@ func (ids *idService) removeConn(c network.Conn) {
ids.connsMu.Unlock()
}

func (ids *idService) identifyConn(c network.Conn, signal chan struct{}) {
var (
s network.Stream
err error
)

defer func() {
close(signal)

// emit the appropriate event.
if p := c.RemotePeer(); err == nil {
ids.emitters.evtPeerIdentificationCompleted.Emit(event.EvtPeerIdentificationCompleted{Peer: p})
} else {
ids.emitters.evtPeerIdentificationFailed.Emit(event.EvtPeerIdentificationFailed{Peer: p, Reason: err})
}
}()

s, err = c.NewStream(network.WithUseTransient(context.TODO(), "identify"))
func (ids *idService) identifyConn(c network.Conn) error {
s, err := c.NewStream(network.WithUseTransient(context.TODO(), "identify"))
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldnt we have a timeout here? Or do we set the deadline?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's do that in a separate PR. We do set a bunch of deadlines, but only read deadlines, which can lead to funny edge cases if the peer withholds flow control credit.

if err != nil {
log.Debugw("error opening identify stream", "error", err)
// the connection is probably already closed if we hit this.
// TODO: Remove this?
c.Close()

// We usually do this on disconnect, but we may have already
// processed the disconnect event.
ids.removeConn(c)
return
return err
}
s.SetProtocol(ID)

// ok give the response to our handler.
if err = msmux.SelectProtoOrFail(ID, s); err != nil {
log.Infow("failed negotiate identify protocol with peer",
"peer", c.RemotePeer(),
"error", err,
)
if err := msmux.SelectProtoOrFail(ID, s); err != nil {
log.Infow("failed negotiate identify protocol with peer", "peer", c.RemotePeer(), "error", err)
s.Reset()
return
return err
}

err = ids.handleIdentifyResponse(s)
return ids.handleIdentifyResponse(s)
}

func (ids *idService) sendIdentifyResp(s network.Stream) {
Expand Down