From 40bf4d9d6549d6b53283bf3311ca3ff4b6c04173 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 15 Nov 2026 19:30:54 +0700 Subject: [PATCH 1/3] fix(sdk): mobile yes/no proposal writes survive a cancelled caller and a released wallet Kotlin: the decision proposal create and edit return what Platform confirmed when their caller was cancelled while the native call ran, instead of throwing a CancellationException that hid a paid create from a caller who might then pay for it again. Swift: the two wallet calls hold the wallet for the whole FFI call, so its deinit cannot destroy the handle the call is using. Both are what #5919 did for the multiple choice calls. Co-Authored-By: Claude Fable 5.2 --- .../dashsdk/voting/DecisionProposals.kt | 11 +++++-- ...nagedPlatformWalletDecisionProposals.swift | 29 +++++++++++-------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt index 30781fbc4d5..4b5b575959e 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt @@ -7,6 +7,7 @@ import org.dashfoundation.dashsdk.ffi.DecisionProposalsNative import org.dashfoundation.dashsdk.persistence.toBase58String import org.dashfoundation.dashsdk.wallet.TeardownGate import org.dashfoundation.dashsdk.wallet.op +import org.dashfoundation.dashsdk.wallet.opReturningCompletedResult /** * Decision proposals (protocol version 15): yes/no questions any identity puts to the @@ -157,6 +158,12 @@ class DecisionProposals internal constructor(private val sdk: Sdk) { * Each call is one native call (`platform_wallet_decision_proposal_create` / `_edit`): the * wallet builds the proposal (or reads the stored one and edits it), picks the signing key, * broadcasts and waits for Platform to confirm. Nothing is decided or checked here. + * + * Cancellation: native code cannot see a coroutine's cancellation, so a write can complete + * after its caller was cancelled. Each call then still returns what Platform confirmed + * (see [opReturningCompletedResult]) instead of throwing, so a paid create is never hidden + * from a caller that would otherwise pay for it again. The coroutine stays cancelled: + * handle the result before the next suspension point. */ class DecisionProposalTransactions internal constructor(private val gate: TeardownGate?) { @@ -175,7 +182,7 @@ class DecisionProposalTransactions internal constructor(private val gate: Teardo ownerId: ByteArray, params: DecisionProposalParams, signerHandle: Long, - ): CreatedDecisionProposal = gate.op { + ): CreatedDecisionProposal = gate.opReturningCompletedResult { val proposalId = ByteArray(PROPOSAL_ID_SIZE) val documentJson = mapNativeErrors { DecisionProposalsNative.create( @@ -214,7 +221,7 @@ class DecisionProposalTransactions internal constructor(private val gate: Teardo proposalId: ByteArray, edit: DecisionProposalEdit, signerHandle: Long, - ): String = gate.op { + ): String = gate.opReturningCompletedResult { val url = edit.url.toNative() mapNativeErrors { DecisionProposalsNative.edit( diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/ManagedPlatformWalletDecisionProposals.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/ManagedPlatformWalletDecisionProposals.swift index 2867ecfa838..e174db5122a 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/ManagedPlatformWalletDecisionProposals.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/ManagedPlatformWalletDecisionProposals.swift @@ -30,7 +30,8 @@ extension ManagedPlatformWallet { /// /// Lifetime contract: as `createDocument`, the `signer` is pinned with /// `withExtendedLifetime` across the synchronous FFI call (Rust holds a - /// `passUnretained` ctx pointer to it). + /// `passUnretained` ctx pointer to it), and so is the wallet, whose + /// deinit destroys the handle the call uses. /// /// - Parameters: /// - ownerIdentityId: The proposer's 32-byte identity id. @@ -51,18 +52,20 @@ extension ManagedPlatformWallet { let handle = self.handle let signerHandle = signer.handle let ownerBytes = [UInt8](ownerIdentityId) - return try await Task.detached(priority: .userInitiated) { + return try await Task.detached(priority: .userInitiated) { [self] in var proposalIdBytes = [UInt8](repeating: 0, count: 32) // Receives an owned canonical-document JSON C string on success; // freed with `platform_wallet_string_free` below. var documentJsonPtr: UnsafeMutablePointer? = nil defer { if let p = documentJsonPtr { platform_wallet_string_free(p) } } - // Pin `signer` for the whole FFI call (see `createDocument` for - // why a bare `_ = signer` is unreliable under -O). Rust reads the - // C strings synchronously inside the call, so the - // `withDecisionProposalCString` scopes are long enough. - let result = try withExtendedLifetime(signer) { + // Pin the wallet and `signer` for the whole FFI call (see + // `createDocument` for why a bare `_ = signer` is unreliable + // under -O): a wallet deinit mid-call would destroy the handle + // the FFI is still using. Rust reads the C strings synchronously + // inside the call, so the `withDecisionProposalCString` scopes + // are long enough. + let result = try withExtendedLifetime((self, signer)) { try ownerBytes.withUnsafeBufferPointer { ownerBp -> PlatformWalletFFIResult in try withDecisionProposalCString( params.title, field: "title", invalid: PlatformWalletError.invalidParameter @@ -128,8 +131,9 @@ extension ManagedPlatformWallet { /// refuses an edit more than a day after the proposal was created (or a /// fifth of the time it runs, if that is shorter). /// - /// Lifetime contract: as `createDocument`, the `signer` is pinned with - /// `withExtendedLifetime` across the synchronous FFI call. + /// Lifetime contract: the wallet and the `signer` are pinned with + /// `withExtendedLifetime` across the synchronous FFI call, as the create + /// pins them. /// /// - Parameters: /// - ownerIdentityId: The proposer's 32-byte identity id. @@ -173,12 +177,13 @@ extension ManagedPlatformWallet { url = nil removeURL = true } - return try await Task.detached(priority: .userInitiated) { + return try await Task.detached(priority: .userInitiated) { [self] in var documentJsonPtr: UnsafeMutablePointer? = nil defer { if let p = documentJsonPtr { platform_wallet_string_free(p) } } - // Pin `signer` for the whole FFI call (see `createDocument`). - let result = try withExtendedLifetime(signer) { + // Pin the wallet and `signer` for the whole FFI call, as the + // create does. + let result = try withExtendedLifetime((self, signer)) { try ownerBytes.withUnsafeBufferPointer { ownerBp -> PlatformWalletFFIResult in try proposalBytes.withUnsafeBufferPointer { proposalBp -> PlatformWalletFFIResult in try withDecisionProposalCString( From d5e42022b6d8c4a71585d0e45d83d4420fb1bee3 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 15 Nov 2026 19:38:36 +0700 Subject: [PATCH 2/3] docs(sdk): the cancelled coroutine receives a completed proposal write, not its awaiters The class docs of the two Kotlin proposal write facades said a paid write is never hidden from a caller. That holds for the coroutine that made the call; code awaiting that coroutine's Job or Deferred from outside still gets a CancellationException. Co-Authored-By: Claude Fable 5.2 --- .../dashfoundation/dashsdk/voting/DecisionProposals.kt | 10 ++++++---- .../dashsdk/voting/MultipleChoiceProposals.kt | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt index 4b5b575959e..71e174c054d 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt @@ -160,10 +160,12 @@ class DecisionProposals internal constructor(private val sdk: Sdk) { * broadcasts and waits for Platform to confirm. Nothing is decided or checked here. * * Cancellation: native code cannot see a coroutine's cancellation, so a write can complete - * after its caller was cancelled. Each call then still returns what Platform confirmed - * (see [opReturningCompletedResult]) instead of throwing, so a paid create is never hidden - * from a caller that would otherwise pay for it again. The coroutine stays cancelled: - * handle the result before the next suspension point. + * after its caller was cancelled. The coroutine that made the call then still receives + * what Platform confirmed (see [opReturningCompletedResult]) instead of an exception, so it + * can record a paid create rather than pay for it again. That coroutine stays + * cancelled, so handle the result before its next suspension point: code that awaits the + * coroutine from outside (its `Job` or `Deferred`) still gets a `CancellationException` + * and never the result. */ class DecisionProposalTransactions internal constructor(private val gate: TeardownGate?) { diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/MultipleChoiceProposals.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/MultipleChoiceProposals.kt index 9c80d62c0bc..285641451ce 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/MultipleChoiceProposals.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/MultipleChoiceProposals.kt @@ -167,10 +167,12 @@ class MultipleChoiceProposals internal constructor(private val sdk: Sdk) { * decided or checked here. * * Cancellation: native code cannot see a coroutine's cancellation, so a write can complete - * after its caller was cancelled. Each call then still returns what Platform confirmed - * (see [opReturningCompletedResult]) instead of throwing, so a paid create or choice is - * never hidden from a caller that would otherwise pay for it again. The coroutine stays - * cancelled: handle the result before the next suspension point. + * after its caller was cancelled. The coroutine that made the call then still receives + * what Platform confirmed (see [opReturningCompletedResult]) instead of an exception, so it + * can record a paid create or choice rather than pay for it again. That coroutine stays + * cancelled, so handle the result before its next suspension point: code that awaits the + * coroutine from outside (its `Job` or `Deferred`) still gets a `CancellationException` + * and never the result. */ class MultipleChoiceProposalTransactions internal constructor(private val gate: TeardownGate?) { From 784aea3450eccd05a55b0941651c66f6f98613bb Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 15 Nov 2026 19:43:15 +0700 Subject: [PATCH 3/3] docs(sdk): joining a cancelled proposal write returns, only awaiting it throws The class docs said code awaiting the cancelled coroutine's Job or Deferred gets a CancellationException. That is true of Deferred.await(); Job.join() returns normally. Neither yields the result. Co-Authored-By: Claude Fable 5.2 --- .../org/dashfoundation/dashsdk/voting/DecisionProposals.kt | 6 +++--- .../dashsdk/voting/MultipleChoiceProposals.kt | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt index 71e174c054d..18be6c434dd 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/DecisionProposals.kt @@ -163,9 +163,9 @@ class DecisionProposals internal constructor(private val sdk: Sdk) { * after its caller was cancelled. The coroutine that made the call then still receives * what Platform confirmed (see [opReturningCompletedResult]) instead of an exception, so it * can record a paid create rather than pay for it again. That coroutine stays - * cancelled, so handle the result before its next suspension point: code that awaits the - * coroutine from outside (its `Job` or `Deferred`) still gets a `CancellationException` - * and never the result. + * cancelled, so handle the result before its next suspension point. Code outside the + * coroutine never gets the result: awaiting its `Deferred` throws a + * `CancellationException`, and joining its `Job` only returns. */ class DecisionProposalTransactions internal constructor(private val gate: TeardownGate?) { diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/MultipleChoiceProposals.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/MultipleChoiceProposals.kt index 285641451ce..6aee465e81e 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/MultipleChoiceProposals.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/voting/MultipleChoiceProposals.kt @@ -170,9 +170,9 @@ class MultipleChoiceProposals internal constructor(private val sdk: Sdk) { * after its caller was cancelled. The coroutine that made the call then still receives * what Platform confirmed (see [opReturningCompletedResult]) instead of an exception, so it * can record a paid create or choice rather than pay for it again. That coroutine stays - * cancelled, so handle the result before its next suspension point: code that awaits the - * coroutine from outside (its `Job` or `Deferred`) still gets a `CancellationException` - * and never the result. + * cancelled, so handle the result before its next suspension point. Code outside the + * coroutine never gets the result: awaiting its `Deferred` throws a + * `CancellationException`, and joining its `Job` only returns. */ class MultipleChoiceProposalTransactions internal constructor(private val gate: TeardownGate?) {