MetaMask / metamask-extension

:globe_with_meridians: :electric_plug: The MetaMask browser extension enables browsing Ethereum blockchain enabled websites
https://metamask.io
Other
11.85k stars 4.84k forks source link

[Bug]: Breaking DApps - Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist. #13465

Open hamzasharif opened 2 years ago

hamzasharif commented 2 years ago

Describe the bug

Hey there,

I've noticed every time I close Chrome and re-open then go to a page that interacts with the MetaMask extension it breaks the sites integrations.

See the following console output

Screenshot 2022-01-28 at 21 24 30

:

I can consistently replicate this issue. It happens on any site that interacts with MetaMask. Is there a fix or a workaround for this?

Steps to reproduce

Setup:

  1. Close Chrome
  2. Re-open Chrome
  3. Once the browser has opened, go to any site/dapp that interacts with MetaMask e.g. https://nftcontracts.art/arcadelife
  4. Check the developer console for error messages

Error messages or log output

Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
contentscript.js:1 MetaMask: Content script lost connection to "MetaMask Background Multiplex". undefined
contentscript.js:1 MetaMask: Muxed traffic for channel "metamask-provider" failed. undefined
contentscript.js:1 MetaMask: Content script lost connection to "MetaMask Background Legacy Multiplex". undefined
contentscript.js:1 MetaMask: Muxed traffic between channels "provider" and "metamask-provider" failed. undefined
contentscript.js:1 MetaMask: Muxed traffic for channel "publicConfig" failed. undefined

Version

10.8.1

Build type

No response

Browser

Chrome

Operating system

MacOS

Hardware wallet

No response

Additional context

No response

kevinghim commented 2 years ago

@hamzasharif tried reproducing using the same OS/Browser combo, and not getting the same errors. Can you provide more details.

j-finance commented 2 years ago

This is also happening to me. I tried uninstalling and reinstalling metamask, but the problem persists.

j-finance commented 2 years ago

Related? https://github.com/MetaMask/metamask-extension/issues/9407

hamzasharif commented 2 years ago

Related? #9407

Yes it is, I can replicate this issue on multiple machines. Each with different OS versions (Mac OS).

@kevinghim what kind of info would help with replicating this?

  1. Close Chrome completely
  2. Open Chrome
  3. Navigate to a DApp (an example in the issue description)
  4. You’ll see an error in the dev console (if you went to the site in the description - If you went to another site just ensure you’re interacting with web3)

Thank you!

monsterbitar commented 2 years ago

I've replicated this in firefox on my end. The problem is timing based, if I open my browser and wait for 30s before I go to the first page, I do not get this issue.

My best guess is that there's some point during plugin initialization where if a request is asked of the plugin it has an error due to not being properly initialized but catches that error and prints to the console instead of letting the error propagate.

One solution would be to provide a new function isReady() that provides a promise that resolves only when metamask has finished initializing and is ready to be used, such that web app developers can wait for that promise to resolve before making any requets to metamask.

erik0612 commented 2 years ago

Hi Everyone, Is there any solution for this problem. I tried to reload app until it's initialized. it's working on chrome and firefox. but not working on metamask browser on the phone.

LJNGDAHL commented 2 years ago

I can confirm that what @monsterbitar describes can also be replicated in Chrome, even when trying out the getting started guide in the MetaMask Docs. Waiting for something like 30s before going to the page works so it definitely seems to be timing based.

5hanth commented 2 years ago

this can happen if you have two dApps in different tabs with different provider connected

Ravenink commented 2 years ago

It happens to me too. I would love to have a solution.

Kstar0722 commented 2 years ago

I found temporary solution. the solution is just reload app until ethereum._state.initialized is true

erik0612 commented 2 years ago

I found temporary solution. the solution is just reload app until ethereum._state.initialized is true

yeah, it works for me as well.

lobotomoe commented 2 years ago

Any update on this?

bbelly commented 2 years ago

Any update on this?

eliomain commented 2 years ago

ethereum._state.initialized is a totally temp solution. but everytimes reload the website, this state "ethereum._state.initialized" is false. This issue cannot to work better. I think this issue will be fix.

axell9641 commented 2 years ago

Any news?

dievardump commented 2 years ago

Hey there, is there any news on this?

Since today, my MetaMask (Firefox extension) shows a completely blank screen (both popup and home.html)

And any tab opened display those errors in the console:

MetaMask: Content script lost connection to "MetaMask Background Multiplex". undefined MetaMask: Muxed traffic for channel "metamask-provider" failed. undefined MetaMask: Content script lost connection to "MetaMask Background Legacy Multiplex". undefined MetaMask: Muxed traffic between channels "provider" and "metamask-provider" failed. undefined MetaMask: Muxed traffic for channel "publicConfig" failed. undefined

Dope-Ass-Ninja commented 2 years ago

I am also able to reproduce this issue on Firefox and Chromium based browsers with the MetaMask extension.

When closing out of the browser completely (tabs and all), and reopening directly to a dApp (say you selected the link from discord, or are testing in a local dev environment which opens the browser to that page for you), MetaMask will consistently throw the above errors and will not work until you refresh the page.

This problem exists not only on my dApps, but very popular ones as well such as UniSwap.

This is quite a breaking issue for front-end developers trying to create a smooth experience for users. An option to refresh the page once MetaMask has loaded or have a function IsReady() as someone has suggested above would definitely help with this issue.

AgreenCabTree commented 2 years ago

I solved that issue using this code. var timerID = setInterval(function() { if (document. readyState === 'complete') { if (ethereum._state.initialized == true) { clearInterval(timerID); } else { location.href = location.href; } } }, 1000);