Skip to content

swift-sdk: keep the signer alive with withExtendedLifetime in every wrapper that passes its handle to the FFI #5939

Open

Description

@romchornyi

Expected Behavior

Every SwiftDashSDK wrapper that passes signer.handle to the FFI guarantees on its own that the signer outlives the call. A caller does not need its own keepalive.

Current Behavior

KeychainSigner registers its callback context with Unmanaged.passUnretained(self), so the Swift object must stay alive until the native call returns. The wrappers ensure this in two different ways:

  • Bare keepalive. 20 wrappers read signer.handle and then write _ = signer as the first line of their Task.detached closure. This covers transferCredits, transferCreditsToAddresses, withdrawCredits and topUpFromAddresses, and the token actions:

    • 8 in ManagedPlatformWallet.swift
    • 12 in Tokens/TokenActions.swift

    KeychainSigner.swift#L66-L76 documents this pattern.

  • withExtendedLifetime. Other wrappers wrap the FFI call in withExtendedLifetime(signer), for example in DpnsMarketplace.swift and ManagedPlatformAddressWallet.swift.

Swift guarantees an object's lifetime only up to its last use. The language promises nothing about _ = signer at the start of a closure. In practice the closure context retains the signer until the task ends, but only withExtendedLifetime states this guarantee. Because the wrappers do not provide it, apps add their own keepalives, as dashpay/dashwallet-ios#1407 does around the identity-credit calls.

The same counts hold on v5.1-dev.

Possible Solution

Wrap the FFI call in withExtendedLifetime(signer) inside each detached closure, then drop the bare _ = signer and update the KeychainSigner doc. A shared helper (signer.withHandle { handle in … }) would leave a new wrapper only one way to do this. There is no behaviour change.

Context

Raised by the review of dashpay/dashwallet-ios#1407: "Identity FFI execution and signer lifetime remain caller conventions". After this lands, the app-side keepalive in LiveIdentityCreditsSend can go.

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