Skip to content

Commit

Permalink
Issue project-chip#22935 [Darwin] MTRBaseSubscriptionCallback OnDone …
Browse files Browse the repository at this point in the history
…callback being called async can lead to crashes
  • Loading branch information
jtung-apple committed Sep 30, 2022
1 parent 2a1f9ef commit c9f61bf
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 c9f61bf

Please sign in to comment.