Skip to content
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

Make sure to not dispatch to shut-down queues in MTROTAProviderDelegateBridge. #23841

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ MTR_NEWLY_AVAILABLE
/*
* OTA Provider delegate to be called when an OTA Requestor is requesting a software update.
* Defaults to nil.
*
* Calls to this delegate can happen on an arbitrary thread, but will not happen
* concurrently.
*/
@property (nonatomic, strong, nullable) id<MTROTAProviderDelegate> otaProviderDelegate;

Expand Down
19 changes: 13 additions & 6 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,11 @@ - (MTRDeviceController * _Nullable)maybeInitializeOTAProvider:(MTRDeviceControll
VerifyOrReturnValue(_otaProviderDelegateBridge != nil, controller);
VerifyOrReturnValue([_controllers count] == 1, controller);

auto systemState = _controllerFactory->GetSystemState();
CHIP_ERROR err = _otaProviderDelegateBridge->Init(systemState->SystemLayer(), systemState->ExchangeMgr());
__block CHIP_ERROR err;
dispatch_sync(_chipWorkQueue, ^{
auto systemState = _controllerFactory->GetSystemState();
err = _otaProviderDelegateBridge->Init(systemState->SystemLayer(), systemState->ExchangeMgr());
});
if (CHIP_NO_ERROR != err) {
MTR_LOG_ERROR("Failed to init provider delegate bridge: %" CHIP_ERROR_FORMAT, err.Format());
[controller shutdown];
Expand Down Expand Up @@ -708,19 +711,23 @@ - (void)controllerShuttingDown:(MTRDeviceController *)controller
[_controllers removeObject:controller];

if ([_controllers count] == 0) {
if (_otaProviderDelegateBridge) {
_otaProviderDelegateBridge->Shutdown();
}

// That was our last controller. Stop the event loop before it
// shuts down, because shutdown of the last controller will tear
// down most of the world.
DeviceLayer::PlatformMgrImpl().StopEventLoopTask();

if (_otaProviderDelegateBridge) {
_otaProviderDelegateBridge->Shutdown();
}

[controller shutDownCppController];
} else {
// Do the controller shutdown on the Matter work queue.
dispatch_sync(_chipWorkQueue, ^{
if (_otaProviderDelegateBridge) {
_otaProviderDelegateBridge->ControllerShuttingDown(controller);
}

[controller shutDownCppController];
});
}
Expand Down
9 changes: 8 additions & 1 deletion src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ class MTROTAProviderDelegateBridge : public chip::app::Clusters::OTAProviderDele
~MTROTAProviderDelegateBridge();

CHIP_ERROR Init(chip::System::Layer * systemLayer, chip::Messaging::ExchangeManager * exchangeManager);

// Shutdown must be called after the event loop has been stopped, since it
// touches Matter objects.
void Shutdown();

// ControllerShuttingDown must be called on the Matter work queue, since it
// touches Matter objects.
void ControllerShuttingDown(MTRDeviceController * controller);

void HandleQueryImage(
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::QueryImage::DecodableType & commandData) override;
Expand Down Expand Up @@ -60,7 +67,7 @@ class MTROTAProviderDelegateBridge : public chip::app::Clusters::OTAProviderDele
MTROTASoftwareUpdateProviderClusterNotifyUpdateAppliedParams * commandParams);

_Nullable id<MTROTAProviderDelegate> mDelegate;
dispatch_queue_t mWorkQueue;
dispatch_queue_t mDelegateNotificationQueue;
};

NS_ASSUME_NONNULL_END
Loading