libp2p / go-libp2p-pubsub

The PubSub implementation for go-libp2p
https://github.com/libp2p/specs/tree/master/pubsub
Other
309 stars 178 forks source link

NewStream failure right after startup #546

Open cpacia opened 8 months ago

cpacia commented 8 months ago

This took me forever debug, but here's the issue. If you start up two nodes and immediately join a topic they will fail discover each other in the pubsub. Here's why:

Node1

Node2

Nodes 1 and 2 never connect :(

Fortunately I was able to come up with a fix that doesn't require any changes in libp2p. Basically detect the protocolupdate event and force it think it received a new connection:

        protocolUpdatedSub, err := host.EventBus().Subscribe(new(event.EvtPeerProtocolsUpdated))
    if err != nil {
        return nil, err
    }
    go func(sub event.Subscription) {
        for {
            select {
            case evt, ok := <-sub.Out():
                if !ok {
                    return
                }
                var updated bool
                for _, proto := range evt.(event.EvtPeerProtocolsUpdated).Added {
                    if proto == cfg.params.ProtocolPrefix+pubsub.GossipSubID_v11 {
                        updated = true
                        break
                    }
                }
                if updated {
                    for _, c := range host.Network().ConnsToPeer(evt.(event.EvtPeerProtocolsUpdated).Peer) {
                        (*pubsub.PubSubNotif)(ps).Connected(host.Network(), c)
                    }
                }
            }
        }
    }(protocolUpdatedSub)

But it would be nice if this was built in as it's definitely not expected behavior.

cpacia commented 8 months ago

Looks like there is an open PR attempting to address this issue.