Skip to content

Commit 3ffe083

Browse files
Fixing exception with calling into a dead XPC object (project-chip#35908)
* Fixing exception with calling into a dead XPC object * Fixing white space * Restyled by whitespace * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 965a377 commit 3ffe083

File tree

3 files changed

+96
-51
lines changed

3 files changed

+96
-51
lines changed

src/darwin/Framework/CHIP/MTRDefines_Internal.h

+37-24
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,26 @@ typedef struct {} variable_hidden_by_mtr_hide;
6767

6868
#pragma mark - XPC Defines
6969

70-
#define MTR_SIMPLE_REMOTE_XPC_GETTER(XPC_CONNECTION, NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, PREFIX) \
71-
\
72-
-(TYPE) NAME \
73-
{ \
74-
__block TYPE outValue = DEFAULT_VALUE; \
75-
\
76-
NSXPCConnection * xpcConnection = XPC_CONNECTION; \
77-
\
78-
[[xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
79-
MTR_LOG_ERROR("Error: %@", error); \
80-
}] PREFIX \
81-
GETTER_NAME:^(TYPE returnValue) { \
82-
outValue = returnValue; \
83-
}]; \
84-
\
85-
return outValue; \
70+
#define MTR_SIMPLE_REMOTE_XPC_GETTER(XPC_CONNECTION, NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, PREFIX) \
71+
\
72+
-(TYPE) NAME \
73+
{ \
74+
__block TYPE outValue = DEFAULT_VALUE; \
75+
\
76+
NSXPCConnection * xpcConnection = XPC_CONNECTION; \
77+
\
78+
@try { \
79+
[[xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
80+
MTR_LOG_ERROR("Error: %@", error); \
81+
}] PREFIX \
82+
GETTER_NAME:^(TYPE returnValue) { \
83+
outValue = returnValue; \
84+
}]; \
85+
} @catch (NSException * exception) { \
86+
MTR_LOG_ERROR("Exception sending XPC messsage: %@", exception); \
87+
outValue = DEFAULT_VALUE; \
88+
} \
89+
return outValue; \
8690
}
8791

8892
#define MTR_SIMPLE_REMOTE_XPC_COMMAND(XPC_CONNECTION, METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS, PREFIX) \
@@ -91,9 +95,13 @@ typedef struct {} variable_hidden_by_mtr_hide;
9195
{ \
9296
NSXPCConnection * xpcConnection = XPC_CONNECTION; \
9397
\
94-
[[xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
95-
MTR_LOG_ERROR("Error: %@", error); \
96-
}] PREFIX ADDITIONAL_ARGUMENTS]; \
98+
@try { \
99+
[[xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
100+
MTR_LOG_ERROR("Error: %@", error); \
101+
}] PREFIX ADDITIONAL_ARGUMENTS]; \
102+
} @catch (NSException * exception) { \
103+
MTR_LOG_ERROR("Exception sending XPC messsage: %@", exception); \
104+
} \
97105
}
98106

99107
#define MTR_COMPLEX_REMOTE_XPC_GETTER(XPC_CONNECTION, SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS, PREFIX) \
@@ -103,11 +111,16 @@ typedef struct {} variable_hidden_by_mtr_hide;
103111
\
104112
NSXPCConnection * xpcConnection = XPC_CONNECTION; \
105113
\
106-
[[xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
107-
MTR_LOG_ERROR("Error: %@", error); \
108-
}] PREFIX ADDITIONAL_ARGUMENTS:^(TYPE returnValue) { \
109-
outValue = returnValue; \
110-
}]; \
114+
@try { \
115+
[[xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { \
116+
MTR_LOG_ERROR("Error: %@", error); \
117+
}] PREFIX ADDITIONAL_ARGUMENTS:^(TYPE returnValue) { \
118+
outValue = returnValue; \
119+
}]; \
120+
} @catch (NSException * exception) { \
121+
MTR_LOG_ERROR("Exception sending XPC messsage: %@", exception); \
122+
outValue = DEFAULT_VALUE; \
123+
} \
111124
\
112125
return outValue; \
113126
}

src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm

+42-14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,45 @@ @interface MTRDeviceController_XPC ()
4444

4545
@implementation MTRDeviceController_XPC
4646

