Reduce 0xdead10cc crashes.

pull/1/head
Matthew Chen 7 years ago
parent f01b7b9167
commit d561ba4c62

@ -285,10 +285,24 @@ public class SystemContactsFetcher: NSObject {
private func updateContacts(completion completionParam: ((Error?) -> Void)?, isUserRequested: Bool = false) { private func updateContacts(completion completionParam: ((Error?) -> Void)?, isUserRequested: Bool = false) {
AssertIsOnMainThread() AssertIsOnMainThread()
var backgroundTask: OWSBackgroundTask? = OWSBackgroundTask(label: "\(#function)", completionBlock: { [weak self] status in
AssertIsOnMainThread()
guard status == .expired else {
return
}
guard let _ = self else {
return
}
Logger.error("background task time ran out contacts fetch completed.")
})
// Ensure completion is invoked on main thread. // Ensure completion is invoked on main thread.
let completion = { error in let completion: (Error?) -> Void = { error in
DispatchMainThreadSafe({ DispatchMainThreadSafe({
completionParam?(error) completionParam?(error)
backgroundTask = nil
}) })
} }

@ -19,6 +19,8 @@ public class ProfileFetcherJob: NSObject {
let ignoreThrottling: Bool let ignoreThrottling: Bool
var backgroundTask: OWSBackgroundTask?
@objc @objc
public class func run(thread: TSThread, networkManager: TSNetworkManager) { public class func run(thread: TSThread, networkManager: TSNetworkManager) {
ProfileFetcherJob(networkManager: networkManager).run(recipientIds: thread.recipientIdentifiers) ProfileFetcherJob(networkManager: networkManager).run(recipientIds: thread.recipientIdentifiers)
@ -38,6 +40,18 @@ public class ProfileFetcherJob: NSObject {
public func run(recipientIds: [String]) { public func run(recipientIds: [String]) {
AssertIsOnMainThread() AssertIsOnMainThread()
backgroundTask = OWSBackgroundTask(label: "\(#function)", completionBlock: { [weak self] status in
AssertIsOnMainThread()
guard status == .expired else {
return
}
guard let _ = self else {
return
}
Logger.error("background task time ran out before profile fetch completed.")
})
if (!CurrentAppContext().isMainApp) { if (!CurrentAppContext().isMainApp) {
// Only refresh profiles in the MainApp to decrease the chance of missed SN notifications // Only refresh profiles in the MainApp to decrease the chance of missed SN notifications
// in the AppExtension for our users who choose not to verify contacts. // in the AppExtension for our users who choose not to verify contacts.

@ -310,9 +310,9 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
// We want a value that is just high enough to yield perf benefits. // We want a value that is just high enough to yield perf benefits.
const NSUInteger kIncomingMessageBatchSize = 32; const NSUInteger kIncomingMessageBatchSize = 32;
NSArray<OWSMessageContentJob *> *jobs = [self.finder nextJobsForBatchSize:kIncomingMessageBatchSize]; NSArray<OWSMessageContentJob *> *batchJobs = [self.finder nextJobsForBatchSize:kIncomingMessageBatchSize];
OWSAssert(jobs); OWSAssert(batchJobs);
if (jobs.count < 1) { if (batchJobs.count < 1) {
self.isDrainingQueue = NO; self.isDrainingQueue = NO;
DDLogVerbose(@"%@ Queue is drained", self.logTag); DDLogVerbose(@"%@ Queue is drained", self.logTag);
return; return;
@ -320,15 +320,16 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
OWSBackgroundTask *backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__]; OWSBackgroundTask *backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
[self processJobs:jobs]; NSArray<OWSMessageContentJob *> *processedJobs = [self processJobs:batchJobs];
[self.finder removeJobsWithIds:jobs.uniqueIds]; [self.finder removeJobsWithIds:processedJobs.uniqueIds];
backgroundTask = nil; backgroundTask = nil;
DDLogVerbose(@"%@ completed %zd jobs. %zd jobs left.", DDLogVerbose(@"%@ completed %zd/%zd jobs. %zd jobs left.",
self.logTag, self.logTag,
jobs.count, processedJobs.count,
batchJobs.count,
[OWSMessageContentJob numberOfKeysInCollection]); [OWSMessageContentJob numberOfKeysInCollection]);
// Wait a bit in hopes of increasing the batch size. // Wait a bit in hopes of increasing the batch size.
@ -340,17 +341,29 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
}); });
} }
- (void)processJobs:(NSArray<OWSMessageContentJob *> *)jobs - (NSArray<OWSMessageContentJob *> *)processJobs:(NSArray<OWSMessageContentJob *> *)jobs
{ {
AssertOnDispatchQueue(self.serialQueue); AssertOnDispatchQueue(self.serialQueue);
NSMutableArray<OWSMessageContentJob *> *processedJobs = [NSMutableArray new];
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (OWSMessageContentJob *job in jobs) { for (OWSMessageContentJob *job in jobs) {
[self.messagesManager processEnvelope:job.envelopeProto [self.messagesManager processEnvelope:job.envelopeProto
plaintextData:job.plaintextData plaintextData:job.plaintextData
transaction:transaction]; transaction:transaction];
[processedJobs addObject:job];
if (CurrentAppContext().isInBackground) {
// If the app is in the background, stop processing this batch.
//
// Since this check is done after processing jobs, we'll continue
// to process jobs in batches of 1. This reduces the cost of
// being interrupted and rolled back if app is suspended.
break;
}
} }
}]; }];
return processedJobs;
} }
@end @end

Loading…
Cancel
Save