diff --git a/Sparkle/Autoupdate/AppInstaller.m b/Sparkle/Autoupdate/AppInstaller.m index a7478ab3..2091792c 100644 --- a/Sparkle/Autoupdate/AppInstaller.m +++ b/Sparkle/Autoupdate/AppInstaller.m @@ -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 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 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"); diff --git a/Sparkle/Autoupdate/SUMessageTypes.h b/Sparkle/Autoupdate/SUMessageTypes.h index 4573cacb..60a21aa9 100644 --- a/Sparkle/Autoupdate/SUMessageTypes.h +++ b/Sparkle/Autoupdate/SUMessageTypes.h @@ -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) diff --git a/Sparkle/Autoupdate/SUMessageTypes.m b/Sparkle/Autoupdate/SUMessageTypes.m index 3c68b2ab..74d1db45 100644 --- a/Sparkle/Autoupdate/SUMessageTypes.m +++ b/Sparkle/Autoupdate/SUMessageTypes.m @@ -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); diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index f780ecd3..b5a7d20c 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -29,6 +29,8 @@ #error This is a "core" class and should NOT import AppKit #endif +#define FIRST_INSTALLER_MESSAGE_TIMEOUT 7ull + @interface SUInstallerDriver () @property (nonatomic, readonly) SUHost *host; @@ -40,6 +42,7 @@ @property (nonatomic) id installerConnection; +@property (nonatomic) NSUInteger extractionAttempts; @property (nonatomic) BOOL postponedOnce; @property (nonatomic, weak, readonly) id updater; @property (nonatomic, weak, readonly) id 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;