Skip to content

platform-wallet: wallet state guards panic when the wallet was removed #5738

Open

Description

@xdustinface

Defect

WalletStateReadGuard and WalletStateWriteGuard in packages/rs-platform-wallet/src/wallet/platform_wallet.rs look the wallet up again on every Deref, DerefMut and wallet() call, and each lookup ends in .expect("wallet exists in guard"). The guard only holds the WalletManager lock. It does not pin the wallet. A PlatformWallet handle can outlive the wallet's removal from its manager, for example while a caller is resolving a mnemonic through the FFI resolver. Once that happens, every state(), state_mut() and state_blocking() caller panics instead of returning an error. Across the FFI that is an abort of the host app.

Evidence

  • origin/v5.1-dev: platform_wallet.rs, the five expect("wallet exists in guard") calls in the WalletStateReadGuard and WalletStateWriteGuard impls.
  • #5727 works around this for derive_core_address_private_key alone, by replacing state_blocking() with a direct get_wallet_and_info lookup that returns PlatformWalletError::WalletNotFound. Its test should_report_a_removed_wallet_as_missing removes a wallet while holding its PlatformWallet and shows the reveal would otherwise panic. Every other guard user still has the panic.

Fix

Make the guard fallible at construction. state(), state_mut() and state_blocking() (and the try_ variants) check that the wallet and its info exist while they hold the lock and return Result<_, PlatformWalletError::WalletNotFound>. The guard then stores the checked references, or keeps the invariant by construction, so Deref and wallet() cannot fail. Callers propagate the error with ?.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions