From fd183985489b5cb2d2fcec59c8c9ea531b722bf6 Mon Sep 17 00:00:00 2001 From: Jesus Rojas Date: Fri, 10 Oct 2025 15:36:04 -0600 Subject: [PATCH 1/7] WIP!!! - Fix app start trace outliers from network delays (#10733) --- .../AppActivity/FPRAppActivityTracker.m | 68 ++++++++++++------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m index f6efa0972f9..e7a54626645 100644 --- a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m +++ b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m @@ -30,6 +30,7 @@ static NSDate *doubleDispatchTime = nil; static NSDate *applicationDidFinishLaunchTime = nil; static NSTimeInterval gAppStartMaxValidDuration = 60 * 60; // 60 minutes. +static NSTimeInterval gAppStartReasonableValidDuration = 30.0; // 30 seconds, reasonable app start time??? static FPRCPUGaugeData *gAppStartCPUGaugeData = nil; static FPRMemoryGaugeData *gAppStartMemoryGaugeData = nil; static BOOL isActivePrewarm = NO; @@ -71,6 +72,9 @@ @interface FPRAppActivityTracker () /** Tracks if the gauge metrics are dispatched. */ @property(nonatomic) BOOL appStartGaugeMetricDispatched; +/** Tracks if TTI stage has been started for this instance. */ +@property(nonatomic) BOOL ttiStageStarted; + /** Firebase Performance Configuration object */ @property(nonatomic) FPRConfigurations *configurations; @@ -135,6 +139,7 @@ - (instancetype)initAppActivityTracker { if (self != nil) { _applicationState = FPRApplicationStateUnknown; _appStartGaugeMetricDispatched = NO; + _ttiStageStarted = NO; _configurations = [FPRConfigurations sharedInstance]; [self startTrackingNetwork]; } @@ -250,7 +255,7 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { [self.appStartTrace startStageNamed:kFPRAppStartStageNameTimeToFirstDraw]; }); - // If ever the app start trace had it life in background stage, do not send the trace. + // If ever the app start trace had its life in background stage, do not send the trace. if (self.appStartTrace.backgroundTraceState != FPRTraceStateForegroundOnly) { self.appStartTrace = nil; } @@ -266,29 +271,46 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { self.foregroundSessionTrace = appTrace; // Start measuring time to make the app interactive on the App start trace. - static BOOL TTIStageStarted = NO; - if (!TTIStageStarted) { + if (!self.ttiStageStarted) { [self.appStartTrace startStageNamed:kFPRAppStartStageNameTimeToUserInteraction]; - TTIStageStarted = YES; - - // Assumption here is that - the app becomes interactive in the next runloop cycle. - // It is possible that the app does more things later, but for now we are not measuring that. - dispatch_async(dispatch_get_main_queue(), ^{ - NSTimeInterval startTimeSinceEpoch = [self.appStartTrace startTimeSinceEpoch]; - NSTimeInterval currentTimeSinceEpoch = [[NSDate date] timeIntervalSince1970]; - - // The below check is to account for 2 scenarios. - // 1. The app gets started in the background and might come to foreground a lot later. - // 2. The app is launched, but immediately backgrounded for some reason and the actual launch - // happens a lot later. - // Dropping the app start trace in such situations where the launch time is taking more than - // 60 minutes. This is an approximation, but a more agreeable timelimit for app start. - if ((currentTimeSinceEpoch - startTimeSinceEpoch < gAppStartMaxValidDuration) && - [self isAppStartEnabled] && ![self isApplicationPreWarmed]) { - [self.appStartTrace stop]; - } else { - [self.appStartTrace cancel]; - } + self.ttiStageStarted = YES; + + // Use dispatch_async with a higher priority queue to reduce interference from network + // operations This ensures trace completion isn't delayed by main queue congestion from network + // calls + __weak typeof(self) weakSelf = self; + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ + dispatch_async(dispatch_get_main_queue(), ^{ + __strong typeof(weakSelf) strongSelf = weakSelf; + if (!strongSelf || !strongSelf.appStartTrace) { + return; + } + + NSTimeInterval startTimeSinceEpoch = [strongSelf.appStartTrace startTimeSinceEpoch]; + NSTimeInterval currentTimeSinceEpoch = [[NSDate date] timeIntervalSince1970]; + NSTimeInterval elapsed = currentTimeSinceEpoch - startTimeSinceEpoch; + + // The below check accounts for multiple scenarios: + // 1. App started in background and comes to foreground later + // 2. App launched but immediately backgrounded + // 3. Network delays during startup inflating metrics + BOOL shouldCompleteTrace = (elapsed < gAppStartMaxValidDuration) && + [strongSelf isAppStartEnabled] && + ![strongSelf isApplicationPreWarmed]; + + // Additional safety: cancel if elapsed time is unreasonably long for app start + if (shouldCompleteTrace && elapsed < gAppStartReasonableValidDuration) { + [strongSelf.appStartTrace stop]; + } else { + [strongSelf.appStartTrace cancel]; + if (elapsed >= gAppStartReasonableValidDuration) { + // Log for debugging network related delays + NSLog( + @"Firebase Performance: App start trace cancelled due to excessive duration: %.2fs", + elapsed); + } + } + }); }); } } From 59a4cadc005f2e3d328ebdd4395b19fbca40b040 Mon Sep 17 00:00:00 2001 From: Jesus Rojas Date: Mon, 13 Oct 2025 13:27:05 -0600 Subject: [PATCH 2/7] Address suggestions plus actual background false start mitigation --- .../AppActivity/FPRAppActivityTracker.m | 95 ++++++++++++------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m index e7a54626645..71bf49b31e5 100644 --- a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m +++ b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m @@ -30,7 +30,8 @@ static NSDate *doubleDispatchTime = nil; static NSDate *applicationDidFinishLaunchTime = nil; static NSTimeInterval gAppStartMaxValidDuration = 60 * 60; // 60 minutes. -static NSTimeInterval gAppStartReasonableValidDuration = 30.0; // 30 seconds, reasonable app start time??? +static NSTimeInterval gAppStartReasonableValidDuration = + 30.0; // 30 seconds, reasonable app start time??? static FPRCPUGaugeData *gAppStartCPUGaugeData = nil; static FPRMemoryGaugeData *gAppStartMemoryGaugeData = nil; static BOOL isActivePrewarm = NO; @@ -117,6 +118,18 @@ + (void)windowDidBecomeVisible:(NSNotification *)notification { + (void)applicationDidFinishLaunching:(NSNotification *)notification { applicationDidFinishLaunchTime = [NSDate date]; + + // Detect a background launch and invalidate app start time + // this prevents we measure duration from background launch + UIApplicationState state = [UIApplication sharedApplication].applicationState; + if (state == UIApplicationStateBackground) { + // App launched in background so we invalidate the captured app start time + // to prevent incorrect measurement when user later opens the app + appStartTime = nil; + NSLog(@"Firebase Performance: Background launch detected. App start measurement will be " + @"skipped."); + } + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidFinishLaunchingNotification object:nil]; @@ -247,6 +260,14 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ + // Early bailout if background launch was detected, appStartTime will be nil if the app was + // launched in background + if (appStartTime == nil) { + NSLog(@"Firebase Performance: App start trace skipped due to background launch. " + @"This prevents reporting incorrect multi-minute/hour durations."); + return; + } + self.appStartTrace = [[FIRTrace alloc] initInternalTraceWithName:kFPRAppStartTraceName]; [self.appStartTrace startWithStartTime:appStartTime]; [self.appStartTrace startStageNamed:kFPRAppStartStageNameTimeToUI startTime:appStartTime]; @@ -256,8 +277,12 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { }); // If ever the app start trace had its life in background stage, do not send the trace. - if (self.appStartTrace.backgroundTraceState != FPRTraceStateForegroundOnly) { + if (self.appStartTrace && + self.appStartTrace.backgroundTraceState != FPRTraceStateForegroundOnly) { + [self.appStartTrace cancel]; self.appStartTrace = nil; + NSLog( + @"Firebase Performance: App start trace cancelled due to background state contamination."); } // Stop the active background session trace. @@ -271,46 +296,44 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { self.foregroundSessionTrace = appTrace; // Start measuring time to make the app interactive on the App start trace. - if (!self.ttiStageStarted) { + if (!self.ttiStageStarted && self.appStartTrace) { [self.appStartTrace startStageNamed:kFPRAppStartStageNameTimeToUserInteraction]; self.ttiStageStarted = YES; - // Use dispatch_async with a higher priority queue to reduce interference from network - // operations This ensures trace completion isn't delayed by main queue congestion from network - // calls + // Defer stopping the trace to the next run loop cycle, this ensures that the app is + // fully interactive and gives the UI a chance to settle before measuring completion. __weak typeof(self) weakSelf = self; - dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ - dispatch_async(dispatch_get_main_queue(), ^{ - __strong typeof(weakSelf) strongSelf = weakSelf; - if (!strongSelf || !strongSelf.appStartTrace) { - return; - } - - NSTimeInterval startTimeSinceEpoch = [strongSelf.appStartTrace startTimeSinceEpoch]; - NSTimeInterval currentTimeSinceEpoch = [[NSDate date] timeIntervalSince1970]; - NSTimeInterval elapsed = currentTimeSinceEpoch - startTimeSinceEpoch; - - // The below check accounts for multiple scenarios: - // 1. App started in background and comes to foreground later - // 2. App launched but immediately backgrounded - // 3. Network delays during startup inflating metrics - BOOL shouldCompleteTrace = (elapsed < gAppStartMaxValidDuration) && - [strongSelf isAppStartEnabled] && - ![strongSelf isApplicationPreWarmed]; - - // Additional safety: cancel if elapsed time is unreasonably long for app start - if (shouldCompleteTrace && elapsed < gAppStartReasonableValidDuration) { - [strongSelf.appStartTrace stop]; - } else { - [strongSelf.appStartTrace cancel]; - if (elapsed >= gAppStartReasonableValidDuration) { - // Log for debugging network related delays - NSLog( - @"Firebase Performance: App start trace cancelled due to excessive duration: %.2fs", + dispatch_async(dispatch_get_main_queue(), ^{ + __strong typeof(weakSelf) strongSelf = weakSelf; + if (!strongSelf || !strongSelf.appStartTrace) { + return; + } + + NSTimeInterval startTimeSinceEpoch = [strongSelf.appStartTrace startTimeSinceEpoch]; + NSTimeInterval currentTimeSinceEpoch = [[NSDate date] timeIntervalSince1970]; + NSTimeInterval elapsed = currentTimeSinceEpoch - startTimeSinceEpoch; + + // The below check accounts for multiple scenarios: + // 1. App started in background and comes to foreground later (60 min or more) + // 2. App launched but immediately backgrounded + // 3. Network delays during startup inflating metrics (30 sec or more) + // 4. iOS prewarm scenarios + BOOL shouldCompleteTrace = (elapsed < gAppStartMaxValidDuration) && + [strongSelf isAppStartEnabled] && + ![strongSelf isApplicationPreWarmed]; + + // Cancel if elapsed time is unreasonably long for app start this should catch network induced + // delays and other edge cases that slip through as am aditional safety check + if (shouldCompleteTrace && elapsed < gAppStartReasonableValidDuration) { + [strongSelf.appStartTrace stop]; + } else { + [strongSelf.appStartTrace cancel]; + if (elapsed >= gAppStartReasonableValidDuration) { + // Log for debugging network related delays + NSLog(@"Firebase Performance: App start trace cancelled due to excessive duration: %.2fs", elapsed); - } } - }); + } }); } } From 2fece14dc5b11f76d8d177ad994d00cdde5461de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Rojas?= Date: Mon, 13 Oct 2025 13:40:31 -0600 Subject: [PATCH 3/7] Typo fix Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m index 71bf49b31e5..25516d9fdd2 100644 --- a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m +++ b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m @@ -323,7 +323,7 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { ![strongSelf isApplicationPreWarmed]; // Cancel if elapsed time is unreasonably long for app start this should catch network induced - // delays and other edge cases that slip through as am aditional safety check + // delays and other edge cases that slip through as an additional safety check if (shouldCompleteTrace && elapsed < gAppStartReasonableValidDuration) { [strongSelf.appStartTrace stop]; } else { From 872a88f1475c9c04ad630acf4cf73b1931ad8aed Mon Sep 17 00:00:00 2001 From: Jesus Rojas Date: Mon, 13 Oct 2025 17:22:26 -0600 Subject: [PATCH 4/7] Reverted aggresive timer and stages, used FPRLogDebug --- .../AppActivity/FPRAppActivityTracker.m | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m index 71bf49b31e5..bc1651a5cb8 100644 --- a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m +++ b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m @@ -25,13 +25,12 @@ #import "FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector+Private.h" #import "FirebasePerformance/Sources/Timer/FIRTrace+Internal.h" #import "FirebasePerformance/Sources/Timer/FIRTrace+Private.h" +#import "FirebasePerformance/Sources/Common/FPRDiagnostics.h" static NSDate *appStartTime = nil; static NSDate *doubleDispatchTime = nil; static NSDate *applicationDidFinishLaunchTime = nil; static NSTimeInterval gAppStartMaxValidDuration = 60 * 60; // 60 minutes. -static NSTimeInterval gAppStartReasonableValidDuration = - 30.0; // 30 seconds, reasonable app start time??? static FPRCPUGaugeData *gAppStartCPUGaugeData = nil; static FPRMemoryGaugeData *gAppStartMemoryGaugeData = nil; static BOOL isActivePrewarm = NO; @@ -73,8 +72,8 @@ @interface FPRAppActivityTracker () /** Tracks if the gauge metrics are dispatched. */ @property(nonatomic) BOOL appStartGaugeMetricDispatched; -/** Tracks if TTI stage has been started for this instance. */ -@property(nonatomic) BOOL ttiStageStarted; +/** Tracks if app start trace completion logic has been executed. */ +@property(nonatomic) BOOL appStartTraceCompleted; /** Firebase Performance Configuration object */ @property(nonatomic) FPRConfigurations *configurations; @@ -126,8 +125,8 @@ + (void)applicationDidFinishLaunching:(NSNotification *)notification { // App launched in background so we invalidate the captured app start time // to prevent incorrect measurement when user later opens the app appStartTime = nil; - NSLog(@"Firebase Performance: Background launch detected. App start measurement will be " - @"skipped."); + FPRLogDebug(kFPRTraceNotCreated, + @"Background launch detected. App start measurement will be skipped."); } [[NSNotificationCenter defaultCenter] removeObserver:self @@ -152,7 +151,7 @@ - (instancetype)initAppActivityTracker { if (self != nil) { _applicationState = FPRApplicationStateUnknown; _appStartGaugeMetricDispatched = NO; - _ttiStageStarted = NO; + _appStartTraceCompleted = NO; _configurations = [FPRConfigurations sharedInstance]; [self startTrackingNetwork]; } @@ -263,8 +262,9 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { // Early bailout if background launch was detected, appStartTime will be nil if the app was // launched in background if (appStartTime == nil) { - NSLog(@"Firebase Performance: App start trace skipped due to background launch. " - @"This prevents reporting incorrect multi-minute/hour durations."); + FPRLogDebug(kFPRTraceNotCreated, + @"App start trace skipped due to background launch. " + @"This prevents reporting incorrect multi-minute/hour durations."); return; } @@ -281,8 +281,8 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { self.appStartTrace.backgroundTraceState != FPRTraceStateForegroundOnly) { [self.appStartTrace cancel]; self.appStartTrace = nil; - NSLog( - @"Firebase Performance: App start trace cancelled due to background state contamination."); + FPRLogDebug(kFPRTraceNotCreated, + @"App start trace cancelled due to background state contamination."); } // Stop the active background session trace. @@ -296,12 +296,12 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { self.foregroundSessionTrace = appTrace; // Start measuring time to make the app interactive on the App start trace. - if (!self.ttiStageStarted && self.appStartTrace) { + if (!self.appStartTraceCompleted && self.appStartTrace) { [self.appStartTrace startStageNamed:kFPRAppStartStageNameTimeToUserInteraction]; - self.ttiStageStarted = YES; + self.appStartTraceCompleted = YES; - // Defer stopping the trace to the next run loop cycle, this ensures that the app is - // fully interactive and gives the UI a chance to settle before measuring completion. + // Assumption here is that - the app becomes interactive in the next runloop cycle. + // It is possible that the app does more things later, but for now we are not measuring that. __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ __strong typeof(weakSelf) strongSelf = weakSelf; @@ -314,24 +314,21 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { NSTimeInterval elapsed = currentTimeSinceEpoch - startTimeSinceEpoch; // The below check accounts for multiple scenarios: - // 1. App started in background and comes to foreground later (60 min or more) - // 2. App launched but immediately backgrounded - // 3. Network delays during startup inflating metrics (30 sec or more) + // 1. App started in background and comes to foreground later + // 2. App launched but immediately backgrounded + // 3. Network delays during startup inflating metrics // 4. iOS prewarm scenarios BOOL shouldCompleteTrace = (elapsed < gAppStartMaxValidDuration) && [strongSelf isAppStartEnabled] && ![strongSelf isApplicationPreWarmed]; - // Cancel if elapsed time is unreasonably long for app start this should catch network induced - // delays and other edge cases that slip through as am aditional safety check - if (shouldCompleteTrace && elapsed < gAppStartReasonableValidDuration) { + if (shouldCompleteTrace) { [strongSelf.appStartTrace stop]; } else { [strongSelf.appStartTrace cancel]; - if (elapsed >= gAppStartReasonableValidDuration) { - // Log for debugging network related delays - NSLog(@"Firebase Performance: App start trace cancelled due to excessive duration: %.2fs", - elapsed); + if (elapsed >= gAppStartMaxValidDuration) { + FPRLogDebug(kFPRTraceInvalidName, + @"App start trace cancelled due to excessive duration: %.2fs", elapsed); } } }); From bd0378b9906b82856ed0bfb7fc8da5cd148a7b61 Mon Sep 17 00:00:00 2001 From: Jesus Rojas Date: Mon, 13 Oct 2025 17:25:49 -0600 Subject: [PATCH 5/7] Fix my horrible job of a merge --- .../Sources/AppActivity/FPRAppActivityTracker.m | 6 ------ 1 file changed, 6 deletions(-) diff --git a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m index 87de97e0f7e..bc1651a5cb8 100644 --- a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m +++ b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m @@ -322,13 +322,7 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { [strongSelf isAppStartEnabled] && ![strongSelf isApplicationPreWarmed]; -<<<<<<< HEAD if (shouldCompleteTrace) { -======= - // Cancel if elapsed time is unreasonably long for app start this should catch network induced - // delays and other edge cases that slip through as an additional safety check - if (shouldCompleteTrace && elapsed < gAppStartReasonableValidDuration) { ->>>>>>> origin/JesusRojas/#10733 [strongSelf.appStartTrace stop]; } else { [strongSelf.appStartTrace cancel]; From 12bd59eecbb22876665bdd9f870023d970bbf173 Mon Sep 17 00:00:00 2001 From: Jesus Rojas Date: Fri, 17 Oct 2025 11:45:22 -0600 Subject: [PATCH 6/7] Variable, comments and style check --- .../AppActivity/FPRAppActivityTracker.m | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m index bc1651a5cb8..fd38dd125fb 100644 --- a/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m +++ b/FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.m @@ -19,13 +19,13 @@ #import #import "FirebasePerformance/Sources/AppActivity/FPRSessionManager.h" +#import "FirebasePerformance/Sources/Common/FPRDiagnostics.h" #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h" #import "FirebasePerformance/Sources/Gauges/CPU/FPRCPUGaugeCollector+Private.h" #import "FirebasePerformance/Sources/Gauges/FPRGaugeManager.h" #import "FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector+Private.h" #import "FirebasePerformance/Sources/Timer/FIRTrace+Internal.h" #import "FirebasePerformance/Sources/Timer/FIRTrace+Private.h" -#import "FirebasePerformance/Sources/Common/FPRDiagnostics.h" static NSDate *appStartTime = nil; static NSDate *doubleDispatchTime = nil; @@ -125,7 +125,7 @@ + (void)applicationDidFinishLaunching:(NSNotification *)notification { // App launched in background so we invalidate the captured app start time // to prevent incorrect measurement when user later opens the app appStartTime = nil; - FPRLogDebug(kFPRTraceNotCreated, + FPRLogDebug(kFPRTraceNotCreated, @"Background launch detected. App start measurement will be skipped."); } @@ -262,7 +262,7 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { // Early bailout if background launch was detected, appStartTime will be nil if the app was // launched in background if (appStartTime == nil) { - FPRLogDebug(kFPRTraceNotCreated, + FPRLogDebug(kFPRTraceNotCreated, @"App start trace skipped due to background launch. " @"This prevents reporting incorrect multi-minute/hour durations."); return; @@ -281,7 +281,7 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { self.appStartTrace.backgroundTraceState != FPRTraceStateForegroundOnly) { [self.appStartTrace cancel]; self.appStartTrace = nil; - FPRLogDebug(kFPRTraceNotCreated, + FPRLogDebug(kFPRTraceNotCreated, @"App start trace cancelled due to background state contamination."); } @@ -311,24 +311,28 @@ - (void)appDidBecomeActiveNotification:(NSNotification *)notification { NSTimeInterval startTimeSinceEpoch = [strongSelf.appStartTrace startTimeSinceEpoch]; NSTimeInterval currentTimeSinceEpoch = [[NSDate date] timeIntervalSince1970]; - NSTimeInterval elapsed = currentTimeSinceEpoch - startTimeSinceEpoch; + NSTimeInterval measuredAppStartTime = currentTimeSinceEpoch - startTimeSinceEpoch; // The below check accounts for multiple scenarios: // 1. App started in background and comes to foreground later - // 2. App launched but immediately backgrounded + // 2. App launched but immediately backgroundedfor some reason and the actual launch + // happens a lot later. // 3. Network delays during startup inflating metrics // 4. iOS prewarm scenarios - BOOL shouldCompleteTrace = (elapsed < gAppStartMaxValidDuration) && - [strongSelf isAppStartEnabled] && - ![strongSelf isApplicationPreWarmed]; + // 5. Dropping the app start trace in such situations where the launch time is taking more + // than 60 minutes. This is an approximation, but a more agreeable timelimit for app start. + BOOL shouldDispatchAppStartTrace = (measuredAppStartTime < gAppStartMaxValidDuration) && + [strongSelf isAppStartEnabled] && + ![strongSelf isApplicationPreWarmed]; - if (shouldCompleteTrace) { + if (shouldDispatchAppStartTrace) { [strongSelf.appStartTrace stop]; } else { [strongSelf.appStartTrace cancel]; - if (elapsed >= gAppStartMaxValidDuration) { - FPRLogDebug(kFPRTraceInvalidName, - @"App start trace cancelled due to excessive duration: %.2fs", elapsed); + if (measuredAppStartTime >= gAppStartMaxValidDuration) { + FPRLogDebug(kFPRTraceInvalidName, + @"App start trace cancelled due to excessive duration: %.2fs", + measuredAppStartTime); } } }); From bd9b212a457051a84e835dd06f07c7632747be74 Mon Sep 17 00:00:00 2001 From: Jesus Rojas Date: Mon, 20 Oct 2025 11:43:35 -0600 Subject: [PATCH 7/7] Update changelog.MD --- FirebasePerformance/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/FirebasePerformance/CHANGELOG.md b/FirebasePerformance/CHANGELOG.md index 159d173e1f3..a63bfa357fe 100644 --- a/FirebasePerformance/CHANGELOG.md +++ b/FirebasePerformance/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased - [fixed] Prevent race condition crash in FPRTraceBackgroundActivityTracker. (#14273) +- [fixed] Fix app start trace outliers from network delays. (#10733) # 12.3.0 - [fixed] Add missing nanopb dependency to fix SwiftPM builds when building