Skip to content

Commit

Permalink
Issue #22935 [Darwin] MTRBaseSubscriptionCallback OnDone callback bei…
Browse files Browse the repository at this point in the history
…ng called async can lead to crashes (#22978)
  • Loading branch information
jtung-apple authored Oct 3, 2022
1 parent aa2f225 commit 8e2c926
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/darwin/Framework/CHIP/MTRBaseSubscriptionCallback.mm
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,21 @@
mOnDoneHandler = nil;
dispatch_async(mQueue, ^{
callback(err);
if (onDoneHandler) {
onDoneHandler();
}
});

if (aCancelSubscription) {
if (onDoneHandler) {
// To guarantee the async onDoneHandler call is made before
// deletion, so that clean up can happen while the callback
// object is still alive (and therefore cluster cache), queue
// deletion after calling the onDoneHandler
mHaveQueuedDeletion = true;
dispatch_async(mQueue, ^{
onDoneHandler();
dispatch_async(DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
delete myself;
});
});
} else if (aCancelSubscription) {
// We can't synchronously delete ourselves, because we're inside one of
// the ReadClient callbacks and we need to outlive the callback's
// execution. Queue an async deletion on the Matter queue (where we are
Expand Down

0 comments on commit 8e2c926

Please sign in to comment.