From ef34cd5d585f441198f5ad752ff5fbb41bd4c068 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 26 Mar 2018 12:55:17 -0400 Subject: [PATCH] Fix 'invalid auth can hang on launch' issue. --- .../OWS106EnsureProfileComplete.swift | 30 +++++++++++++++---- SignalServiceKit/src/Util/AppReadiness.m | 2 ++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/SignalMessaging/environment/migrations/OWS106EnsureProfileComplete.swift b/SignalMessaging/environment/migrations/OWS106EnsureProfileComplete.swift index d01092f12..3e88834b6 100644 --- a/SignalMessaging/environment/migrations/OWS106EnsureProfileComplete.swift +++ b/SignalMessaging/environment/migrations/OWS106EnsureProfileComplete.swift @@ -21,9 +21,14 @@ public class OWS106EnsureProfileComplete: OWSDatabaseMigration { // Overriding runUp since we have some specific completion criteria which // is more likely to fail since it involves network requests. override public func runUp(completion:@escaping ((Void)) -> Void) { - let job = CompleteRegistrationFixerJob(completionHandler: { - Logger.info("\(self.TAG) Completed. Saving.") - self.save() + let job = CompleteRegistrationFixerJob(completionHandler: { (didSucceed) in + + if (didSucceed) { + Logger.info("\(self.TAG) Completed. Saving.") + self.save() + } else { + Logger.error("\(self.TAG) Failed. Saving.") + } completion() }) @@ -46,9 +51,9 @@ public class OWS106EnsureProfileComplete: OWSDatabaseMigration { static let kRetryInterval: TimeInterval = 5 * 60 var timer: Timer? - let completionHandler: () -> Void + let completionHandler: (Bool) -> Void - init(completionHandler: @escaping () -> Void) { + init(completionHandler: @escaping (Bool) -> Void) { self.completionHandler = completionHandler } @@ -69,8 +74,21 @@ public class OWS106EnsureProfileComplete: OWSDatabaseMigration { Logger.info("\(strongSelf.TAG) complete. Canceling timer and saving.") isCompleted = true aTimer.invalidate() - strongSelf.completionHandler() + strongSelf.completionHandler(true) }.catch { error in + let nserror = error as NSError + if nserror.domain == TSNetworkManagerDomain { + // Don't retry if we had an unrecoverable error. + // In particular, 401 (invalid auth) is unrecoverable. + let isUnrecoverableError = (400 <= nserror.code) && (nserror.code <= 599) + if isUnrecoverableError { + Logger.error("\(strongSelf.TAG) failed due to unrecoverable error: \(error). Aborting.") + aTimer.invalidate() + strongSelf.completionHandler(false) + return + } + } + Logger.error("\(strongSelf.TAG) failed with \(error). We'll try again in \(CompleteRegistrationFixerJob.kRetryInterval) seconds.") }.retainUntilComplete() } diff --git a/SignalServiceKit/src/Util/AppReadiness.m b/SignalServiceKit/src/Util/AppReadiness.m index 44a68cfe9..31491893d 100755 --- a/SignalServiceKit/src/Util/AppReadiness.m +++ b/SignalServiceKit/src/Util/AppReadiness.m @@ -80,6 +80,8 @@ NS_ASSUME_NONNULL_BEGIN OWSAssertIsOnMainThread(); OWSAssert(!self.isAppReady); + DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); + self.isAppReady = YES; [self runAppReadyBlocks];