Skip to content

Commit 1879730

Browse files
jtung-applepull[bot]
authored andcommitted
Issue #22935 [Darwin] MTRBaseSubscriptionCallback OnDone callback being called async can lead to crashes (#22978)
1 parent 47a53bd commit 1879730

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/darwin/Framework/CHIP/MTRBaseSubscriptionCallback.mm

+13-4
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,21 @@
138138
mOnDoneHandler = nil;
139139
dispatch_async(mQueue, ^{
140140
callback(err);
141-
if (onDoneHandler) {
142-
onDoneHandler();
143-
}
144141
});
145142

146-
if (aCancelSubscription) {
143+
if (onDoneHandler) {
144+
// To guarantee the async onDoneHandler call is made before
145+
// deletion, so that clean up can happen while the callback
146+
// object is still alive (and therefore cluster cache), queue
147+
// deletion after calling the onDoneHandler
148+
mHaveQueuedDeletion = true;
149+
dispatch_async(mQueue, ^{
150+
onDoneHandler();
151+
dispatch_async(DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
152+
delete myself;
153+
});
154+
});
155+
} else if (aCancelSubscription) {
147156
// We can't synchronously delete ourselves, because we're inside one of
148157
// the ReadClient callbacks and we need to outlive the callback's
149158
// execution. Queue an async deletion on the Matter queue (where we are

0 commit comments

Comments
 (0)