Add timeouts if the installer or updater don't respond back in timely matter

This is only for creating the initial connection
This commit is contained in:
Zorg committed 2016-07-13 22:28:36 -04:00
1 parent 97eb25b27f
commit 2c3996a522
4 files changed
+52 -10

No files matched your search

+17
View File
@@ -28,6 +28,8 @@
#import "SUErrors.h"
#import "SUInstallerCommunicationProtocol.h"
#define FIRST_UPDATER_MESSAGE_TIMEOUT 7ull
/*!
* Terminate the application after a delay from launching the new update to avoid OS activation issues
* This delay should be be high enough to increase the likelihood that our updated app will be launched up front,
@@ -60,6 +62,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7;
@property (nonatomic) id<SUInstallerProtocol> installer;
@property (nonatomic) BOOL willCompleteInstallation;
@property (nonatomic) BOOL receivedInstallationData;
@property (nonatomic) dispatch_queue_t installerQueue;
@property (nonatomic) BOOL performedStage1Installation;
@@ -83,6 +86,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7;
@synthesize shouldShowUI = _shouldShowUI;
@synthesize installer = _installer;
@synthesize willCompleteInstallation = _willCompleteInstallation;
@synthesize receivedInstallationData = _receivedInstallationData;
@synthesize installerQueue = _installerQueue;
@synthesize performedStage1Installation = _performedStage1Installation;
@synthesize performedStage2Installation = _performedStage2Installation;
@@ -153,6 +157,13 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7;
{
[self.xpcListener resume];
[self.statusInfo startListener];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(FIRST_UPDATER_MESSAGE_TIMEOUT * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (!self.receivedInstallationData) {
SULog(@"Timeout: installation data was never received");
[self cleanupAndExitWithStatus:EXIT_FAILURE];
}
});
}
/**
@@ -245,6 +256,8 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7;
- (void)extractAndInstallUpdate
{
[self.communicator handleMessageWithIdentifier:SUExtractionStarted data:[NSData data]];
NSString *downloadPath = [self.installationData.updateDirectoryPath stringByAppendingPathComponent:self.installationData.downloadName];
id <SUUnarchiverProtocol> unarchiver = [SUUnarchiver unarchiverForPath:downloadPath updatingHostBundlePath:self.host.bundlePath decryptionPassword:self.installationData.decryptionPassword delegate:self];
if (!unarchiver) {
@@ -295,6 +308,10 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7;
{
if (identifier == SUInstallationData && self.installationData == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
// Mark that we have received the installation data
// Do not rely on self.installationData != nil because we may set it to nil again if an early stage fails (i.e, archive extraction)
self.receivedInstallationData = YES;
SUInstallationInputData *installationData = (SUInstallationInputData *)SUUnarchiveRootObjectSecurely(data, [SUInstallationInputData class]);
if (installationData == nil) {
SULog(@"Error: Failed to unarchive input installation data");
+8 -7
View File
@@ -18,13 +18,14 @@ extern NSString *SUAppcastItemArchiveKey;
typedef NS_ENUM(int32_t, SUInstallerMessageType)
{
SUInstallerNotStarted = 0,
SUExtractedArchiveWithProgress = 1,
SUArchiveExtractionFailed = 2,
SUValidationStarted = 3,
SUInstallationStartedStage1 = 4,
SUInstallationFinishedStage1 = 5,
SUInstallationFinishedStage2 = 6,
SUInstallationFinishedStage3 = 7
SUExtractionStarted = 1,
SUExtractedArchiveWithProgress = 2,
SUArchiveExtractionFailed = 3,
SUValidationStarted = 4,
SUInstallationStartedStage1 = 5,
SUInstallationFinishedStage1 = 6,
SUInstallationFinishedStage2 = 7,
SUInstallationFinishedStage3 = 8
};
typedef NS_ENUM(int32_t, SUUpdaterMessageType)
+5 -2
View File
@@ -18,12 +18,15 @@ BOOL SUInstallerMessageTypeIsLegal(SUInstallerMessageType oldMessageType, SUInst
case SUInstallerNotStarted:
legal = (oldMessageType == SUInstallerNotStarted);
break;
case SUExtractionStarted:
legal = (oldMessageType == SUInstallerNotStarted);
break;
case SUExtractedArchiveWithProgress:
case SUArchiveExtractionFailed:
legal = (oldMessageType == SUInstallerNotStarted || oldMessageType == SUExtractedArchiveWithProgress);
legal = (oldMessageType == SUExtractionStarted || oldMessageType == SUExtractedArchiveWithProgress);
break;
case SUValidationStarted:
legal = (oldMessageType == SUInstallerNotStarted || oldMessageType == SUExtractedArchiveWithProgress);
legal = (oldMessageType == SUExtractionStarted || oldMessageType == SUExtractedArchiveWithProgress);
break;
case SUInstallationStartedStage1:
legal = (oldMessageType == SUValidationStarted);
+22 -1
View File
@@ -29,6 +29,8 @@
#error This is a "core" class and should NOT import AppKit
#endif
#define FIRST_INSTALLER_MESSAGE_TIMEOUT 7ull
@interface SUInstallerDriver () <SUInstallerCommunicationProtocol>
@property (nonatomic, readonly) SUHost *host;
@@ -40,6 +42,7 @@
@property (nonatomic) id<SUInstallerConnectionProtocol> installerConnection;
@property (nonatomic) NSUInteger extractionAttempts;
@property (nonatomic) BOOL postponedOnce;
@property (nonatomic, weak, readonly) id updater;
@property (nonatomic, weak, readonly) id<SUUpdaterDelegate> updaterDelegate;
@@ -60,6 +63,7 @@
@synthesize currentStage = _currentStage;
@synthesize startedInstalling = _startedInstalling;
@synthesize installerConnection = _installerConnection;
@synthesize extractionAttempts = _extractionAttempts;
@synthesize postponedOnce = _postponedOnce;
@synthesize updater = _updater;
@synthesize updaterDelegate = _updaterDelegate;
@@ -182,6 +186,20 @@
[self.installerConnection handleMessageWithIdentifier:SUInstallationData data:archivedData];
self.currentStage = SUInstallerNotStarted;
// If the number of extractions attempts stays the same, then we've waited too long and should abort the installation
// The extraction attempts is incremented when we receive an extraction should start message from the installer
// This also handles the case when a delta extraction fails and tries to re-try another extraction attempt later
// We will also want to make sure current stage is still SUInstallerNotStarted because it may not be due to resumability
NSUInteger currentExtractionAttempts = self.extractionAttempts;
__weak SUInstallerDriver *weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(FIRST_INSTALLER_MESSAGE_TIMEOUT * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
SUInstallerDriver *strongSelf = weakSelf;
if (strongSelf != nil && strongSelf.currentStage == SUInstallerNotStarted && currentExtractionAttempts == self.extractionAttempts) {
SULog(@"Timeout: Installer never started archive extraction");
[strongSelf.delegate installerIsRequestingAbortInstallWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey:SULocalizedString(@"An error occurred while starting the installer. Please try again later.", nil) }]];
}
});
}
- (void)handleMessageWithIdentifier:(int32_t)identifier data:(NSData *)data
@@ -198,7 +216,10 @@
return;
}
if (identifier == SUExtractedArchiveWithProgress) {
if (identifier == SUExtractionStarted) {
self.extractionAttempts++;
self.currentStage = identifier;
} else if (identifier == SUExtractedArchiveWithProgress) {
if (data.length == sizeof(double) && sizeof(double) == sizeof(uint64_t)) {
uint64_t progressValue = CFSwapInt64LittleToHost(*(const uint64_t *)data.bytes);
double progress = *(double *)&progressValue;