Skip to content

Commit

Permalink
Fix locking deadlock on app resume
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Dec 16, 2024
1 parent 61cd5ef commit d0ebcae
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
61 changes: 49 additions & 12 deletions Monal/Classes/HelperTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,23 @@ @interface MLDelayableTimer()
-(void) invalidate;
@end

//make method visible
@interface DDLog()
-(void) queueLogMessage:(DDLogMessage*) logMessage asynchronously:(BOOL) asyncFlag;
@end

@interface DDLog (AllowQueueFreeze)
-(void) swizzled_queueLogMessage:(DDLogMessage*) logMessage asynchronously:(BOOL) asyncFlag;
@end

static char* _crashBundleName = "UnifiedReport";
static NSString* _processID;
static DDFileLogger* _fileLogger = nil;
static char _origLogfilePath[1024] = "";
static char _logfilePath[1024] = "";
static NSObject* _isAppExtensionLock = nil;
static NSObject* _suspensionHandlingLock = nil;
static BOOL _suspensionHandlingIsSuspended = NO;
static NSObject* _suspensionHandling_lock = nil;
static BOOL _suspensionHandling_isSuspended = NO;
static NSMutableDictionary* _versionInfoCache;
static MLStreamRedirect* _stdoutRedirector = nil;
static MLStreamRedirect* _stderrRedirector = nil;
Expand Down Expand Up @@ -293,12 +302,36 @@ -(id) initWithObj:(id) obj
}
@end

@implementation DDLog (AllowQueueFreeze)

-(void) swizzled_queueLogMessage:(DDLogMessage*) logMessage asynchronously:(BOOL) asyncFlag
{
//don't do sync logging for any message (usually ERROR), while the global logging queue is suspended
@synchronized(_suspensionHandling_lock) {
return [self swizzled_queueLogMessage:logMessage asynchronously:_suspensionHandling_isSuspended ? YES : asyncFlag];
}
}

//see https://stackoverflow.com/a/13326633 and https://fek.io/blog/method-swizzling-in-obj-c-and-swift/
+(void) load
{
if(self == DDLog.self)
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
swizzle([self class], @selector(queueLogMessage:asynchronously:), @selector(swizzled_queueLogMessage:asynchronously:));
});
}
}

@end

@implementation HelperTools

+(void) initialize
{
_suspensionHandlingLock = [NSObject new];
_suspensionHandlingIsSuspended = NO;
_suspensionHandling_lock = [NSObject new];
_suspensionHandling_isSuspended = NO;
_isAppExtensionLock = [NSObject new];
_versionInfoCache = [NSMutableDictionary new];

Expand Down Expand Up @@ -1880,27 +1913,31 @@ +(void) flushLogsWithTimeout:(double) timeout

+(void) signalSuspension
{
@synchronized(_suspensionHandlingLock) {
if(!_suspensionHandlingIsSuspended)
@synchronized(_suspensionHandling_lock) {
if(!_suspensionHandling_isSuspended)
{
DDLogVerbose(@"Suspending logger queue...");
[HelperTools flushLogsWithTimeout:0.100];
dispatch_suspend([DDLog loggingQueue]);
_suspensionHandlingIsSuspended = YES;
_suspensionHandling_isSuspended = YES;

DDLogVerbose(@"Posting kMonalFrozen notification now...");
[[NSNotificationCenter defaultCenter] postNotificationName:kMonalFrozen object:nil];
}
}
DDLogVerbose(@"Posting kMonalIsFreezed notification now...");
[[NSNotificationCenter defaultCenter] postNotificationName:kMonalIsFreezed object:nil];
}

+(void) signalResumption
{
@synchronized(_suspensionHandlingLock) {
if(_suspensionHandlingIsSuspended)
@synchronized(_suspensionHandling_lock) {
if(_suspensionHandling_isSuspended)
{
DDLogVerbose(@"Resuming logger queue...");
dispatch_resume([DDLog loggingQueue]);
_suspensionHandlingIsSuspended = NO;
_suspensionHandling_isSuspended = NO;

DDLogVerbose(@"Posting kMonalUnfrozen notification now...");
[[NSNotificationCenter defaultCenter] postNotificationName:kMonalUnfrozen object:nil];
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Monal/Classes/MonalAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -1595,9 +1595,6 @@ -(void) handleBackgroundProcessingTask:(BGTask*) task
}];
};

//resume logging and other core tasks
[HelperTools signalResumption];

//only proceed with our BGTASK if the NotificationServiceExtension is not running
[MLProcessLock lock];
[[IPC sharedInstance] sendMessage:@"Monal.disconnectAll" withData:nil to:@"NotificationServiceExtension"];
Expand Down Expand Up @@ -1702,9 +1699,6 @@ -(void) handleBackgroundRefreshingTask:(BGTask*) task
}];
};

//resume logging and other core tasks
[HelperTools signalResumption];

//only proceed with our BGTASK if the NotificationServiceExtension is not running
[MLProcessLock lock];
[[IPC sharedInstance] sendMessage:@"Monal.disconnectAll" withData:nil to:@"NotificationServiceExtension"];
Expand Down Expand Up @@ -1748,6 +1742,9 @@ -(void) handleBackgroundRefreshingTask:(BGTask*) task
-(void) configureBackgroundTasks
{
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:kBackgroundProcessingTask usingQueue:dispatch_get_main_queue() launchHandler:^(BGTask *task) {
//resume logging and other core tasks
[HelperTools signalResumption];

DDLogDebug(@"RUNNING BGPROCESSING LAUNCH HANDLER");
DDLogInfo(@"BG time available: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
if(![HelperTools isInBackground])
Expand All @@ -1768,6 +1765,9 @@ -(void) configureBackgroundTasks
}];

[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:kBackgroundRefreshingTask usingQueue:dispatch_get_main_queue() launchHandler:^(BGTask *task) {
//resume logging and other core tasks
[HelperTools signalResumption];

DDLogDebug(@"RUNNING BGREFRESHING LAUNCH HANDLER");
DDLogInfo(@"BG time available: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
if(![HelperTools isInBackground])
Expand Down

0 comments on commit d0ebcae

Please sign in to comment.