metamask-extension icon indicating copy to clipboard operation
metamask-extension copied to clipboard

[Bug]: Network changed events fired with a delay

Open SpasiboKojima opened this issue 3 years ago • 8 comments

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

SpasiboKojima avatar Jan 21 '22 15:01 SpasiboKojima

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.

mloit avatar Feb 02 '22 07:02 mloit

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

ErnoW avatar Feb 04 '22 20:02 ErnoW

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

tmm avatar Feb 19 '22 23:02 tmm

same issue observed, any solution?

vm06007 avatar Mar 21 '22 18:03 vm06007

@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.

tmm avatar Apr 04 '22 01:04 tmm

is this still an issue or has been fixed?

vm06007 avatar Apr 04 '22 04:04 vm06007

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

mqklin avatar May 18 '22 13:05 mqklin

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.

einaralex avatar Aug 04 '22 12:08 einaralex

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

elias-garcia avatar Oct 20 '22 09:10 elias-garcia

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

einaralex avatar Oct 20 '22 10:10 einaralex

@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

elias-garcia avatar Oct 20 '22 11:10 elias-garcia

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

platschi avatar Oct 21 '22 14:10 platschi

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

webdott avatar Oct 22 '22 04:10 webdott

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).

turbowizardry avatar Oct 24 '22 02:10 turbowizardry

Has anyone found a solution/workaround for this issue?

0xhashmap avatar Nov 18 '22 22:11 0xhashmap

Bump.

istankovic avatar Jan 11 '23 15:01 istankovic

Be fantastic to get this fixed. Still occurring today.

0xflumedev avatar Jan 17 '23 02:01 0xflumedev

Any Update?

aiiiden avatar Feb 21 '23 19:02 aiiiden

is this issue fixed? still occuring for me!

KarthickSakthi avatar Mar 09 '23 01:03 KarthickSakthi

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

michecode avatar Mar 27 '23 22:03 michecode

+1, still have the issue

Charlie85270 avatar Apr 15 '23 14:04 Charlie85270

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.

wwhchung avatar May 05 '23 00:05 wwhchung

+1, still reproducible

Matko95 avatar Jul 03 '23 14:07 Matko95

+1, issue still exits

D-Laur avatar Aug 18 '23 12:08 D-Laur

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

xinnanyemm avatar Sep 15 '23 09:09 xinnanyemm