47+
#pragma mark - Device Node ID Commands
48+
49+
- (void)_registerNodeID:(NSNumber *)nodeID
50+
{
51+
@try {
52+
[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
53+
MTR_LOG_ERROR("Register node error: %@ nodeID: %@", error, nodeID);
54+
}] deviceController:self.uniqueIdentifier registerNodeID:nodeID];
55+
} @catch (NSException * exception) {
56+
MTR_LOG_ERROR("Exception registering nodeID: %@", exception);
57+
}
58+
}
59+
60+
- (void)_unregisterNodeID:(NSNumber *)nodeID
61+
{
62+
@try {
63+
[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
64+
MTR_LOG_ERROR("Unregister node error: %@ nodeID: %@", error, nodeID);
65+
}] deviceController:self.uniqueIdentifier unregisterNodeID:nodeID];
66+
} @catch (NSException * exception) {
67+
MTR_LOG_ERROR("Exception registering nodeID: %@", exception);
68+
}
69+
}
70+
71+
- (void)_checkinWithContext:(NSDictionary *)context
72+
{
73+
@try {
74+
if (!context)
75+
context = [NSDictionary dictionary];
76+
77+
[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
78+
MTR_LOG_ERROR("Checkin error: %@", error);
79+
}] deviceController:self.uniqueIdentifier checkInWithContext:context];
80+
} @catch (NSException * exception) {
81+
MTR_LOG_ERROR("Exception registering nodeID: %@", exception);
82+
}
83+
}
84+
85+
#pragma mark - XPC
4786
+ (NSMutableSet *)_allowedClasses
4887
{
4988
static NSArray * sBaseAllowedClasses = @[
@@ -202,22 +241,15 @@ - (BOOL)_setupXPCConnection
202241
MTR_LOG("%@ Activating new XPC connection", self);
203242
[self.xpcConnection activate];
204243

205-
[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
206-
MTR_LOG_ERROR("Checkin error: %@", error);
207-
}] deviceController:self.uniqueIdentifier checkInWithContext:[NSDictionary dictionary]];
244+
[self _checkinWithContext:[NSDictionary dictionary]];
208245

209246
// FIXME: Trying to kick all the MTRDevices attached to this controller to re-establish connections
210247
// This state needs to be stored properly and re-established at connnection time
211248

212249
MTR_LOG("%@ Starting existing NodeID Registration", self);
213250
for (NSNumber * nodeID in [self.nodeIDToDeviceMap keyEnumerator]) {
214251
MTR_LOG("%@ => Registering nodeID: %@", self, nodeID);
215-
mtr_weakify(self);
216-
217-
[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
218-
mtr_strongify(self);
219-
MTR_LOG_ERROR("%@ Registration error for device nodeID: %@ : %@", self, nodeID, error);
220-
}] deviceController:self.uniqueIdentifier registerNodeID:nodeID];
252+
[self _registerNodeID:nodeID];
221253
}
222254

223255
MTR_LOG("%@ Done existing NodeID Registration", self);
@@ -308,11 +340,7 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
308340
[self.nodeIDToDeviceMap setObject:deviceToReturn forKey:nodeID];
309341
MTR_LOG("%s: returning XPC device for node id %@", __PRETTY_FUNCTION__, nodeID);
310342

311-
mtr_weakify(self);
312-
[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
313-
mtr_strongify(self);
314-
MTR_LOG_ERROR("%@ Registration error for device nodeID: %@ : %@", self, nodeID, error);
315-
}] deviceController:self.uniqueIdentifier registerNodeID:nodeID];
343+
[self _registerNodeID:nodeID];
316344

317345
return deviceToReturn;
318346
}

src/darwin/Framework/CHIP/MTRDevice_XPC.mm

+17-13
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,23 @@ - (void)_invokeCommandWithEndpointID:(NSNumber *)endpointID
249249
{
250250
NSXPCConnection * xpcConnection = [(MTRDeviceController_XPC *) [self deviceController] xpcConnection];
251251

252-
[[xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
253-
MTR_LOG_ERROR("Error: %@", error);
254-
}] deviceController:[[self deviceController] uniqueIdentifier]
255-
nodeID:[self nodeID]
256-
invokeCommandWithEndpointID:endpointID
257-
clusterID:clusterID
258-
commandID:commandID
259-
commandFields:commandFields
260-
expectedValues:expectedValues
261-
expectedValueInterval:expectedValueInterval
262-
timedInvokeTimeout:timeout
263-
serverSideProcessingTimeout:serverSideProcessingTimeout
264-
completion:completion];
252+
@try {
253+
[[xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
254+
MTR_LOG_ERROR("Error: %@", error);
255+
}] deviceController:[[self deviceController] uniqueIdentifier]
256+
nodeID:[self nodeID]
257+
invokeCommandWithEndpointID:endpointID
258+
clusterID:clusterID
259+
commandID:commandID
260+
commandFields:commandFields
261+
expectedValues:expectedValues
262+
expectedValueInterval:expectedValueInterval
263+
timedInvokeTimeout:timeout
264+
serverSideProcessingTimeout:serverSideProcessingTimeout
265+
completion:completion];
266+
} @catch (NSException * exception) {
267+
MTR_LOG_ERROR("Exception sending XPC messsage: %@", exception);
268+
}
265269
}
266270

267271
// Not Supported via XPC

0 commit comments

Comments
 (0)