Protect RemoteMessagePort service terminating prematurely

This commit is contained in:
Zorg committed 2016-05-20 18:28:07 -04:00
1 parent e3795194d6
commit d463db5742
1 file changed
+24
+24
View File
@@ -15,11 +15,14 @@
static NSMutableDictionary<NSString *, NSMutableArray<SURemoteMessagePort *> *> *gMessagePortsTable;
static dispatch_queue_t gMessageQueue;
static NSString *SURemoteServiceLookupReason = @"Remote Service Connection";
@interface SURemoteMessagePort ()
@property (nonatomic, copy) NSString *serviceName;
@property (nonatomic) CFMessagePortRef messagePort;
@property (nonatomic, copy) void (^invalidationCallback)(void);
@property (nonatomic) BOOL disabledAutomaticTermination;
@end
@@ -28,6 +31,7 @@ static dispatch_queue_t gMessageQueue;
@synthesize serviceName = _serviceName;
@synthesize messagePort = _messagePort;
@synthesize invalidationCallback = _invalidationCallback;
@synthesize disabledAutomaticTermination = _disabledAutomaticTermination;
- (instancetype)init
{
@@ -38,6 +42,10 @@ static dispatch_queue_t gMessageQueue;
gMessagePortsTable = [[NSMutableDictionary alloc] init];
gMessageQueue = dispatch_queue_create("org.sparkle-project.remote-message-port", DISPATCH_QUEUE_SERIAL);
});
// If we are a XPC service, protect it from being terminated until the invalidation handler is set
_disabledAutomaticTermination = YES;
[[NSProcessInfo processInfo] disableAutomaticTermination:SURemoteServiceLookupReason];
}
return self;
}
@@ -51,6 +59,19 @@ static dispatch_queue_t gMessageQueue;
return self;
}
- (void)dealloc
{
[self enableAutomaticTermination];
}
- (void)enableAutomaticTermination
{
if (self.disabledAutomaticTermination) {
[[NSProcessInfo processInfo] enableAutomaticTermination:SURemoteServiceLookupReason];
self.disabledAutomaticTermination = NO;
}
}
- (void)connectWithLookupCompletion:(void (^)(BOOL))lookupCompletionHandler
{
dispatch_async(gMessageQueue, ^{
@@ -80,6 +101,9 @@ static dispatch_queue_t gMessageQueue;
- (void)setInvalidationHandler:(void (^)(void))invalidationHandler
{
// We can disable automatic termination now because we will be protected by the invalidationHandler (if this is a XPC service)
[self enableAutomaticTermination];
dispatch_async(gMessageQueue, ^{
if (self.messagePort != NULL) {
self.invalidationCallback = [invalidationHandler copy];