Skip to content
This repository was archived by the owner on Jul 25, 2025. It is now read-only.

Commit b052e40

Browse files
authored
fix: Thread safe subscription watcher cancel (#523)
* fix: Thread safe subscription watcher cancel * use guard
1 parent 5339230 commit b052e40

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

AWSAppSyncClient/AWSAppSyncSubscriptionWatcher.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class AWSAppSyncSubscriptionWatcher<Subscription: GraphQLSubscripti
2828
private var subscriptionItem: SubscriptionItem?
2929

3030
private let handlerQueue: DispatchQueue
31+
private let subscriptionsQueue: DispatchQueue
3132
private var resultHandler: SubscriptionResultHandler<Subscription>?
3233
private var connectedCallback: (() -> Void)?
3334
private var statusChangeHandler: SubscriptionStatusChangeHandler?
@@ -57,6 +58,7 @@ public final class AWSAppSyncSubscriptionWatcher<Subscription: GraphQLSubscripti
5758
resultHandler(result, transaction, error)
5859
}
5960
}
61+
self.subscriptionsQueue = subscriptionsQueue
6062
subscriptionsQueue.async { [weak self] in
6163
guard let self = self else {return}
6264
if !self.isCancelled {
@@ -177,22 +179,26 @@ public final class AWSAppSyncSubscriptionWatcher<Subscription: GraphQLSubscripti
177179
/// Specifically, this means that cancelling a subscription watcher will not invoke `statusChangeHandler` or
178180
/// `resultHandler`, although it will set the internal state of the watcher to `.disconnected`
179181
public func cancel() {
182+
guard !self.isCancelled else { return }
183+
180184
if self.cancellationSource == .none {
181185
self.cancellationSource = .user
182186
}
183-
performCleanUpTasksOnCancel()
187+
self.performCleanUpTasksOnCancel()
184188
}
185189

186190
internal func performCleanUpTasksOnCancel() {
187191
isCancelled = true
188192
status = .disconnected
189193
resultHandler = nil
190194
statusChangeHandler = nil
191-
192-
if self.cancellationSource != .error, let item = subscriptionItem {
193-
connection?.unsubscribe(item: item)
195+
subscriptionsQueue.async { [weak self] in
196+
guard let self = self else {return}
197+
if self.cancellationSource != .error, let item = self.subscriptionItem {
198+
self.connection?.unsubscribe(item: item)
199+
}
200+
self.connection = nil
194201
}
195-
connection = nil
196202
}
197203

198204
}

0 commit comments

Comments
 (0)