Skip to content

fix: resolve race conditions in wallet identity toggling and hook caching - #24

Merged
JamesEjembi merged 1 commit into
Lumina-etwork:mainfrom
JerryIdoko:fix/race-condition-wallet-identity-toggling
Jun 16, 2026
Merged

fix: resolve race conditions in wallet identity toggling and hook caching#24
JamesEjembi merged 1 commit into
Lumina-etwork:mainfrom
JerryIdoko:fix/race-condition-wallet-identity-toggling

Conversation

@JerryIdoko

Copy link
Copy Markdown
Contributor

Summary

Fixes #1 — Race conditions when rapidly switching wallet accounts in Freighter. The WalletProvider dispatches identity updates asynchronously, but downstream hooks cached stale React Query results keyed to the previous public key.

Changes

WalletProvider (src/components/providers/WalletProvider.tsx)

  • Added isTransitioning state flag that blocks query execution during identity switches
  • Listens to Freighter accountChange events and calls queryClient.cancelQueries() before fetching new identity
  • Increments a generation counter on every account change to force downstream re-renders

useWalletIdentity (src/hooks/useWalletIdentity.ts)

  • Returns { publicKey, generation, isTransitioning } from WalletContext
  • Uses useMemo for identity stability across renders
  • useWalletQueryKey prefixes query keys with the wallet public key, or returns ["wallet-blocked", ...] during transitions

useSorobanBilling / useLedgerEvents

  • Query keys now incorporate the wallet public key via useWalletQueryKey
  • Queries are disabled (enabled: false) during wallet transitions via key prefix check
  • Added useMemo deep-comparison wrappers around query keys

QueryClient (src/lib/queryClient.ts)

  • Exposes a shared walletTransitioningRef that WalletProvider updates
  • Default staleTime of 30s with retry=1

E2E Tests (e2e/wallet-toggling.spec.ts)

  • Mocks Freighter with 3 test wallets (alice, bob, carol)
  • Rapidly toggles between wallets in a loop and verifies correct public key is displayed
  • Validates no stale data persists after rapid switching

Verification

  • npm run build passes with zero errors
  • npx eslint . passes with zero errors
  • Playwright e2e tests validate the race condition fix

…hing

Implement wallet transitioning guard, identity generation counter,
cache invalidation, and deep-comparison query key wrapper.

- Add WalletProvider with transitioning state that blocks queries
- Add useWalletIdentity hook with publicKey, generation, isTransitioning
- Add useWalletQueryKey with automatic public-key-prefixed cache keys
- Wire queryClient.cancelQueries on every identity change
- Guard query execution via 'enabled' check during transitions
- Add useMemo deep-comparison around wallet-dependent query keys
- Add WalletStatusBar test helper for e2e assertions
- Write Playwright e2e test validating rapid 3-wallet toggling

Closes Lumina-etwork#1
@JamesEjembi
JamesEjembi merged commit 0e01fd4 into Lumina-etwork:main Jun 16, 2026
1 check passed
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.

Race Conditions in Asynchronous Wallet Identity Toggling and Hook Caching

2 participants