Connection closes during bitswap fetches

I’ve replicated this locally, thanks so much for the detailed repro steps.

From what I can see, you’ve got browser nodes connected to each other through a relay to perform the sync.

As of libp2p@0.43.x the relay implementation is Circuit Relay v2 - this is a little different to v1 in that all connections are limited in time/data.

The idea here is that relays should be cheap to run and turned on by default - this makes every publicly diallable node on the network usable to coordinate NAT hole punching, to help browsers dial each other, etc, but you don’t want to allow peers to transfer unlimited amounts of data forever as that would be rife for abuse.

In the test the browser nodes are transferring all data via the relay which trips the data limit and the connection is closed.

You have a couple of options:

1. Turn off relay limits

This restores the behaviour of circuit relay v1 and is not advised for production deployment, but it’s ok for testing.

circuitRelayServer({
  reservations: {
    applyDefaultLimit: false
  }
})

Unfortunately there’s actually a bug around this config setting which will be fixed in the next release, so for now you should increase the data limit instead:

circuitRelayServer({
  reservations: {
    defaultDataLimit: BigInt(1024 * 1024 * 1024) // or just something big
  }
})

2. Use a direct connection between browsers

The reason it works in node.js and not browsers is node.js nodes can dial each other directly so there are no limits.

To accomplish something similar in browsers you should get them to connect via WebRTC - there’s an example of how to get this working in browsers here.

2 Likes