Skip to content

Commit

Permalink
Fix: App hang being reported from background (#3298)
Browse files Browse the repository at this point in the history
Some Apps may run in the background after receiving a notification or as part of background fetch.
We should not report app hang in this cases.
  • Loading branch information
brustolin authored Oct 30, 2023
1 parent 2124551 commit 5c115c5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

### Fixes

- Reporting app hangs from background (#3298)
- Missing `mechanism.handled` is not considered crash (#3353)

## 8.14.1
Expand All @@ -40,6 +41,7 @@ The MetricKit integration subscribes to [MXHangDiagnostic](https://developer.app
[MXDiskWriteExceptionDiagnostic](https://developer.apple.com/documentation/metrickit/mxdiskwriteexceptiondiagnostic),
and [MXCPUExceptionDiagnostic](https://developer.apple.com/documentation/metrickit/mxcpuexceptiondiagnostic).


## 8.13.1

### Fixes
Expand Down
15 changes: 14 additions & 1 deletion Sources/Sentry/SentryANRTrackingIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import "SentryClient+Private.h"
#import "SentryCrashMachineContext.h"
#import "SentryCrashWrapper.h"
#import "SentryDependencyContainer.h"
#import "SentryDispatchQueueWrapper.h"
#import "SentryEvent.h"
#import "SentryException.h"
Expand All @@ -14,9 +15,13 @@
#import "SentryThread.h"
#import "SentryThreadInspector.h"
#import "SentryThreadWrapper.h"
#import <SentryDependencyContainer.h>
#import "SentryUIApplication.h"
#import <SentryOptions+Private.h>

#if SENTRY_HAS_UIKIT
# import <UIKit/UIKit.h>
#endif

NS_ASSUME_NONNULL_BEGIN

@interface
Expand Down Expand Up @@ -61,6 +66,14 @@ - (void)dealloc

- (void)anrDetected
{
#if SENTRY_HAS_UIKIT
// If the app is not active, the main thread may be blocked or too busy.
// Since there is no UI for the user to interact, there is no need to report app hang.
if (SentryDependencyContainer.sharedInstance.application.applicationState
!= UIApplicationStateActive) {
return;
}
#endif
SentryThreadInspector *threadInspector = SentrySDK.currentHub.getClient.threadInspector;

NSArray<SentryThread *> *threads = [threadInspector getCurrentThreadsWithStackTrace];
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryUIApplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ - (UIApplication *)sharedApplication
return result;
}

- (UIApplicationState)applicationState
{
return self.sharedApplication.applicationState;
}

@end

#endif // SENTRY_HAS_UIKIT
7 changes: 7 additions & 0 deletions Sources/Sentry/include/SentryUIApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
@class UIWindow;
@protocol UIApplicationDelegate;

typedef NS_ENUM(NSInteger, UIApplicationState);

NS_ASSUME_NONNULL_BEGIN

/**
* A helper tool to retrieve informations from the application instance.
*/
@interface SentryUIApplication : NSObject

/**
* Returns the application state available at @c NSApplication.sharedApplication.applicationState
*/
@property (nonatomic, readonly) UIApplicationState applicationState;

/**
* Application shared UIApplication instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,23 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {

assertNoEventCaptured()
}

#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
func testANRDetected_ButBackground_EventNotCaptured() {

class BackgroundSentryUIApplication: SentryUIApplication {
override var applicationState: UIApplication.State { .background }
}

givenInitializedTracker()
setUpThreadInspector()
SentryDependencyContainer.sharedInstance().application = BackgroundSentryUIApplication()

Dynamic(sut).anrDetected()

assertNoEventCaptured()
}
#endif

func testDealloc_CallsUninstall() {
givenInitializedTracker()

Expand Down Expand Up @@ -137,7 +153,7 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {

private func setUpThreadInspector(addThreads: Bool = true) {
let threadInspector = TestThreadInspector.instance

if addThreads {

let frame1 = Sentry.Frame()
Expand Down

0 comments on commit 5c115c5

Please sign in to comment.