Skip to content

Commit

Permalink
[Darwin] Unstored attributes should be flushed to storage on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Dec 10, 2024
1 parent 918320e commit 82e5795
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ typedef void (^MTRDeviceControllerDataStoreClusterDataHandler)(NSDictionary<NSNu
- (nullable NSDictionary<NSString *, id> *)getStoredDeviceDataForNodeID:(NSNumber *)nodeID;
- (void)storeDeviceData:(NSDictionary<NSString *, id> *)data forNodeID:(NSNumber *)nodeID;

/**
* Mechanism for API client to perform a block after previous async operations (writes) on the storage queue have executed.
*
* If no block is passed in, then the method returns after having synchronously flushed the queue.
*/
- (void)synchronouslyPerformBlock:(void (^_Nullable)(void))block;

@end

NS_ASSUME_NONNULL_END
9 changes: 9 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,15 @@ - (void)storeDeviceData:(NSDictionary<NSString *, id> *)data forNodeID:(NSNumber
});
}

- (void)synchronouslyPerformBlock:(void (^_Nullable)(void))block
{
dispatch_sync(_storageDelegateQueue, ^{
if (block) {
block();
}
});
}

@end

@implementation MTRCASESessionResumptionInfo
Expand Down
7 changes: 7 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,13 @@ - (void)cleanupAfterStartup
for (MTRDevice * device in devices) {
[device invalidate];
}

// Since MTRDevice invalidate may issue asynchronous writes to storage, perform a
// block synchronously on the storage delegate queue to flush those operations.
[self.controllerDataStore synchronouslyPerformBlock:^{
MTR_LOG("%@ Finished flushing data store", self);
}];

[self stopBrowseForCommissionables];

[_factory controllerShuttingDown:self];
Expand Down
3 changes: 3 additions & 0 deletions src/darwin/Framework/CHIP/MTRDevice_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,9 @@ - (void)invalidate
{
MTR_LOG("%@ invalidate", self);

// First flush unstored attributes if any
[self _persistClusterData];

[_asyncWorkQueue invalidate];

os_unfair_lock_lock(&self->_timeSyncLock);
Expand Down

0 comments on commit 82e5795

Please sign in to comment.