mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-22 04:30:47 +00:00
Wait for additional time for installation to complete on shutdown (#2946)
This is meant to increase the success of downloaded updates from finishing installing on system shutdown. The updater progress app needs to use dispatch source for observing when the PID dies because LaunchServices may no longer be available at this point. Before an update is superceded we will need to send a skip/cancel message to expedite the termination of the helpers.
This commit is contained in:
9 files changed
+195
-30
No files matched your search
@@ -17,6 +17,8 @@ SPU_OBJC_DIRECT_MEMBERS @interface AppInstaller : NSObject
|
||||
|
||||
- (void)start;
|
||||
|
||||
- (void)handleTerminationRequest;
|
||||
|
||||
- (void)cleanupAndExitWithStatus:(int)status error:(NSError * _Nullable)error __attribute__((noreturn));
|
||||
|
||||
@end
|
||||
|
||||
@@ -45,6 +45,10 @@
|
||||
*/
|
||||
static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7;
|
||||
|
||||
// This is slightly longer than SUTerminationRequestTargetWaitDelay in the installer progress app
|
||||
// to give extra time to allow receiving the termination message and finishing installation
|
||||
static const NSTimeInterval SUTerminationRequestTargetWaitDelay = 2.0;
|
||||
|
||||
@interface AppInstaller () <NSXPCListenerDelegate, SUInstallerCommunicationProtocol, AgentConnectionDelegate>
|
||||
@end
|
||||
|
||||
@@ -103,6 +107,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7;
|
||||
BOOL _performedStage3Installation;
|
||||
|
||||
BOOL _targetTerminated;
|
||||
BOOL _receivedTerminationRequest;
|
||||
}
|
||||
|
||||
- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier homeDirectory:(NSString *)homeDirectory userName:(NSString *)userName
|
||||
@@ -382,6 +387,26 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)handleTerminationRequest
|
||||
{
|
||||
// When we receive a termination quit request do not clean up the update directory
|
||||
// to reduce risk of removing something in the directory while the installer thread tries to install/swap
|
||||
// an update bundle from there
|
||||
if (_performedStage1Installation) {
|
||||
if (!_receivedTerminationRequest) {
|
||||
_receivedTerminationRequest = YES;
|
||||
|
||||
// Give grace period before doing forceful exit,
|
||||
// which may give some time to finish an app update installation.
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUTerminationRequestTargetWaitDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
exit(SIGTERM);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
exit(SIGTERM);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)agentConnectionDidInvalidate
|
||||
{
|
||||
if (!_finishedValidation || !_agentInitiatedConnection || !_targetTerminated) {
|
||||
|
||||
+1
-5
@@ -28,11 +28,7 @@ int main(int __unused argc, const char __unused *argv[])
|
||||
|
||||
dispatch_source_t sigtermSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
|
||||
dispatch_source_set_event_handler(sigtermSource, ^{
|
||||
// Don't clear the update directory because the installer may be in middle of installing an update
|
||||
// We still need to set an event handler for receiving SIGTERM though, otherwise our job may not terminate
|
||||
// (This is also recommended from the developer documentation).
|
||||
// Simply exit with SIGTERM if we receive this signal
|
||||
exit(SIGTERM);
|
||||
[appInstaller handleTerminationRequest];
|
||||
});
|
||||
dispatch_resume(sigtermSource);
|
||||
|
||||
|
||||
@@ -18,6 +18,8 @@ SPU_OBJC_DIRECT_MEMBERS @interface InstallerProgressAppController : NSObject <NS
|
||||
|
||||
- (void)run;
|
||||
|
||||
- (void)handleTerminationRequest;
|
||||
|
||||
- (void)cleanupAndExitWithStatus:(int)status error:(NSError * _Nullable)error __attribute__((noreturn));
|
||||
|
||||
@end
|
||||
|
||||
+96
-7
@@ -28,6 +28,10 @@
|
||||
*/
|
||||
static const NSTimeInterval SUTerminationTimeDelay = 0.3;
|
||||
|
||||
// This is slightly shorter than SUTerminationRequestTargetWaitDelay in Autoupdate
|
||||
// to give Autoupdate extra time to allow receiving the termination message and finishing installation
|
||||
static const NSTimeInterval SUTerminationRequestTargetWaitDelay = 1.5;
|
||||
|
||||
@interface InstallerProgressAppController () <NSApplicationDelegate, SPUInstallerAgentProtocol>
|
||||
@end
|
||||
|
||||
@@ -47,6 +51,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3;
|
||||
__weak id<InstallerProgressDelegate> _delegate;
|
||||
|
||||
void (^_terminationCompletionHandler)(void);
|
||||
dispatch_source_t _targetProcessExitSource;
|
||||
|
||||
BOOL _connected;
|
||||
BOOL _repliedToRegistration;
|
||||
@@ -55,6 +60,8 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3;
|
||||
BOOL _submittedLauncherJob;
|
||||
BOOL _willTerminate;
|
||||
BOOL _applicationInitiallyAlive;
|
||||
BOOL _receivedTerminationRequest;
|
||||
BOOL _targetProcessExited;
|
||||
}
|
||||
|
||||
- (instancetype)initWithApplication:(NSApplication *)application arguments:(NSArray<NSString *> *)arguments delegate:(id<InstallerProgressDelegate>)delegate
|
||||
@@ -172,6 +179,39 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3;
|
||||
[self startConnection];
|
||||
}
|
||||
|
||||
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)__unused sender
|
||||
{
|
||||
[self handleTerminationRequest];
|
||||
|
||||
return NSTerminateLater;
|
||||
}
|
||||
|
||||
- (void)handleTerminationRequest
|
||||
{
|
||||
// Check if we're going to terminate anyway
|
||||
if (_receivedTerminationRequest) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If Autoupdate installer is listening for termination completion,
|
||||
// we will delay termination for a bit to allow us some time to observe
|
||||
// the targetted app's termination, otherwise we will exit immediately
|
||||
|
||||
if (_terminationCompletionHandler == nil) {
|
||||
[self cleanupAndExitWithStatus:SIGTERM error:nil];
|
||||
}
|
||||
|
||||
_receivedTerminationRequest = YES;
|
||||
|
||||
if (_targetProcessExited) {
|
||||
[self notifyTargetTermination];
|
||||
}
|
||||
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUTerminationRequestTargetWaitDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[self cleanupAndExitWithStatus:SIGTERM error:nil];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)cleanupAndExitWithStatus:(int)status error:(NSError * _Nullable)error __attribute__((noreturn))
|
||||
{
|
||||
if (error != nil) {
|
||||
@@ -315,17 +355,49 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3;
|
||||
NSString *isTerminatedKeyPath = NSStringFromSelector(@selector(isTerminated));
|
||||
if ([keyPath isEqualToString:isTerminatedKeyPath]) {
|
||||
if (_targetRunningApplication.terminated && _terminationCompletionHandler != nil) {
|
||||
_terminationCompletionHandler();
|
||||
|
||||
[_targetRunningApplication removeObserver:self forKeyPath:isTerminatedKeyPath];
|
||||
_terminationCompletionHandler = nil;
|
||||
_targetRunningApplication = nil;
|
||||
[self notifyTargetTermination];
|
||||
}
|
||||
} else {
|
||||
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)notifyTargetTermination SPU_OBJC_DIRECT
|
||||
{
|
||||
if (_terminationCompletionHandler == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
_terminationCompletionHandler();
|
||||
_terminationCompletionHandler = nil;
|
||||
|
||||
[_targetRunningApplication removeObserver:self forKeyPath:NSStringFromSelector(@selector(isTerminated))];
|
||||
_targetRunningApplication = nil;
|
||||
|
||||
if (_targetProcessExitSource != nil) {
|
||||
dispatch_source_cancel(_targetProcessExitSource);
|
||||
_targetProcessExitSource = nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)targetProcessDidExit SPU_OBJC_DIRECT
|
||||
{
|
||||
if (_targetProcessExited) {
|
||||
return;
|
||||
}
|
||||
|
||||
_targetProcessExited = YES;
|
||||
|
||||
if (_targetProcessExitSource != nil) {
|
||||
dispatch_source_cancel(_targetProcessExitSource);
|
||||
_targetProcessExitSource = nil;
|
||||
}
|
||||
|
||||
if (_receivedTerminationRequest && _terminationCompletionHandler != nil) {
|
||||
[self notifyTargetTermination];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)listenForTerminationWithCompletion:(void (^)(void))completionHandler
|
||||
{
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
@@ -342,6 +414,23 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3;
|
||||
self->_terminationCompletionHandler = [completionHandler copy];
|
||||
|
||||
[self->_targetRunningApplication addObserver:self forKeyPath:NSStringFromSelector(@selector(isTerminated)) options:NSKeyValueObservingOptionNew context:NULL];
|
||||
|
||||
pid_t targetProcessIdentifier = self->_targetRunningApplication.processIdentifier;
|
||||
if (targetProcessIdentifier > 0) {
|
||||
dispatch_source_t targetProcessExitSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, (uintptr_t)targetProcessIdentifier, DISPATCH_PROC_EXIT, dispatch_get_main_queue());
|
||||
if (targetProcessExitSource != nil) {
|
||||
self->_targetProcessExitSource = targetProcessExitSource;
|
||||
|
||||
dispatch_source_set_event_handler(targetProcessExitSource, ^{
|
||||
[self targetProcessDidExit];
|
||||
});
|
||||
dispatch_resume(targetProcessExitSource);
|
||||
|
||||
if (kill(targetProcessIdentifier, 0) != 0 && errno == ESRCH) {
|
||||
[self targetProcessDidExit];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
SULog(SULogLevelError, @"Error: -listenForTerminationWithCompletion: called in unexpected state");
|
||||
@@ -381,7 +470,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3;
|
||||
- (void)relaunchApplication
|
||||
{
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (!self->_willTerminate && self->_applicationBundle != nil && self->_applicationInitiallyAlive) {
|
||||
if (!self->_willTerminate && !self->_receivedTerminationRequest && self->_applicationBundle != nil && self->_applicationInitiallyAlive) {
|
||||
NSString *pathToRelaunch;
|
||||
if (self->_normalizedPath != nil) {
|
||||
pathToRelaunch = self->_normalizedPath;
|
||||
@@ -417,7 +506,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3;
|
||||
- (void)showProgress
|
||||
{
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (!self->_willTerminate) {
|
||||
if (!self->_willTerminate && !self->_receivedTerminationRequest) {
|
||||
// Activate ourselves otherwise we will probably be in the background
|
||||
// See comments in -[SPUStandardUserDriver _activateApplication] for why -activate is not used
|
||||
[self->_application activateIgnoringOtherApps:YES];
|
||||
|
||||
Vendored
+1
-1
@@ -27,7 +27,7 @@ int main(int __unused argc, const char __unused *argv[])
|
||||
|
||||
dispatch_source_t sigtermSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
|
||||
dispatch_source_set_event_handler(sigtermSource, ^{
|
||||
[appController cleanupAndExitWithStatus:SIGTERM error:nil];
|
||||
[appController handleTerminationRequest];
|
||||
});
|
||||
dispatch_resume(sigtermSource);
|
||||
|
||||
|
||||
Vendored
+2
@@ -33,6 +33,8 @@ SPU_OBJC_DIRECT_MEMBERS @interface SPUInstallerDriver : NSObject
|
||||
|
||||
- (void)resumeInstallingUpdateWithUpdateItem:(SUAppcastItem *)updateItem systemDomain:(BOOL)systemDomain;
|
||||
|
||||
- (void)cancelSupersededInstallerBeforeLaunching;
|
||||
|
||||
- (void)setUpdateWillInstallHandler:(void (^)(void))updateWillInstallHandler;
|
||||
|
||||
- (void)extractDownloadedUpdate:(SPUDownloadedUpdate *)downloadedUpdate silently:(BOOL)silently completion:(void (^)(NSError * _Nullable))completionHandler;
|
||||
|
||||
Vendored
+65
-15
@@ -32,6 +32,7 @@
|
||||
#include "AppKitPrevention.h"
|
||||
|
||||
#define FIRST_INSTALLER_MESSAGE_TIMEOUT 7ull
|
||||
#define CANCEL_SUPERSEDED_INSTALLER_TIMEOUT 3ull
|
||||
|
||||
#if SPARKLE_BUILD_LEGACY_SUUPDATER
|
||||
@interface NSObject (PrivateDelegateMethods)
|
||||
@@ -66,6 +67,7 @@
|
||||
BOOL _systemDomain;
|
||||
BOOL _aborted;
|
||||
BOOL _notifiedDelegateInstallationWillFinish;
|
||||
BOOL _cancelSupersededInstaller;
|
||||
}
|
||||
|
||||
- (instancetype)initWithHost:(SUHost *)host applicationBundle:(NSBundle *)applicationBundle updater:(id)updater updaterDelegate:(id<SPUUpdaterDelegate>)updaterDelegate delegate:(nullable id<SPUInstallerDriverDelegate>)delegate
|
||||
@@ -147,26 +149,38 @@
|
||||
[_delegate installerIsRequestingAbortInstallWithError:installerError];
|
||||
}
|
||||
|
||||
- (id<SUInstallerConnectionProtocol>)createInstallerConnectionAndGetServiceName:(NSString * __autoreleasing *)outServiceName SPU_OBJC_DIRECT
|
||||
{
|
||||
NSString *hostBundleIdentifier = _host.bundle.bundleIdentifier;
|
||||
assert(hostBundleIdentifier != nil);
|
||||
|
||||
id<SUInstallerConnectionProtocol> installerConnection;
|
||||
#if INSTALLER_CONNECTION_XPC_SERVICE_EMBEDDED
|
||||
if (SPUXPCServiceIsEnabled(SUEnableInstallerConnectionServiceKey)) {
|
||||
installerConnection = [[SUXPCInstallerConnection alloc] initWithDelegate:self];
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
installerConnection = [[SUInstallerConnection alloc] initWithDelegate:self remote:NO];
|
||||
}
|
||||
|
||||
if (outServiceName != NULL) {
|
||||
*outServiceName = SPUInstallerServiceNameForBundleIdentifier(hostBundleIdentifier);
|
||||
}
|
||||
return installerConnection;
|
||||
}
|
||||
|
||||
- (void)setUpConnection SPU_OBJC_DIRECT
|
||||
{
|
||||
if (_installerConnection != nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *hostBundleIdentifier = _host.bundle.bundleIdentifier;
|
||||
assert(hostBundleIdentifier != nil);
|
||||
NSString *serviceName = nil;
|
||||
_installerConnection = [self createInstallerConnectionAndGetServiceName:&serviceName];
|
||||
|
||||
BOOL usingInstallerService;
|
||||
#if INSTALLER_CONNECTION_XPC_SERVICE_EMBEDDED
|
||||
if (SPUXPCServiceIsEnabled(SUEnableInstallerConnectionServiceKey)) {
|
||||
_installerConnection = [[SUXPCInstallerConnection alloc] initWithDelegate:self];
|
||||
usingInstallerService = YES;
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
_installerConnection = [[SUInstallerConnection alloc] initWithDelegate:self remote:NO];
|
||||
usingInstallerService = NO;
|
||||
}
|
||||
// Check if we're using remote SUXPCInstallerConnection for diagnostic purposes
|
||||
BOOL usingInstallerService = ![(id<NSObject>)_installerConnection isKindOfClass:SUInstallerConnection.class];
|
||||
|
||||
__weak __typeof__(self) weakSelf = self;
|
||||
[_installerConnection setInvalidationHandler:^{
|
||||
@@ -195,7 +209,6 @@
|
||||
});
|
||||
}];
|
||||
|
||||
NSString *serviceName = SPUInstallerServiceNameForBundleIdentifier(hostBundleIdentifier);
|
||||
NSString *installationType = _updateItem.installationType;
|
||||
assert(installationType != nil);
|
||||
|
||||
@@ -211,7 +224,15 @@
|
||||
_currentStage = SPUInstallerNotStarted;
|
||||
|
||||
if (_installerConnection == nil) {
|
||||
[self launchAutoUpdateSilently:silently completion:completionHandler];
|
||||
if (_cancelSupersededInstaller) {
|
||||
_cancelSupersededInstaller = NO;
|
||||
|
||||
[self cancelSupersededInstallerWithCompletion:^{
|
||||
[self launchAutoUpdateSilently:silently completion:completionHandler];
|
||||
}];
|
||||
} else {
|
||||
[self launchAutoUpdateSilently:silently completion:completionHandler];
|
||||
}
|
||||
} else {
|
||||
// The Install tool is already alive; just send out installation input data again
|
||||
[self sendInstallationData];
|
||||
@@ -225,6 +246,35 @@
|
||||
_systemDomain = systemDomain;
|
||||
}
|
||||
|
||||
- (void)cancelSupersededInstallerBeforeLaunching
|
||||
{
|
||||
_cancelSupersededInstaller = YES;
|
||||
}
|
||||
|
||||
- (void)cancelSupersededInstallerWithCompletion:(void (^)(void))completionHandler SPU_OBJC_DIRECT
|
||||
{
|
||||
NSString *serviceName = nil;
|
||||
id<SUInstallerConnectionProtocol> installerConnection = [self createInstallerConnectionAndGetServiceName:&serviceName];
|
||||
|
||||
__block BOOL completed = NO;
|
||||
void (^completeCancellation)(void) = ^{
|
||||
if (!completed) {
|
||||
completed = YES;
|
||||
[installerConnection invalidate];
|
||||
completionHandler();
|
||||
}
|
||||
};
|
||||
|
||||
[installerConnection setInvalidationHandler:^{
|
||||
dispatch_async(dispatch_get_main_queue(), completeCancellation);
|
||||
}];
|
||||
|
||||
[installerConnection setServiceName:serviceName systemDomain:_systemDomain];
|
||||
[installerConnection handleMessageWithIdentifier:SPUCancelInstallation data:[NSData data]];
|
||||
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CANCEL_SUPERSEDED_INSTALLER_TIMEOUT * NSEC_PER_SEC)), dispatch_get_main_queue(), completeCancellation);
|
||||
}
|
||||
|
||||
- (void)sendInstallationData SPU_OBJC_DIRECT
|
||||
{
|
||||
NSString *pathToRelaunch = _applicationBundle.bundlePath;
|
||||
|
||||
Vendored
+1
-2
@@ -361,8 +361,7 @@ static BOOL SPUUpdateRequiresUserAttentionBeforeDownloading(SUAppcastItem *updat
|
||||
return;
|
||||
}
|
||||
|
||||
// No need to discard the prior resumable installed update.
|
||||
// The update will be discarded when we submit new installer job
|
||||
[_installerDriver cancelSupersededInstallerBeforeLaunching];
|
||||
} else if (_resumableLocalUpdate != nil) {
|
||||
if (![self pendingUpdateItem:_resumableLocalUpdate.updateItem isSupersededByUpdateItem:updateItem]) {
|
||||
[self notifyFoundValidUpdateWithAppcastItem:_resumableLocalUpdate.updateItem secondaryAppcastItem:_resumableLocalUpdate.secondaryUpdateItem systemDomain:nil resumingExistingUpdate:YES];
|
||||
|
||||
Reference in new issue
Block a user