Reduce 0xdead10cc crashes.

This commit is contained in:
Matthew Chen 2018-04-11 13:09:44 -04:00
parent f01b7b9167
commit d561ba4c62
3 changed files with 50 additions and 9 deletions

View File

@ -285,10 +285,24 @@ public class SystemContactsFetcher: NSObject {
private func updateContacts(completion completionParam: ((Error?) -> Void)?, isUserRequested: Bool = false) {
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.
let completion = { error in
let completion: (Error?) -> Void = { error in
DispatchMainThreadSafe({
completionParam?(error)
backgroundTask = nil
})
}

View File

@ -19,6 +19,8 @@ public class ProfileFetcherJob: NSObject {
let ignoreThrottling: Bool
var backgroundTask: OWSBackgroundTask?
@objc
public class func run(thread: TSThread, networkManager: TSNetworkManager) {
ProfileFetcherJob(networkManager: networkManager).run(recipientIds: thread.recipientIdentifiers)
@ -38,6 +40,18 @@ public class ProfileFetcherJob: NSObject {
public func run(recipientIds: [String]) {
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) {
// 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.

View File

@ -310,9 +310,9 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
// We want a value that is just high enough to yield perf benefits.
const NSUInteger kIncomingMessageBatchSize = 32;
NSArray<OWSMessageContentJob *> *jobs = [self.finder nextJobsForBatchSize:kIncomingMessageBatchSize];
OWSAssert(jobs);
if (jobs.count < 1) {
NSArray<OWSMessageContentJob *> *batchJobs = [self.finder nextJobsForBatchSize:kIncomingMessageBatchSize];
OWSAssert(batchJobs);
if (batchJobs.count < 1) {
self.isDrainingQueue = NO;
DDLogVerbose(@"%@ Queue is drained", self.logTag);
return;
@ -320,15 +320,16 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
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;
DDLogVerbose(@"%@ completed %zd jobs. %zd jobs left.",
DDLogVerbose(@"%@ completed %zd/%zd jobs. %zd jobs left.",
self.logTag,
jobs.count,
processedJobs.count,
batchJobs.count,
[OWSMessageContentJob numberOfKeysInCollection]);
// 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);
NSMutableArray<OWSMessageContentJob *> *processedJobs = [NSMutableArray new];
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (OWSMessageContentJob *job in jobs) {
[self.messagesManager processEnvelope:job.envelopeProto
plaintextData:job.plaintextData
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