From 7b95939fb317d5e8b3385aebec82e3ecfd28975b Mon Sep 17 00:00:00 2001 From: Thilo Molitor Date: Sun, 29 Dec 2024 10:37:40 +0100 Subject: [PATCH] Request background task before disconnecting This makes sure we get it even when killed by some watchdog while disconnecting/shutting down --- Monal/Classes/MonalAppDelegate.m | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Monal/Classes/MonalAppDelegate.m b/Monal/Classes/MonalAppDelegate.m index 00d1303d6..7a04a9b90 100644 --- a/Monal/Classes/MonalAppDelegate.m +++ b/Monal/Classes/MonalAppDelegate.m @@ -1412,8 +1412,8 @@ -(void) checkIfBackgroundTaskIsStillNeeded [DDLog flushLog]; DDLogVerbose(@"Setting _shutdownPending to YES..."); _shutdownPending = YES; - [[MLXMPPManager sharedInstance] disconnectAll]; //disconnect all accounts to prevent TCP buffer leaking [HelperTools scheduleBackgroundTask:NO]; //request bg fetch execution in BGFETCH_DEFAULT_INTERVAL seconds + [[MLXMPPManager sharedInstance] disconnectAll]; //disconnect all accounts to prevent TCP buffer leaking [HelperTools dispatchAsync:NO reentrantOnQueue:dispatch_get_main_queue() withBlock:^{ BOOL stopped = NO; //make sure this will be done only once, even if we have an uikit bgtask and a bg fetch running simultaneously @@ -1490,13 +1490,13 @@ -(void) addBackgroundTask //this has to be before account disconnects, to detect which accounts are not idle (e.g. have a sync error) [HelperTools updateSyncErrorsWithDeleteOnly:NO andWaitForCompletion:YES]; - //disconnect all accounts to prevent TCP buffer leaking - [[MLXMPPManager sharedInstance] disconnectAll]; - //schedule a BGProcessingTaskRequest to process this further as soon as possible //(if we end up here, the graceful shuttdown did not work out because we are not idle --> we need more cpu time) [HelperTools scheduleBackgroundTask:YES]; //force as soon as possible + //disconnect all accounts to prevent TCP buffer leaking + [[MLXMPPManager sharedInstance] disconnectAll]; + //notify about pending app freeze (don't queue this notification because it should be handled IMMEDIATELY and INLINE) DDLogVerbose(@"Posting kMonalWillBeFreezed notification now..."); [[NSNotificationCenter defaultCenter] postNotificationName:kMonalWillBeFreezed object:nil]; @@ -1549,13 +1549,13 @@ -(void) handleBackgroundProcessingTask:(BGTask*) task //this has to be before account disconnects, to detect which accounts are not idle (e.g. have a sync error) [HelperTools updateSyncErrorsWithDeleteOnly:YES andWaitForCompletion:YES]; - //disconnect all accounts to prevent TCP buffer leaking - [[MLXMPPManager sharedInstance] disconnectAll]; - //schedule a new BGProcessingTaskRequest to process this further as soon as possible //(if we end up here, the graceful shuttdown did not work out because we are not idle --> we need more cpu time) [HelperTools scheduleBackgroundTask:YES]; //force as soon as possible + //disconnect all accounts to prevent TCP buffer leaking + [[MLXMPPManager sharedInstance] disconnectAll]; + //notify about pending app freeze (don't queue this notification because it should be handled IMMEDIATELY and INLINE) DDLogVerbose(@"Posting kMonalWillBeFreezed notification now..."); [[NSNotificationCenter defaultCenter] postNotificationName:kMonalWillBeFreezed object:nil]; @@ -1653,13 +1653,13 @@ -(void) handleBackgroundRefreshingTask:(BGTask*) task //this has to be before account disconnects, to detect which accounts are not idle (e.g. have a sync error) [HelperTools updateSyncErrorsWithDeleteOnly:YES andWaitForCompletion:YES]; - //disconnect all accounts to prevent TCP buffer leaking - [[MLXMPPManager sharedInstance] disconnectAll]; - //schedule a new BGProcessingTaskRequest to process this further as soon as possible //(if we end up here, the graceful shuttdown did not work out because we are not idle --> we need more cpu time) [HelperTools scheduleBackgroundTask:YES]; //force as soon as possible + //disconnect all accounts to prevent TCP buffer leaking + [[MLXMPPManager sharedInstance] disconnectAll]; + //notify about pending app freeze (don't queue this notification because it should be handled IMMEDIATELY and INLINE) DDLogVerbose(@"Posting kMonalWillBeFreezed notification now..."); [[NSNotificationCenter defaultCenter] postNotificationName:kMonalWillBeFreezed object:nil]; @@ -1880,13 +1880,13 @@ -(void) incomingWakeupWithCompletionHandler:(void (^)(UIBackgroundFetchResult re BOOL wasIdle = [[MLXMPPManager sharedInstance] allAccountsIdle] && [MLFiletransfer isIdle]; [HelperTools updateSyncErrorsWithDeleteOnly:NO andWaitForCompletion:YES]; - //disconnect all accounts to prevent TCP buffer leaking - [[MLXMPPManager sharedInstance] disconnectAll]; - //schedule a new BGProcessingTaskRequest to process this further as soon as possible, if we are not idle //(if we end up here, the graceful shuttdown did not work out because we are not idle --> we need more cpu time) [HelperTools scheduleBackgroundTask:!wasIdle]; + //disconnect all accounts to prevent TCP buffer leaking + [[MLXMPPManager sharedInstance] disconnectAll]; + //notify about pending app freeze (don't queue this notification because it should be handled IMMEDIATELY and INLINE) DDLogVerbose(@"Posting kMonalWillBeFreezed notification now..."); [[NSNotificationCenter defaultCenter] postNotificationName:kMonalWillBeFreezed object:nil];