Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-6708] Stop app hangs being reported if app is launched in the background #1112

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions Bugsnag/Helpers/BSGAppHangDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <Bugsnag/BugsnagConfiguration.h>
#import <Bugsnag/BugsnagErrorTypes.h>

#import "BSG_KSCrashState.h"
#import "BSG_KSMach.h"
#import "BugsnagCollections.h"
#import "BugsnagLogger.h"
Expand All @@ -26,36 +27,11 @@ @interface BSGAppHangDetector ()

@property (nonatomic) CFRunLoopObserverRef observer;

@property (nonatomic) BOOL isInBackground;

@end


@implementation BSGAppHangDetector

#if TARGET_OS_IOS

- (instancetype)init {
if ((self = [super init])) {
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(applicationDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification object:nil];

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification object:nil];
}
return self;
}

- (void)applicationDidEnterBackground {
self.isInBackground = YES;
}

- (void)applicationWillEnterForeground {
self.isInBackground = NO;
}

#endif

- (void)dealloc {
if (_observer) {
CFRunLoopRemoveObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes);
Expand Down Expand Up @@ -89,7 +65,6 @@ - (void)startWithDelegate:(id<BSGAppHangDetectorDelegate>)delegate {
dispatch_queue_t backgroundQueue;
__block dispatch_semaphore_t semaphore;
__weak typeof(delegate) weakDelegate = delegate;
__weak typeof(self) weakSelf = self;

backgroundQueue = dispatch_queue_create("com.bugsnag.app-hang-detector", DISPATCH_QUEUE_SERIAL);

Expand All @@ -107,7 +82,7 @@ - (void)startWithDelegate:(id<BSGAppHangDetectorDelegate>)delegate {
dispatch_time_t timeout = dispatch_time(now, (int64_t)(threshold * NSEC_PER_SEC));
dispatch_after(after, backgroundQueue, ^{
if (dispatch_semaphore_wait(semaphore, timeout) != 0) {
if (weakSelf.isInBackground) {
if (!bsg_kscrashstate_currentState()->applicationIsInForeground) {
bsg_log_debug(@"Ignoring app hang because app is in the background");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return;
Expand Down
4 changes: 0 additions & 4 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ - (BSG_KSCrashType)install:(BSG_KSCrashType)crashTypes directory:(NSString *)dir
free(crashReportPath);
free(recrashReportPath);

if (!installedCrashTypes) {
return 0;
}

NSNotificationCenter *nCenter = [NSNotificationCenter defaultCenter];
#if BSG_HAS_UIKIT
[nCenter addObserver:self
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog

### Bug fixes

* Stop app hangs being reported if app is launched in the background.
[#1112](https://github.com/bugsnag/bugsnag-cocoa/pull/1112)

* Stop session being reported if app is launched in the background.
[#1107](https://github.com/bugsnag/bugsnag-cocoa/pull/1107)

Expand Down