MetaMask / metamask-extension

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

[Bug]: Network changed events fired with a delay #13375

Open SpasiboKojima opened 2 years ago

SpasiboKojima commented 2 years ago

Describe the bug

I hope I'm at the right place because it doesn't seem much of a bug, but maybe something can be done about it. I'm connecting Metamask to my app, and right after user grants access I check the chainId, if it's not the one I expect I do wallet_switchEthereumChain, and if right after that I do login process again where this time it passes chainId check and subscriptions happen, and after going this far MM emits events disconnect or/and accountsChanged. Alternatively, without auto-login after chain switch if you click fast enough on login button again after network switch you'll catch this events. BUT it doesn't happen if the target chain is mainnet, in my case it's Binance Smart Chain and it's Testnet that I'm testing. Again, events related to network switch fire much later after it actually happened

Steps to reproduce

  1. Try to login into app with wrong network
  2. Have a wallet_switchEthereumChain request asking you to change network to the right one
  3. Click on login button again within 1.5 seconds or automatically try to connect through web3
  4. Have chainId check passed but still get an event related to changing networks much later

Error messages or log output

No response

Version

10.8.1

Build type

No response

Browser

Chrome, Firefox

Operating system

Linux

Hardware wallet

No response

Additional context

No response

mloit commented 2 years ago

I believe I'm seeing a related issue. Metamask (injected provider) disconnects whenever the chain is changed to the Binance Test Network. I can freely switch from it to any network, or between any other networks I've tried, but as soon as I switch back into the binance testnet instead of getting a chainChanged event, I get a disconnect event.

main.js:450 onChainChanged: 0x38
main.js:450 onChainChanged: 0x3
main.js:450 onChainChanged: 0x4
main.js:468 onDisconnect: Error: MetaMask: Disconnected from chain. Attempting to connect.
    at l._handleDisconnect (inpage.js:1:39598)
    at l._handleDisconnect (inpage.js:1:43404)
    at l._handleChainChanged (inpage.js:1:40205)
    at l._handleChainChanged (inpage.js:1:45320)
    at o.<anonymous> (inpage.js:1:37771)
    at i (inpage.js:1:54958)
    at o.emit (inpage.js:1:55498)
    at inpage.js:1:54022
    at f.write [as _write] (inpage.js:1:54045)
    at w (inpage.js:17:29414)

If I reload the page, I am connected just fine. It only breaks when I switch into the testnet from metamask.

ErnoW commented 2 years ago

I can confirm that this is an issue with some Binance Smartchain Testnet RPCs.

When switching from any chain to Binance Smartchain Testnet, for some RPCs a disconnect event is fired. (and results in a delay)

This RPC results in the above error MetaMask: Disconnected from chain. Attempting to connect, but succeeds after the retry:

https://data-seed-prebsc-1-s1.binance.org:8545

This one seems to work without issues:

https://data-seed-prebsc-2-s3.binance.org:8545

Both are from the binance docs

tmm commented 2 years ago

Can confirm this happens for other chains and is affecting wagmi library users.

