ipfs / js-ipfs

IPFS implementation in JavaScript
https://js.ipfs.tech
Other
7.44k stars 1.25k forks source link

Multiple tabs in js-ipfs 0.49 #3240

Closed aphelionz closed 4 years ago

aphelionz commented 4 years ago

Severity: High

Description:

In OrbitDB we have a test that verifies that database state is consistent across tabs in the same browser. Upon upgrading to js-ipfs 0.49, everything seems to work, except that test throws this error:

[Error: Error: Key 'self' already exists
    at e.exports.importPeer (file:///home/mark/Documents/Projects/OrbitDB/orbit-db/test/browser/ipfs.js:2:1244502)
    at async R.loadKeychain (file:///home/mark/Documents/Projects/OrbitDB/orbit-db/test/browser/ipfs.js:2:1211128)
    at async file:///home/mark/Documents/Projects/OrbitDB/orbit-db/test/browser/ipfs.js:2:1031537
    at async Proxy.<anonymous> (file:///home/mark/Documents/Projects/OrbitDB/orbit-db/test/browser/ipfs.js:2:1031244)
    at async Object.create (file:///home/mark/Documents/Projects/OrbitDB/orbit-db/test/browser/ipfs.js:2:717328)
    at async HTMLDocument.<anonymous> (file:///home/mark/Documents/Projects/OrbitDB/orbit-db/test/browser/index.html:27:22)]
      1) "before all" hook for "syncLocal option - Multiple tabs converge to same log"

Steps to reproduce the error:

I made a PR that "fixes" it by reducing the number of tabs in the test from 3 to 2. More of a workaround since I couldn't get to the bottom of this in the afternoon I spent on it. I'm gonna keep investigating but any advice you have would be great!

aphelionz commented 4 years ago

Update: I added a delay of 500ms between opening tabs in the puppeteer script and that seems to have helped.

achingbrain commented 4 years ago

If each tab is on the same origin, each tab will try initialise the repo simultaneously which will lead to race conditions.

I'd either:

a. wait for the first tab to finish the initialisation before opening other tabs, though this may cause other subtle bugs as each tab will maintain it's own network connections but share a peer ID

b. switch to the shared-worker IPFS node (though be aware it's got a very limited API at present) - see the sharing a node across tabs example

aphelionz commented 4 years ago

Makes sense! I think we're good then since I introduced the delay. I am hacking on the shared-worker now and will report back soon :+1: but since pubsub isnt yet an option there we can't rely on that for OrbitDB