-
-
Notifications
You must be signed in to change notification settings - Fork 11
fix(apple): serialize connection teardown #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
873385b
fix(apple): serialize connection teardown
hyochan 73cb694
fix(apple): reduce connection task churn
hyochan 151f624
fix(apple): restore listener after reconnect
hyochan 2586d76
docs(releases): add apple teardown patch notes
hyochan 80b463a
fix(apple): guard connection task completion
hyochan 2aba861
refactor(apple): extract connection lifecycle state
hyochan 1d2a47a
refactor(apple): simplify product manager init
hyochan 3e986b9
fix(apple): harden connection init retry
hyochan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
285 changes: 285 additions & 0 deletions
285
packages/apple/Sources/Helpers/OpenIapConnectionLifecycle.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,285 @@ | ||
| import Foundation | ||
|
|
||
| /// Owns the mutable connection resources that must move together during | ||
| /// init/end races. OpenIapModule keeps StoreKit behavior; this helper keeps the | ||
| /// lock, generation checks, and task handles in one place. | ||
| @available(iOS 15.0, macOS 14.0, tvOS 16.0, watchOS 8.0, *) | ||
| final class OpenIapConnectionLifecycle { | ||
| // MARK: - Resource Snapshots | ||
|
|
||
| struct CleanupResources { | ||
| let updateListenerTask: Task<Void, Error>? | ||
| let messageListenerTask: Task<Void, Never>? | ||
| let unfinishedTransactionTask: Task<Void, Never>? | ||
| let productManager: ProductManager? | ||
| let didRegisterPaymentQueueObserver: Bool | ||
| } | ||
|
|
||
| struct DeinitResources { | ||
| let initTask: Task<Bool, Error>? | ||
| let endTask: Task<Void, Never>? | ||
| let updateListenerTask: Task<Void, Error>? | ||
| let messageListenerTask: Task<Void, Never>? | ||
| let unfinishedTransactionTask: Task<Void, Never>? | ||
| } | ||
|
|
||
| private let lock = NSLock() | ||
| private var connectionGeneration: UInt64 = 0 | ||
| private var initTask: Task<Bool, Error>? | ||
| private var initTaskGeneration: UInt64? | ||
| private var endTask: Task<Void, Never>? | ||
| private var endTaskGeneration: UInt64? | ||
| private var updateListenerTask: Task<Void, Error>? | ||
| private var messageListenerTask: Task<Void, Never>? | ||
| private var unfinishedTransactionTask: Task<Void, Never>? | ||
| private var productManager: ProductManager? | ||
|
|
||
| #if os(iOS) | ||
| private var didRegisterPaymentQueueObserver = false | ||
| #endif | ||
|
|
||
| // MARK: - Init / End Tasks | ||
|
|
||
| func currentEndTask() -> Task<Void, Never>? { | ||
| withLock { endTask } | ||
| } | ||
|
|
||
| func makeInitTask( | ||
| operation: @escaping (UInt64) async throws -> Bool | ||
| ) -> (task: Task<Bool, Error>, generation: UInt64)? { | ||
| withLock { | ||
| guard endTask == nil else { | ||
| return nil | ||
| } | ||
|
|
||
| if let initTask, initTaskGeneration == connectionGeneration { | ||
| return (initTask, connectionGeneration) | ||
| } | ||
|
|
||
| connectionGeneration += 1 | ||
| let generation = connectionGeneration | ||
| let task = Task<Bool, Error> { | ||
| try await operation(generation) | ||
| } | ||
| initTask = task | ||
| initTaskGeneration = generation | ||
| return (task, generation) | ||
| } | ||
| } | ||
|
|
||
| func makeEndTask(cleanup: @escaping () async -> Void) -> Task<Void, Never> { | ||
| withLock { | ||
| if let endTask { | ||
| return endTask | ||
| } | ||
|
|
||
| connectionGeneration += 1 | ||
| let generation = connectionGeneration | ||
| let taskToCancel = initTask | ||
| initTask = nil | ||
| initTaskGeneration = nil | ||
|
|
||
| let task = Task { [weak self] in | ||
| taskToCancel?.cancel() | ||
| if let taskToCancel { | ||
| await Self.awaitCancelledInitTask(taskToCancel) | ||
| } | ||
|
|
||
| await cleanup() | ||
| self?.clearEndTask(generation: generation) | ||
| } | ||
| endTask = task | ||
| endTaskGeneration = generation | ||
| return task | ||
| } | ||
| } | ||
|
|
||
| func clearInitTask(generation: UInt64) { | ||
| withLock { | ||
| if initTaskGeneration == generation { | ||
| initTask = nil | ||
| initTaskGeneration = nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func clearUnfinishedTransactionTask(generation: UInt64) { | ||
| withLock { | ||
| if connectionGeneration == generation { | ||
| unfinishedTransactionTask = nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func ensureCurrent(_ generation: UInt64) throws { | ||
| try withLock { | ||
| guard connectionGeneration == generation else { | ||
| throw CancellationError() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Resource Access | ||
|
|
||
| func currentProductManager() -> ProductManager? { | ||
| withLock { productManager } | ||
| } | ||
|
|
||
| func getOrCreateProductManager(generation: UInt64) throws -> ProductManager { | ||
| try withLock { | ||
| guard connectionGeneration == generation else { | ||
| throw CancellationError() | ||
| } | ||
|
|
||
| if let productManager { | ||
| return productManager | ||
| } | ||
|
|
||
| let productManager = ProductManager() | ||
| self.productManager = productManager | ||
| return productManager | ||
| } | ||
| } | ||
|
|
||
| #if os(iOS) | ||
| func markPaymentQueueObserverRegisteredIfNeeded(generation: UInt64) throws -> Bool { | ||
| try withLock { | ||
| guard connectionGeneration == generation else { | ||
| throw CancellationError() | ||
| } | ||
|
|
||
| if didRegisterPaymentQueueObserver { | ||
| return false | ||
| } | ||
|
|
||
| didRegisterPaymentQueueObserver = true | ||
| return true | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| // MARK: - Listener Tasks | ||
|
|
||
| func startTransactionListenerTask( | ||
| generation: UInt64, | ||
| makeTask: () -> Task<Void, Error> | ||
| ) throws { | ||
| try withLock { | ||
| guard connectionGeneration == generation else { | ||
| throw CancellationError() | ||
| } | ||
| guard updateListenerTask == nil else { | ||
| return | ||
| } | ||
|
|
||
| updateListenerTask = makeTask() | ||
| } | ||
| } | ||
|
|
||
| func startUnfinishedTransactionTask( | ||
| generation: UInt64, | ||
| makeTask: () -> Task<Void, Never> | ||
| ) throws { | ||
| try withLock { | ||
| guard connectionGeneration == generation else { | ||
| throw CancellationError() | ||
| } | ||
| guard unfinishedTransactionTask == nil else { | ||
| return | ||
| } | ||
|
|
||
| unfinishedTransactionTask = makeTask() | ||
| } | ||
| } | ||
|
|
||
| func startMessageListenerTask( | ||
| generation: UInt64?, | ||
| makeTask: () -> Task<Void, Never> | ||
| ) throws { | ||
| try withLock { | ||
| if let generation, connectionGeneration != generation { | ||
| throw CancellationError() | ||
| } | ||
| guard endTask == nil, messageListenerTask == nil else { | ||
| return | ||
| } | ||
|
|
||
| messageListenerTask = makeTask() | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Cleanup | ||
|
|
||
| func detachResourcesForCleanup() -> CleanupResources { | ||
| withLock { | ||
| #if os(iOS) | ||
| let wasRegistered = didRegisterPaymentQueueObserver | ||
| didRegisterPaymentQueueObserver = false | ||
| #else | ||
| let wasRegistered = false | ||
| #endif | ||
|
|
||
| let resources = CleanupResources( | ||
| updateListenerTask: updateListenerTask, | ||
| messageListenerTask: messageListenerTask, | ||
| unfinishedTransactionTask: unfinishedTransactionTask, | ||
| productManager: productManager, | ||
| didRegisterPaymentQueueObserver: wasRegistered | ||
| ) | ||
|
|
||
| updateListenerTask = nil | ||
| messageListenerTask = nil | ||
| unfinishedTransactionTask = nil | ||
| productManager = nil | ||
| return resources | ||
| } | ||
| } | ||
|
|
||
| func detachTasksForDeinit() -> DeinitResources { | ||
| withLock { | ||
| let resources = DeinitResources( | ||
| initTask: initTask, | ||
| endTask: endTask, | ||
| updateListenerTask: updateListenerTask, | ||
| messageListenerTask: messageListenerTask, | ||
| unfinishedTransactionTask: unfinishedTransactionTask | ||
| ) | ||
|
|
||
| initTask = nil | ||
| initTaskGeneration = nil | ||
| endTask = nil | ||
| endTaskGeneration = nil | ||
| updateListenerTask = nil | ||
| messageListenerTask = nil | ||
| unfinishedTransactionTask = nil | ||
| return resources | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Locking | ||
|
|
||
| private func clearEndTask(generation: UInt64) { | ||
| withLock { | ||
| if endTaskGeneration == generation { | ||
| endTask = nil | ||
| endTaskGeneration = nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func withLock<T>(_ body: () throws -> T) rethrows -> T { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| return try body() | ||
| } | ||
|
|
||
| private static func awaitCancelledInitTask(_ task: Task<Bool, Error>) async { | ||
| do { | ||
| _ = try await task.value | ||
| } catch is CancellationError { | ||
| // Expected when endConnection cancels an in-flight initConnection. | ||
| } catch { | ||
| OpenIapLog.warn("initConnection failed while endConnection was cancelling it: \(error)") | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.