Skip to content

Commit

Permalink
fix(messaging, ios): serialize access to background handler state
Browse files Browse the repository at this point in the history
this should make race conditions impossible, in case that was the cause
for a recent crash

note that the 25 second timeout block did *not* execute in testing if
the completion handler was called - the app went back to sleep immediately,
so no create_dispatch_block / dispatch_block_cancel was needed
  • Loading branch information
mikehardy committed Feb 19, 2025
1 parent 153637d commit 4d166ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions packages/messaging/ios/RNFBMessaging/RNFBMessaging+AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,16 @@ - (void)application:(UIApplication *)application
// If app is in background state, register background task to guarantee async queues aren't
// frozen.
sharedInstance.backgroundTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
if (sharedInstance.backgroundTaskId != UIBackgroundTaskInvalid) {
[application endBackgroundTask:sharedInstance.backgroundTaskId];
sharedInstance.backgroundTaskId = UIBackgroundTaskInvalid;
}
dispatch_get_main_queue(), ^{
if (sharedInstance.backgroundTaskId != UIBackgroundTaskInvalid) {
[application endBackgroundTask:sharedInstance.backgroundTaskId];
sharedInstance.backgroundTaskId = UIBackgroundTaskInvalid;
}
};
}];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(25 * NSEC_PER_SEC)),
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
dispatch_get_main_queue(), ^{
if (sharedInstance.completionHandler) {
sharedInstance.completionHandler(UIBackgroundFetchResultNewData);
sharedInstance.completionHandler = nil;
Expand Down
4 changes: 2 additions & 2 deletions packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ - (NSDictionary *)constantsToExport {
}

RCT_EXPORT_METHOD(completeNotificationProcessing) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
dispatch_get_main_queue(), ^{
RNFBMessagingAppDelegate *appDelegate = [RNFBMessagingAppDelegate sharedInstance];
if (appDelegate.completionHandler) {
appDelegate.completionHandler(UIBackgroundFetchResultNewData);
Expand All @@ -230,7 +230,7 @@ - (NSDictionary *)constantsToExport {
[[UIApplication sharedApplication] endBackgroundTask:appDelegate.backgroundTaskId];
appDelegate.backgroundTaskId = UIBackgroundTaskInvalid;
}
});
};
}

RCT_EXPORT_METHOD(requestPermission
Expand Down

0 comments on commit 4d166ca

Please sign in to comment.