Skip to content

fix(connectors): retry failed metaMask SDK initialization - #5310

Open
hacker14398 wants to merge 1 commit into
wevm:mainfrom
hacker14398:fix/metamask-retry-init
Open

hacker14398 wants to merge 1 commit into
wevm:mainfrom
hacker14398:fix/metamask-retry-init

Conversation

@hacker14398

@hacker14398 hacker14398 commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes two problems in the metaMask connector's getInstance().

1. A failed SDK initialization is cached for the life of the page. If createEVMClient() rejects, for example from a transient storage or transport error during setup, metamaskPromise keeps the rejected promise. Every later getInstance(), getProvider() and connect() call rethrows the original error, so the connector stays broken until a reload. Because the EIP-6963 pre-connect probes check !metamaskPromise, extension users also lose the injected-provider fast path after one failed attempt.

2. Concurrent calls create more than one SDK client. metamaskPromise is only assigned after await import('@metamask/connect-evm'). So two calls that start before the import resolves both pass the !metamaskPromise check, and each calls createEVMClient(). One example is reconnect()'s getProvider() probe and a user-initiated connect() on a browser without the extension. The multichain core is a singleton, but each call creates a separate MetamaskConnectEVM that subscribes to it with the connector's eventHandlers. As a result, accountsChanged, display_uri and similar events are delivered twice.

Fix: this follows the same pattern as walletConnect's initProvider. The initialization moves into a local initInstance(). The promise is assigned synchronously, so concurrent callers share it. When it rejects, it is cleared, but only if it is still the current attempt, so the next call retries. Pending and successful initializations are still shared.

This is the MetaMask counterpart to #5293 / #5294, which fixes the same retry problem for WalletConnect only.

Most of the diff is re-indentation from moving the existing body into initInstance(). With whitespace hidden, the source change is +14/−4.

Tests (both fail on main):

  • retries SDK initialization after a failed attempt: createEVMClient rejects once, and the next getProvider() resolves instead of rethrowing.
  • creates one SDK client for concurrent calls: two concurrent getProvider() calls share a single createEVMClient() call.

Also ran the connectors test suite, tsgo --noEmit for @wagmi/connectors, and biome check.

Written with AI assistance (Claude).

`getInstance()` cached the `createEVMClient()` promise even when it
rejected, so one failed initialization left the connector failing with
the same error until the page was reloaded.

The promise was also assigned only after `await import()`, so concurrent
calls could each create a `MetamaskConnectEVM` client, with each one
registering the connector's event handlers.

Assign the initialization promise synchronously so concurrent calls
share it, and clear it when it rejects so the next call retries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R9CgQNT2zLfeaCLijemCct
@vercel

vercel Bot commented Nov 24, 2026

Copy link
Copy Markdown

@hacker14398 is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1524764

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@wagmi/connectors Patch
wagmi Patch
@wagmi/solid Patch
@wagmi/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant