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-6816] Detect hangs during launch of scene based apps #1263

Merged
merged 2 commits into from
Jan 4, 2022
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
12 changes: 8 additions & 4 deletions Bugsnag/Helpers/BSGAppHangDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@interface BSGAppHangDetector ()

@property (weak, nonatomic) id<BSGAppHangDetectorDelegate> delegate;
@property (nonatomic) BOOL runLoopIsRunning;
@property (nonatomic) BOOL recordAllThreads;
@property (nonatomic) CFRunLoopObserverRef observer;
@property (atomic) dispatch_time_t processingDeadline;
Expand Down Expand Up @@ -81,6 +82,7 @@ - (void)startWithDelegate:(id<BSGAppHangDetectorDelegate>)delegate {
// Each iteration indicates a separate unit of work so the hang detection should be reset accordingly.
dispatch_semaphore_signal(self.processingFinished);
}
self.runLoopIsRunning = YES;
self.processingDeadline = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(threshold * NSEC_PER_SEC));
dispatch_semaphore_signal(self.processingStarted);
isProcessing = YES;
Expand All @@ -104,9 +106,9 @@ - (void)startWithDelegate:(id<BSGAppHangDetectorDelegate>)delegate {
self.observer = CFRunLoopObserverCreateWithHandler(NULL, activities, true, order, observerBlock);

// Start monitoring immediately so that app hangs during launch can be detected.
// Note that because scene-based apps start in UIApplicationStateBackground, hangs in
// -[AppDelegate application:didFinishLaunchingWithOptions:] will be ignored.
observerBlock(self.observer, kCFRunLoopAfterWaiting);
self.processingDeadline = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(threshold * NSEC_PER_SEC));
dispatch_semaphore_signal(self.processingStarted);
isProcessing = YES;

CFRunLoopAddObserver(CFRunLoopGetMain(), self.observer, kCFRunLoopCommonModes);

Expand Down Expand Up @@ -146,7 +148,9 @@ - (void)detectAppHangs {
}
#endif

if (shouldReportAppHang && !bsg_kscrashstate_currentState()->applicationIsInForeground) {
// Ignore background state if the runloop has not yet ticked so that hangs in `didFinishLaunching` in UIScene-based
// apps are detected. UIScene-based apps always start in UIApplicationStateBackground, unlike those without scenes.
if (shouldReportAppHang && !bsg_kscrashstate_currentState()->applicationIsInForeground && self.runLoopIsRunning) {
bsg_log_debug(@"Ignoring app hang because app is in the background");
shouldReportAppHang = NO;
}
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changelog

### Bug fixes

* Detect hangs during launch of UIScene based apps.
[#1263](https://github.com/bugsnag/bugsnag-cocoa/pull/1263)
* Fix some potential deadlocks that could occur if a crash handler crashes.
[#1252](https://github.com/bugsnag/bugsnag-cocoa/pull/1252)

Expand Down
10 changes: 8 additions & 2 deletions features/fixtures/shared/scenarios/AppHangScenarios.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class AppHangFatalOnlyScenario: Scenario {

override func run() {
NSLog("Hanging indefinitely...")
while true {}
// Use asyncAfter to allow the Appium click event to be handled
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
while true {}
}
}
}

Expand All @@ -84,7 +87,10 @@ class AppHangFatalDisabledScenario: Scenario {

override func run() {
NSLog("Hanging indefinitely...")
while true {}
// Use asyncAfter to allow the Appium click event to be handled
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
while true {}
}
}
}

Expand Down