From 2df354f0bb326e076dc990f67051eeb3a366de75 Mon Sep 17 00:00:00 2001 From: romchornyi Date: Wed, 11 Nov 2026 14:04:52 +0200 Subject: [PATCH 1/3] fix(swift-sdk): run identityGet and dpnsGetUsername off the main actor Both were @MainActor and called synchronous FFI exports that block on the network fetch, so an app awaiting them froze its UI for the whole round trip, timeouts included. They are now nonisolated and run the blocking call through performBlockingQuery, as documentList does; their signatures stay async throws. Co-Authored-By: Claude Opus 5.5 --- .../FFI/PlatformQueryExtensions.swift | 56 +++++++++++-------- .../PlatformQueryOffMainTests.swift | 42 ++++++++++++++ 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/FFI/PlatformQueryExtensions.swift b/packages/swift-sdk/Sources/SwiftDashSDK/FFI/PlatformQueryExtensions.swift index 19cec7a9e48..34269d26f1b 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/FFI/PlatformQueryExtensions.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/FFI/PlatformQueryExtensions.swift @@ -217,21 +217,15 @@ extension SDK { // MARK: - Identity Queries /// Get an identity by ID - @MainActor - public func identityGet(identityId: String) async throws -> [String: Any] { - print("🔵 SDK.identityGet: Called with ID: \(identityId)") - - guard let sdkHandle = handle else { - print("❌ SDK.identityGet: SDK handle is nil") - throw SDKError.invalidState("SDK not initialized") + /// + /// `nonisolated`: the fetch blocks on the network inside Rust, so it runs + /// on the shared Platform query queue (see ``performBlockingQuery(_:)``) + /// instead of on the caller's actor. + public nonisolated func identityGet(identityId: String) async throws -> sending [String: Any] { + let json = try await performBlockingQuery { handle in + try Self.consumeStringResult(dash_sdk_identity_fetch(handle, identityId)) } - - print("🔵 SDK.identityGet: SDK handle exists: \(sdkHandle)") - print("🔵 SDK.identityGet: About to call dash_sdk_identity_fetch with handle and ID: \(identityId)") - - // Make the FFI call directly and return the parsed JSON - let result = dash_sdk_identity_fetch(sdkHandle, identityId) - return try processJSONResult(result) + return try Self.parseJSONObject(json) } /// Get identity keys @@ -1124,16 +1118,23 @@ extension SDK { // MARK: - DPNS Queries /// Get DPNS usernames for identity - @MainActor - public func dpnsGetUsername(identityId: String, limit: UInt32?) async throws -> [[String: Any]] { - guard let handle = handle else { - throw SDKError.invalidState("SDK not initialized") + /// + /// `nonisolated`: the lookup blocks on the network inside Rust, so it runs + /// on the shared Platform query queue (see ``performBlockingQuery(_:)``) + /// instead of on the caller's actor. + public nonisolated func dpnsGetUsername(identityId: String, limit: UInt32?) async throws -> sending [[String: Any]] { + let json: String? = try await performBlockingQuery { handle in + let result = dash_sdk_dpns_get_usernames(handle, identityId, limit ?? 10) + // No data and no error means no usernames. + if result.error == nil && result.data == nil { + return nil + } + return try Self.consumeStringResult(result) } - - // Call native FFI function with identity ID as string - let result = dash_sdk_dpns_get_usernames(handle, identityId, limit ?? 10) - - return try processJSONArrayResult(result) + guard let json else { + return [] + } + return try Self.parseJSONArray(json) } /// Check DPNS name availability @@ -2425,4 +2426,13 @@ extension SDK { } return object } + + /// Parse a JSON array payload returned by a query. + fileprivate nonisolated static func parseJSONArray(_ json: String) throws -> sending [[String: Any]] { + guard let data = json.data(using: .utf8), + let array = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] else { + throw SDKError.serializationError("Failed to parse JSON array") + } + return array + } } diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift index 9a58b42448a..d5e5f94e53b 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift @@ -1,4 +1,5 @@ import Foundation +import os import XCTest @testable import SwiftDashSDK @@ -45,6 +46,47 @@ final class PlatformQueryOffMainTests: XCTestCase { XCTAssertTrue(released, "the parked query was not released by the main actor") } + /// `identityGet` and `dpnsGetUsername` make their FFI call without the + /// main actor: the main thread is held for the whole of both calls, and + /// each still returns (with the mock's missing-expectation error) before it + /// is released. A call that needed the main actor would wait until the hold + /// timed out. + func testIdentityReadsDoNotNeedTheMainActor() async throws { + let sdk = try SDK(mockVectorsDirectory: nil) + let mainHeld = DispatchSemaphore(value: 0) + let releaseMain = DispatchSemaphore(value: 0) + let holdTimedOut = OSAllocatedUnfairLock(initialState: false) + + DispatchQueue.main.async { + mainHeld.signal() + if releaseMain.wait(timeout: .now() + 5) == .timedOut { + holdTimedOut.withLock { $0 = true } + } + } + await withCheckedContinuation { continuation in + DispatchQueue.global().async { + mainHeld.wait() + continuation.resume() + } + } + + do { + _ = try await sdk.identityGet(identityId: "5DbLwAxGBzUzo81VewMUwn4b5P4bpv9FNFybi25XB5Bk") + XCTFail("identityGet should fail without recorded responses") + } catch {} + + do { + _ = try await sdk.dpnsGetUsername( + identityId: "5DbLwAxGBzUzo81VewMUwn4b5P4bpv9FNFybi25XB5Bk", limit: 1) + XCTFail("dpnsGetUsername should fail without recorded responses") + } catch {} + + let returnedWhileMainWasHeld = !holdTimedOut.withLock { $0 } + releaseMain.signal() + XCTAssertTrue( + returnedWhileMainWasHeld, "an identity read waited for the main actor") + } + /// The off-main entry points marshal their arguments, make the FFI call and /// surface its error from a main-actor caller. The mock has no recorded /// responses, so each one must fail rather than return data. From 5d99479c7bb2291270886c70ed456a1f908ea0d6 Mon Sep 17 00:00:00 2001 From: romchornyi Date: Wed, 11 Nov 2026 16:31:09 +0200 Subject: [PATCH 2/3] test(swift-sdk): fail the off-main identity reads test when a read never reaches the FFI Co-Authored-By: Claude Opus 5.5 --- .../SwiftDashSDKTests/PlatformQueryOffMainTests.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift index d5e5f94e53b..e141294c83f 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift @@ -73,12 +73,16 @@ final class PlatformQueryOffMainTests: XCTestCase { do { _ = try await sdk.identityGet(identityId: "5DbLwAxGBzUzo81VewMUwn4b5P4bpv9FNFybi25XB5Bk") XCTFail("identityGet should fail without recorded responses") + } catch SDKError.invalidState { + XCTFail("identityGet must reach the FFI, not fail on an unavailable SDK handle") } catch {} do { _ = try await sdk.dpnsGetUsername( identityId: "5DbLwAxGBzUzo81VewMUwn4b5P4bpv9FNFybi25XB5Bk", limit: 1) XCTFail("dpnsGetUsername should fail without recorded responses") + } catch SDKError.invalidState { + XCTFail("dpnsGetUsername must reach the FFI, not fail on an unavailable SDK handle") } catch {} let returnedWhileMainWasHeld = !holdTimedOut.withLock { $0 } From da4773cd9376c3bcf07a7e9fef173873760976fa Mon Sep 17 00:00:00 2001 From: romchornyi Date: Thu, 12 Nov 2026 11:42:23 +0200 Subject: [PATCH 3/3] test(swift-sdk): bound the main-thread hold handshake The wait for the main queue to start the hold had no deadline, so a host that never serviced the main queue made the test hang instead of fail. Wait at most 5 seconds, release the hold, and throw an error saying the main queue never ran it. Co-Authored-By: Claude Opus 5.5 --- .../PlatformQueryOffMainTests.swift | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift index e141294c83f..2ae4aa4681f 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PlatformQueryOffMainTests.swift @@ -46,6 +46,11 @@ final class PlatformQueryOffMainTests: XCTestCase { XCTAssertTrue(released, "the parked query was not released by the main actor") } + private struct MainHoldError: Error, CustomStringConvertible { + let message: String + var description: String { message } + } + /// `identityGet` and `dpnsGetUsername` make their FFI call without the /// main actor: the main thread is held for the whole of both calls, and /// each still returns (with the mock's missing-expectation error) before it @@ -63,10 +68,15 @@ final class PlatformQueryOffMainTests: XCTestCase { holdTimedOut.withLock { $0 = true } } } - await withCheckedContinuation { continuation in + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in DispatchQueue.global().async { - mainHeld.wait() - continuation.resume() + if mainHeld.wait(timeout: .now() + 5) == .timedOut { + releaseMain.signal() + continuation.resume( + throwing: MainHoldError(message: "the main queue never ran the hold")) + } else { + continuation.resume() + } } }