From 2c996579e9c421b89ce4156a95379868095f9696 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Wed, 25 Nov 2026 12:47:20 +0100 Subject: [PATCH 1/2] fix: disconnect a profile that newer paykit data keeps from loading An identity that never restored is forgotten on this device, as on Android, and other sign-out errors show plain text. Verified: xcodebuild test (unit-tests.yml skips), swiftformat --lint --- Bitkit/Managers/PubkyProfileManager.swift | 28 ++++++++++++++++--- Bitkit/Views/Profile/EditProfileView.swift | 2 +- Bitkit/Views/Profile/ProfileView.swift | 2 +- BitkitTests/PubkyProfileManagerTests.swift | 32 ++++++++++++++++++++++ changelog.d/next/1427.fixed.md | 1 + 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 changelog.d/next/1427.fixed.md diff --git a/Bitkit/Managers/PubkyProfileManager.swift b/Bitkit/Managers/PubkyProfileManager.swift index 06845ed59..e781b0022 100644 --- a/Bitkit/Managers/PubkyProfileManager.swift +++ b/Bitkit/Managers/PubkyProfileManager.swift @@ -1630,15 +1630,35 @@ class PubkyProfileManager: ObservableObject { } private func signOut(cleanPrivatePaykitEndpoints: Bool, allowDuringProfileDeletion: Bool = false) async throws { + let isRestored = publicKey != nil try await signOut(allowDuringProfileDeletion: allowDuringProfileDeletion) { - if cleanPrivatePaykitEndpoints { - try await Self.removePrivatePaykitEndpoints(context: "PubkyProfileManager.signOut") + try await Self.signOutCleanup(isRestored: isRestored) { + if cleanPrivatePaykitEndpoints { + try await Self.removePrivatePaykitEndpoints(context: "PubkyProfileManager.signOut") + } + await Self.removePublicPaykitEndpointsBestEffort(context: "PubkyProfileManager.signOut") + try await PubkyService.signOut() } - await Self.removePublicPaykitEndpointsBestEffort(context: "PubkyProfileManager.signOut") - try await PubkyService.signOut() } } + /// An identity that never restored, such as one whose shared Paykit state a newer Bitkit saved, cannot read that state + /// to remove its endpoints or end its session, so Disconnect only forgets it on this device. A pending endpoint cleanup + /// is dropped with it, so it cannot run against the next identity. + nonisolated static func signOutCleanup( + isRestored: Bool, + forgetSessionAccess: @Sendable () async throws -> Void = { try await PubkyService.forgetSessionAccess() }, + dropPendingEndpointCleanup: @Sendable () -> Void = { PublicPaykitService.setCleanupPending(false) }, + signOutRestored: @Sendable () async throws -> Void + ) async throws { + guard isRestored else { + try await forgetSessionAccess() + dropPendingEndpointCleanup() + return + } + try await signOutRestored() + } + func signOut(performSessionCleanup: @escaping @Sendable () async throws -> Void) async throws { try await signOut(allowDuringProfileDeletion: false, performSessionCleanup: performSessionCleanup) } diff --git a/Bitkit/Views/Profile/EditProfileView.swift b/Bitkit/Views/Profile/EditProfileView.swift index 927d8d578..78dc75b1b 100644 --- a/Bitkit/Views/Profile/EditProfileView.swift +++ b/Bitkit/Views/Profile/EditProfileView.swift @@ -228,7 +228,7 @@ struct EditProfileView: View { try await pubkyProfile.signOut() navigation.path = [app.hasSeenProfileIntro ? .pubkyChoice : .profileIntro] } catch { - app.toast(type: .error, title: t("profile__sign_out_title"), description: error.localizedDescription) + app.toast(type: .error, title: t("profile__sign_out_title"), description: error.profileErrorDescription) } } diff --git a/Bitkit/Views/Profile/ProfileView.swift b/Bitkit/Views/Profile/ProfileView.swift index 68a967190..683419ed1 100644 --- a/Bitkit/Views/Profile/ProfileView.swift +++ b/Bitkit/Views/Profile/ProfileView.swift @@ -406,7 +406,7 @@ struct ProfileView: View { do { try await pubkyProfile.signOut() } catch { - app.toast(type: .error, title: t("profile__sign_out_title"), description: error.localizedDescription) + app.toast(type: .error, title: t("profile__sign_out_title"), description: error.profileErrorDescription) } isSigningOut = false } diff --git a/BitkitTests/PubkyProfileManagerTests.swift b/BitkitTests/PubkyProfileManagerTests.swift index d716caa9d..09e0f341d 100644 --- a/BitkitTests/PubkyProfileManagerTests.swift +++ b/BitkitTests/PubkyProfileManagerTests.swift @@ -1135,6 +1135,25 @@ final class PubkyProfileManagerTests: XCTestCase { XCTAssertFalse(manager.sessionRestorationFailed) } + func testDisconnectOnlyForgetsAnIdentityThatNeverRestored() async throws { + let calls = LockedCalls() + + try await PubkyProfileManager.signOutCleanup( + isRestored: false, + forgetSessionAccess: { calls.append("forget") }, + dropPendingEndpointCleanup: { calls.append("dropCleanup") }, + signOutRestored: { calls.append("signOut") } + ) + try await PubkyProfileManager.signOutCleanup( + isRestored: true, + forgetSessionAccess: { calls.append("forget") }, + dropPendingEndpointCleanup: { calls.append("dropCleanup") }, + signOutRestored: { calls.append("signOut") } + ) + + XCTAssertEqual(calls.values, ["forget", "dropCleanup", "signOut"]) + } + @MainActor func testRecoveryAsksForAnUpdateOnceAndStopsRetryingWhenPaykitStateIsNewer() async { let manager = RecoveryProfileManager() @@ -4428,3 +4447,16 @@ private actor SessionRecoveryAttempts { return count } } + +private final class LockedCalls: @unchecked Sendable { + private let lock = NSLock() + private var calls: [String] = [] + + var values: [String] { + lock.withLock { calls } + } + + func append(_ call: String) { + lock.withLock { calls.append(call) } + } +} diff --git a/changelog.d/next/1427.fixed.md b/changelog.d/next/1427.fixed.md new file mode 100644 index 000000000..81718cea2 --- /dev/null +++ b/changelog.d/next/1427.fixed.md @@ -0,0 +1 @@ +Disconnect now removes a profile that needs a newer Bitkit from this device instead of showing a storage error. From e018ea98133bde9384d503cf44b6f1c31c981e5e Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Wed, 25 Nov 2026 14:07:58 +0100 Subject: [PATCH 2/2] fix: wait for a running restore before disconnecting Verified: xcodebuild test (unit-tests.yml skips), swiftformat --lint --- Bitkit/Managers/PubkyProfileManager.swift | 32 +++++++++---- BitkitTests/PubkyProfileManagerTests.swift | 56 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 8 deletions(-) diff --git a/Bitkit/Managers/PubkyProfileManager.swift b/Bitkit/Managers/PubkyProfileManager.swift index e781b0022..e25680fd1 100644 --- a/Bitkit/Managers/PubkyProfileManager.swift +++ b/Bitkit/Managers/PubkyProfileManager.swift @@ -1630,18 +1630,34 @@ class PubkyProfileManager: ObservableObject { } private func signOut(cleanPrivatePaykitEndpoints: Bool, allowDuringProfileDeletion: Bool = false) async throws { - let isRestored = publicKey != nil - try await signOut(allowDuringProfileDeletion: allowDuringProfileDeletion) { - try await Self.signOutCleanup(isRestored: isRestored) { - if cleanPrivatePaykitEndpoints { - try await Self.removePrivatePaykitEndpoints(context: "PubkyProfileManager.signOut") - } - await Self.removePublicPaykitEndpointsBestEffort(context: "PubkyProfileManager.signOut") - try await PubkyService.signOut() + try await signOutOrForget(allowDuringProfileDeletion: allowDuringProfileDeletion) { + if cleanPrivatePaykitEndpoints { + try await Self.removePrivatePaykitEndpoints(context: "PubkyProfileManager.signOut") } + await Self.removePublicPaykitEndpointsBestEffort(context: "PubkyProfileManager.signOut") + try await PubkyService.signOut() } } + /// Waits for a restore already running, so an identity it restores is signed out rather than only forgotten. + func signOutOrForget( + allowDuringProfileDeletion: Bool, + forgetSessionAccess: @escaping @Sendable () async throws -> Void = { try await PubkyService.forgetSessionAccess() }, + signOutRestored: @escaping @Sendable () async throws -> Void + ) async throws { + while let initializationTask { + await initializationTask.value + } + let isRestored = publicKey != nil + try await signOut(allowDuringProfileDeletion: allowDuringProfileDeletion, performSessionCleanup: { + try await Self.signOutCleanup( + isRestored: isRestored, + forgetSessionAccess: forgetSessionAccess, + signOutRestored: signOutRestored + ) + }) + } + /// An identity that never restored, such as one whose shared Paykit state a newer Bitkit saved, cannot read that state /// to remove its endpoints or end its session, so Disconnect only forgets it on this device. A pending endpoint cleanup /// is dropped with it, so it cannot run against the next identity. diff --git a/BitkitTests/PubkyProfileManagerTests.swift b/BitkitTests/PubkyProfileManagerTests.swift index 09e0f341d..7d30a9e18 100644 --- a/BitkitTests/PubkyProfileManagerTests.swift +++ b/BitkitTests/PubkyProfileManagerTests.swift @@ -1154,6 +1154,62 @@ final class PubkyProfileManagerTests: XCTestCase { XCTAssertEqual(calls.values, ["forget", "dropCleanup", "signOut"]) } + @MainActor + func testDisconnectForgetsAProfileThatNeedsAnUpdate() async throws { + try await withEmptyIdentityStorage { + let manager = RecoveryProfileManager() + let calls = LockedCalls() + await manager.initialize { .restorationNeedsUpdate } + XCTAssertTrue(manager.restorationNeedsUpdate) + + try await manager.signOutOrForget( + allowDuringProfileDeletion: false, + forgetSessionAccess: { calls.append("forget") }, + signOutRestored: { calls.append("signOut") } + ) + + XCTAssertEqual(calls.values, ["forget"]) + XCTAssertNil(manager.publicKeyForDisplay) + XCTAssertFalse(manager.restorationNeedsUpdate) + } + } + + @MainActor + func testDisconnectSignsOutAProfileRestoredWhileItWaits() async throws { + try await withEmptyIdentityStorage { + let manager = RecoveryProfileManager() + let calls = LockedCalls() + let started = expectation(description: "Restore started") + let (resume, continuation) = AsyncStream.makeStream() + defer { continuation.finish() } + let restore = Task { + await manager.initialize { + started.fulfill() + for await _ in resume {} + return .restored(publicKey: "restored-identity") + } + } + await fulfillment(of: [started], timeout: 2) + + let disconnect = Task { + try await manager.signOutOrForget( + allowDuringProfileDeletion: false, + forgetSessionAccess: { calls.append("forget") }, + signOutRestored: { calls.append("signOut") } + ) + } + for _ in 0 ..< 5 { + await Task.yield() + } + XCTAssertTrue(calls.values.isEmpty) + continuation.finish() + await restore.value + try await disconnect.value + + XCTAssertEqual(calls.values, ["signOut"]) + } + } + @MainActor func testRecoveryAsksForAnUpdateOnceAndStopsRetryingWhenPaykitStateIsNewer() async { let manager = RecoveryProfileManager()