To reproduce:

  1. Open CodeSandbox (https://vwngww.csb.app) and connect wallet
  2. Switch to any non MM default chain (Arbitrum One, Polygon Mainnet, etc.)

Using MM 10.9.3. cc @kevinghim

vm06007 commented 2 years ago

same issue observed, any solution?

tmm commented 2 years ago

@kevinghim here's a very simple reproduction (using MetaMask 10.11.3) that you can run in the browser console. Let me know if you need any more info.

  1. Add event listener for disconnect event
window.ethereum.on('disconnect', () => alert('disconnect'));
  1. Connect account
await window.ethereum.request({ method: 'eth_requestAccounts' });
  1. Using a custom chain (i.e. Optimism), switch:
await window.ethereum.request({
  method: 'wallet_switchEthereumChain',
  params: [{ chainId: '0xa' }],
})

Or add and switch:

await window.ethereum.request({
  method: 'wallet_addEthereumChain',
  params: [
    {
      chainId: '0xa',
      chainName: 'Optimism',
      nativeCurrency: {name: 'Ether', symbol: 'ETH', decimals: 18},
      rpcUrls: ['https://mainnet.optimism.io'],
      blockExplorerUrls: ['https://optimistic.etherscan.io'],
    },
  ],
});
  1. After a successfully switching to the custom chain, disconnect event fires and browser alert is created.
vm06007 commented 2 years ago

is this still an issue or has been fixed?

mqklin commented 2 years ago

I have the same problem. Switching to custom network sometimes fires disconnect event.

einaralex commented 1 year ago

I've been having this issue for a while, that Metamask will disconnect when I switch to Mumbai.

I was certain this was a Metamask issue but found out today that I was missing an await, causing the changeChain event to be interrupted.

elias-garcia commented 1 year ago

Same problem here, we are experiencing this issue when switching from Goerli to another network... Is this planned to be fixed?

einaralex commented 1 year ago

@elias-garcia What other network? Ropsten just very recently got deprecated, could that be the issue?

elias-garcia commented 1 year ago

@elias-garcia What other network? Ropsten just very recently got deprecated, could that be the issue?

Hi @einaralex! We are experiencing it when switching from Goerli to the zkEVM Testnet in our bridge.

This happened live in Jordi's presentation at Devcon, you can take a look to the exact moment here:

https://www.youtube.com/watch?v=3A-LotQjc2k&t=5873s

You can see how after switching the network, the onDisconnect event is being fired and it's kicking out the user to the login page. The problem here is that we've not been able to reproduce the bug in a deterministic way yet. Looks like it's happening randomly, sometimes it doesn't fire, sometimes it fires.

Details of the zkEVM Testnet network just in case it's helpful for something:

RPC URL: https://public.zkevm-test.net:2083
Chain ID: 1402
Currency symbol: ETH
platschi commented 1 year ago

+1, we are experiencing the same issue when switching between Ethereum Mainnet and Optimism Mainnet.

webdott commented 1 year ago

Experiencing the same issue switching from Polygon to Celo and vice versa

turbowizardry commented 1 year ago

It seems like this bug is hit every time you switch to a custom network. When I switch between the default Ethereum network loaded in MM and testnets, no disconnect happens. I added a custom Ethereum node with Alchemy as the RPC and now the Dapp disconnects when I switch to it, as well as any other custom network I've added (Optimism, ZKSync etc).

0xhashmap commented 1 year ago

Has anyone found a solution/workaround for this issue?

istankovic commented 1 year ago

Bump.

0xflumedev commented 1 year ago

Be fantastic to get this fixed. Still occurring today.

aiiiden commented 1 year ago

Any Update?

KarthickSakthi commented 1 year ago

is this issue fixed? still occuring for me!

michecode commented 1 year ago

This is still broken for me but I was able to implement a workaround. I'm using web3modal but I assume you can just put this in whatever function handles your disconnect event.

const handleDisconnect = (error) => {
  // @DEV: see https://github.com/MetaMask/metamask-extension/issues/13375
  // 1013 indicates that MetaMask is attempting to reestablish the connection
  // https://github.com/MetaMask/providers/releases/tag/v8.0.0
  if (error.code === 1013) {
    console.warn('MetaMask logged connection error 1013: "Try again later"');
      return;
    }
  _disconnectWeb3(dispatch);
};

Stole this from https://github.com/Uniswap/web3-react/issues/709 thank you @niZmosis

Charlie85270 commented 1 year ago

+1, still have the issue

wwhchung commented 1 year ago

Confirmed that this is still an issue. disconnect events are erroneously being emitted when chain switch happens. In our case, because we respect this event, we disconnect an authenticated account.

Matko95 commented 1 year ago

+1, still reproducible

D-Laur commented 11 months ago

+1, issue still exits

xinnanyemm commented 10 months ago

Please note that label:team-dapp-api has been replaced by label:team-devex . Thanks.

DarkProtocol commented 4 weeks ago

+1 have same issue