From 4cb6719c6b3e9ddbb089840e7fb342d840208727 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 6 Aug 2022 11:28:47 -0700 Subject: [PATCH] Critical update alerts may not show up as promptly as they should when automatically installing them (#2221) Wait for appcast item registration before finishing automatic update driver. This fixes a potential race issue where sometimes the automatic update driver on completion would sometimes not trigger to prompt an update alert immediately for critical updates. Note in this case, the update would still be installed on app termination and would still be scheduled to alert the user on the regular update check interval, so this issue is not severe. --- Autoupdate/AppInstaller.m | 21 ++++++++++++++++++- Autoupdate/SPUMessageTypes.h | 3 ++- Autoupdate/SPUMessageTypes.m | 7 +++++-- .../InstallerProgressAppController.m | 4 +++- .../SPUInstallerAgentProtocol.h | 2 +- Sparkle/SPUInstallerDriver.m | 13 +++++++++++- 6 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 2223f389..3d69c0eb 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -34,6 +34,7 @@ #define FIRST_UPDATER_MESSAGE_TIMEOUT 18ull #define RETRIEVE_PROCESS_IDENTIFIER_TIMEOUT 8ull +#define UPDATER_APPCAST_ITEM_REGISTRATION_TIMEOUT 8ull /** * Show display progress UI after a delay from starting the final part of the installation. @@ -425,7 +426,25 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; NSData *archivedData = SPUArchiveRootObjectSecurely(installationInfo); if (archivedData != nil) { - [self.agentConnection.agent registerInstallationInfoData:archivedData]; + __block BOOL notifiedRegistrationCompleted = NO; + + [self.agentConnection.agent registerInstallationInfoData:archivedData completionHandler:^{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (!notifiedRegistrationCompleted) { + notifiedRegistrationCompleted = YES; + [self.communicator handleMessageWithIdentifier:SPUInstallerRegisteredAppcastItem data:[NSData data]]; + } + }); + }]; + + // Not receiving that we've registered the appcast item in a timely manner is not a fatal issue + // If this operation takes too long, just let assume the item will be registered + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(UPDATER_APPCAST_ITEM_REGISTRATION_TIMEOUT * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + if (!notifiedRegistrationCompleted) { + notifiedRegistrationCompleted = YES; + [self.communicator handleMessageWithIdentifier:SPUInstallerRegisteredAppcastItem data:[NSData data]]; + } + }); } } } else if (identifier == SPUResumeInstallationToStage2 && data.length == sizeof(uint8_t) * 2) { diff --git a/Autoupdate/SPUMessageTypes.h b/Autoupdate/SPUMessageTypes.h index d938ed9b..ee166559 100644 --- a/Autoupdate/SPUMessageTypes.h +++ b/Autoupdate/SPUMessageTypes.h @@ -23,7 +23,8 @@ typedef NS_ENUM(int32_t, SPUInstallerMessageType) SPUInstallationFinishedStage2 = 7, SPUInstallationFinishedStage3 = 8, SPUUpdaterAlivePing = 9, - SPUInstallerError = 10 + SPUInstallerError = 10, + SPUInstallerRegisteredAppcastItem = 11, }; typedef NS_ENUM(int32_t, SPUUpdaterMessageType) diff --git a/Autoupdate/SPUMessageTypes.m b/Autoupdate/SPUMessageTypes.m index 132b3d97..b09390c3 100644 --- a/Autoupdate/SPUMessageTypes.m +++ b/Autoupdate/SPUMessageTypes.m @@ -23,7 +23,7 @@ BOOL SPUInstallerMessageTypeIsLegal(SPUInstallerMessageType oldMessageType, SPUInstallerMessageType newMessageType) { - BOOL legal; + BOOL legal = NO; switch (newMessageType) { case SPUInstallerNotStarted: legal = (oldMessageType == SPUInstallerNotStarted); @@ -45,7 +45,7 @@ BOOL SPUInstallerMessageTypeIsLegal(SPUInstallerMessageType oldMessageType, SPUI legal = (oldMessageType == SPUInstallationStartedStage1); break; case SPUInstallationFinishedStage2: - legal = (oldMessageType == SPUInstallationFinishedStage1); + legal = (oldMessageType == SPUInstallationFinishedStage1 || oldMessageType == SPUInstallerRegisteredAppcastItem); break; case SPUInstallationFinishedStage3: legal = (oldMessageType == SPUInstallationFinishedStage2); @@ -56,6 +56,9 @@ BOOL SPUInstallerMessageTypeIsLegal(SPUInstallerMessageType oldMessageType, SPUI // So just always allow these type of messages legal = YES; break; + case SPUInstallerRegisteredAppcastItem: + legal = (oldMessageType == SPUInstallationFinishedStage1); + break; } return legal; } diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index d1119308..d984690a 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -284,7 +284,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; }); } -- (void)registerInstallationInfoData:(NSData *)installationInfoData +- (void)registerInstallationInfoData:(NSData *)installationInfoData completionHandler:(void (^)(void))completionHandler { dispatch_async(dispatch_get_main_queue(), ^{ if (self.statusInfo.installationInfoData == nil && installationInfoData != nil) { @@ -297,6 +297,8 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; SULog(SULogLevelError, @"Error: Failed to decode initial installation info from installer: %@", installationInfoData); } } + + completionHandler(); }); } diff --git a/Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h b/Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h index e5b52ee1..cc25e61b 100644 --- a/Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h +++ b/Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h @@ -14,7 +14,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)registerApplicationBundlePath:(NSString *)applicationBundlePath reply:(void (^)(NSNumber * _Nullable processIdentifier))reply; -- (void)registerInstallationInfoData:(NSData *)installationInfoData; +- (void)registerInstallationInfoData:(NSData *)installationInfoData completionHandler:(void (^)(void))completionHandler; - (void)sendTerminationSignal; diff --git a/Sparkle/SPUInstallerDriver.m b/Sparkle/SPUInstallerDriver.m index 36bfdf12..f290a1eb 100644 --- a/Sparkle/SPUInstallerDriver.m +++ b/Sparkle/SPUInstallerDriver.m @@ -66,6 +66,8 @@ @implementation SPUInstallerDriver { void (^_updateWillInstallHandler)(void); + BOOL _canInstallSilently; + BOOL _hasTargetTerminated; } @synthesize host = _host; @@ -327,7 +329,16 @@ hasTargetTerminated = (BOOL)*((const uint8_t *)data.bytes + 1); } - [self.delegate installerDidFinishPreparationAndWillInstallImmediately:hasTargetTerminated silently:canInstallSilently]; + // The installer letting us know when the appcast item was registered (SPUInstallerRegisteredAppcastItem) + // was added later. In earlier versions of Sparkle, we could have notified the update too early + // before the installer finished registering the appcast item. + // Note we don't need to handle an "old" installer because Sparkle and the installer should still be aligned at this point. + _canInstallSilently = canInstallSilently; + _hasTargetTerminated = hasTargetTerminated; + } else if (identifier == SPUInstallerRegisteredAppcastItem) { + self.currentStage = identifier; + + [self.delegate installerDidFinishPreparationAndWillInstallImmediately:_hasTargetTerminated silently:_canInstallSilently]; } else if (identifier == SPUInstallationFinishedStage2) { self.currentStage = identifier;