MetaMask / metamask-extension

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

[Bug]: Firefox, Browser Launch, window.ethereum isConnected = true, initialized = false #27150

Open adraffy opened 2 months ago

adraffy commented 2 months ago

Describe the bug

on browser launch, window.ethereum is frequently stuck in some limbo state where it will either already be connected or emit a connect event, yet chainChanged event will never fire.

Only reloading the page fixes the problem.

Expected behavior

After connect, initialized should be true.

Screenshots/Recordings

image

Steps to reproduce

  1. Have Metamask Installed
  2. Restart Firefox
  3. Check window.ethereum._state

Error messages or log output

No response

Detection stage

In production (default)

Version

12.0.6

Build type

None

Browser

Firefox

Operating system

Windows

Hardware wallet

No response

Additional context

No response

Severity

No response

adraffy commented 2 months ago

I'm using the following workaround, which delays interacting with the window.ethereum until it's initialized or force-reload the page after a fixed (arbitrary) timeout.

if (window.ethereum) {
    if (ethereum._state) {
        let killer = setTimeout(() => window.location.reload(), 2000);
        let monitor = setInterval(() => {
            if (ethereum._state.initialized) {
                clearInterval(monitor);
                clearTimeout(killer);
                init();
            }
        }, 100);
    } else {
        init();
    }
}
function init() {
     window_provider = new ethers.BrowserProvider(ethereum, 'any');
}