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-7263] Disable app hang detection in app extensions #1198

Merged
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
5 changes: 2 additions & 3 deletions Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ - (void)start {
[self.eventUploader uploadStoredEvents];

// App hang detector deliberately started after sendLaunchCrashSynchronously (which by design may itself trigger an app hang)
if (self.configuration.enabledErrorTypes.appHangs) {
[self startAppHangDetector];
}
// Note: BSGAppHangDetector itself checks configuration.enabledErrorTypes.appHangs
[self startAppHangDetector];

self.configMetadataFromLastLaunch = nil;
self.metadataFromLastLaunch = nil;
Expand Down
8 changes: 8 additions & 0 deletions Bugsnag/Helpers/BSGAppHangDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ - (void)startWithDelegate:(id<BSGAppHangDetectorDelegate>)delegate {
return;
}

if ([BSG_KSSystemInfo isRunningInAppExtension]) {
// App extensions have a different life cycle and environment that make the hang detection mechanism unsuitable.
// * Depending on the type of extension, the run loop is not necessarily dedicated to UI.
// * The host app or other extensions run by it may trigger false positives.
// * The system may kill app extensions without any notification.
return;
}

if (NSProcessInfo.processInfo.environment[@"XCTestConfigurationFilePath"]) {
// Disable functionality during unit testing to avoid crashes that can occur due to there
// being many leaked BugsnagClient instances and BSGAppHangDetectors running while global
Expand Down
25 changes: 8 additions & 17 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -441,23 +441,14 @@ + (NSString *)osBuildVersion {
}

+ (BOOL)isRunningInAppExtension {
#if BSG_PLATFORM_IOS
NSBundle *mainBundle = [NSBundle mainBundle];
// From the App Extension Programming Guide:
// > When you build an extension based on an Xcode template, you get an
// > extension bundle that ends in .appex.
return [[mainBundle executablePath] containsString:@".appex"]
// In the case that the extension bundle was renamed or generated
// outside of the Xcode template, check the Bundle OS Type Code:
// > This key consists of a four-letter code for the bundle type. For
// > apps, the code is APPL, for frameworks, it's FMWK, and for bundles,
// > it's BNDL.
// If the main bundle type is not "APPL", assume this is an extension
// context.
|| ![[mainBundle infoDictionary][@"CFBundlePackageType"] isEqualToString:@"APPL"];
#else
return NO;
#endif
// From "Information Property List Key Reference" > "App Extension Keys"
// https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/AppExtensionKeys.html
//
// NSExtensionPointIdentifier
// String - iOS, macOS. Specifies the extension point that supports an app extension, in reverse-DNS notation.
// This key is required for every app extension, and must be placed as an immediate child of the NSExtension key.
// Each Xcode app extension template is preconfigured with the appropriate extension point identifier key.
return NSBundle.mainBundle.infoDictionary[@"NSExtension"][@"NSExtensionPointIdentifier"] != nil;
}

#if BSG_PLATFORM_IOS || BSG_PLATFORM_TVOS
Expand Down
2 changes: 2 additions & 0 deletions Bugsnag/include/Bugsnag/BugsnagErrorTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Determines whether App Hang events should be reported to bugsnag.
*
* This flag is true by default.
*
* Note: this flag is ignored in App Extensions, where app hang detection is always disabled.
*/
@property (nonatomic) BOOL appHangs;

Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

## TBD

### Bug fixes

* Disable app hang detection for app extensions.
[#1198](https://github.com/bugsnag/bugsnag-cocoa/pull/1198)

## 6.13.0 (2021-09-29)

### Enhancements
Expand Down