Skip to content

Commit

Permalink
chore: fix pubsub interop tests (#2191)
Browse files Browse the repository at this point in the history
When opening outbound streams, only make sure no outbound pubsub
streams exist on the connection, not just any pubsub streams.

Fixes a race condition where the remote peer can open streams
before us which then prevents us opening streams.
  • Loading branch information
achingbrain authored Nov 2, 2023
1 parent 3bdaad3 commit 16a8707
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/libp2p/test/interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ async function createJsPeer (options: SpawnOptions): Promise<Daemon> {
},
transports: [tcp(), circuitRelayTransport()],
streamMuxers: [],
connectionEncryption: [noise()]
connectionEncryption: [noise()],
connectionManager: {
minConnections: 0
}
}

const services: ServiceFactoryMap = {
Expand Down
13 changes: 10 additions & 3 deletions packages/pubsub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
log('connected %p', peerId)

// if this connection is already in use for pubsub, ignore it
if (conn.streams.find(stream => stream.protocol != null && this.multicodecs.includes(stream.protocol)) != null) {
if (conn.streams.find(stream => stream.direction === 'outbound' && stream.protocol != null && this.multicodecs.includes(stream.protocol)) != null) {
log('outbound pubsub streams already present on connection from %p', peerId)
return
}

Expand Down Expand Up @@ -533,8 +534,14 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
sendRpc (peer: PeerId, rpc: PubSubRPC): void {
const peerStreams = this.peers.get(peer)

if (peerStreams == null || !peerStreams.isWritable) {
log.error('Cannot send RPC to %p as there is no open stream to it available', peer)
if (peerStreams == null) {
log.error('Cannot send RPC to %p as there are no streams to it available', peer)

return
}

if (!peerStreams.isWritable) {
log.error('Cannot send RPC to %p as there is no outbound stream to it available', peer)

return
}
Expand Down

0 comments on commit 16a8707

Please sign in to comment.