Skip to content

fix: retry wallet connect on WalletNotSelectedError to fix first-select race - #4

Open
SAY-5 wants to merge 1 commit into
Cypher-CP0:mainfrom
SAY-5:fix/wallet-connect-retry-on-first-select
Open

fix: retry wallet connect on WalletNotSelectedError to fix first-select race#4
SAY-5 wants to merge 1 commit into
Cypher-CP0:mainfrom
SAY-5:fix/wallet-connect-retry-on-first-select

Conversation

@SAY-5

@SAY-5 SAY-5 commented Apr 28, 2026

Copy link
Copy Markdown

Summary

Closes #2.

`select(walletName)` triggers a React state update; the auto-connect
`useEffect` then sees `wallet` truthy and calls `connect()` from
`useWallet()`. On the first selection after the modal mounts,
the @solana/wallet-adapter-react WalletContext has not always finished
registering the underlying adapter by the time this effect re-runs,
so `connect()` throws `WalletNotSelectedError` and the wallet
popup never opens. Clicking again succeeds because the second pass
sees a fully-registered adapter.

Fix

Wrap the auto-connect in a small async helper that retries once
after 200ms
when, and only when, the failure is
`WalletNotSelectedError`. Other errors, user rejection, timeout,
session-expired, propagate to `setError` unchanged.

The effect's cleanup cancels both an in-flight retry timer and any
post-`await` state writes, so a re-mount or wallet switch during
the retry window doesn't leak a state update or a stray retry call.

The original code's behaviour for the happy path (`connect`
resolves on first try) and for non-race errors is preserved
verbatim, the retry only fires for `WalletNotSelectedError` and
only once per selection.

Test plan

  • `npm run build` (rollup) succeeds with the change applied.
  • Manual smoke test in a real browser with a Solana wallet
    extension installed (would confirm the popup opens on first
    select). I don't have a desktop Solana wallet installed in
    this environment, so I'm relying on review for the runtime
    check.

Notes on alternatives considered

The issue body lists three options. I went with the retry approach
(option 3) because:

  • Option 1 (ref instead of state) needs a wider refactor of the
    connecting flag and is more invasive.
  • Option 2 (`wallet?.adapter.connect()` directly) bypasses the
    WalletContext layer and risks the context's internal state going
    out of sync with the adapter's connected state for downstream
    hooks (`publicKey`, `sendTransaction`).
  • Option 3 is a localised, explicit, single-retry guard against a
    known race; it leaves all other paths untouched and is easy to
    remove later if @solana/wallet-adapter-react fixes the race
    upstream.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@SAY-5
SAY-5 force-pushed the fix/wallet-connect-retry-on-first-select branch from 07a68d7 to 7bf6c21 Compare August 16, 2026 09:54
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.

fix: wallet connection unreliable on first select —> WalletNotSelectedError

1 participant