Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call stopControllerFactory atexit in the Darwin framework.
Browse files Browse the repository at this point in the history
We have lots of things with static destructors that assert clean
shutdown in the destructor.  That means that calling exit() without a
clean shutdown is pretty much guaranteed to lead to a shutdown crash.

Just ensure that we are in fact shutting down the
MTRDeviceControllerFactory if exit() is called, to avoid those crashes.
bzbarsky-apple committed Jan 31, 2023
1 parent b15826a commit 5afad08
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
@@ -44,6 +44,8 @@
#include <lib/support/TestPersistentStorageDelegate.h>
#include <platform/PlatformManager.h>

#include <cstdlib>

using namespace chip;
using namespace chip::Controller;

@@ -58,6 +60,9 @@
static NSString * const kErrorCDCertStoreInit = @"Init failure while initializing Certificate Declaration Signing Keys store";
static NSString * const kErrorOtaProviderInit = @"Init failure while creating an OTA provider delegate";

static bool sExitHandlerRegistered = false;
static void ShutdownOnExit() { [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; }

@interface MTRDeviceControllerFactory ()

@property (atomic, readonly) dispatch_queue_t chipWorkQueue;
@@ -382,7 +387,14 @@ - (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
// This needs to happen after DeviceControllerFactory::Init,
// because that creates (lazily, by calling functions with
// static variables in them) some static-lifetime objects.
chip::HeapObjectPoolExitHandling::IgnoreLeaksOnExit();
if (!sExitHandlerRegistered) {
int ret = atexit(ShutdownOnExit);
if (ret != 0) {
MTR_LOG_ERROR("Error registering exit handler: %d", ret);
return;
}
}
HeapObjectPoolExitHandling::IgnoreLeaksOnExit();

// Make sure we don't leave a system state running while we have no
// controllers started. This is working around the fact that a system

0 comments on commit 5afad08

Please sign in to comment.