Skip to content

Commit

Permalink
Operate app hang thread operations inside an autorelease pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kstenerud committed Dec 7, 2022
1 parent 48bf75a commit 64f4925
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
78 changes: 40 additions & 38 deletions Bugsnag/Helpers/BSGAppHangDetector.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,46 +122,48 @@ - (void)detectAppHangs {
NSThread.currentThread.name = @"com.bugsnag.app-hang-detector";

while (!self.shouldStop) {
if (dispatch_semaphore_wait(self.processingStarted, DISPATCH_TIME_FOREVER) != 0) {
bsg_log_err(@"BSGAppHangDetector: dispatch_semaphore_wait failed unexpectedly");
return;
}

const dispatch_time_t deadline = self.processingDeadline;

if (dispatch_semaphore_wait(self.processingFinished, deadline) == 0) {
// Run loop finished within the deadline
continue;
}

BOOL shouldReportAppHang = YES;

if (dispatch_time(DISPATCH_TIME_NOW, 0) > dispatch_time(deadline, 1 * NSEC_PER_SEC)) {
// If this thread has woken up long after the deadline, the app may have been suspended.
bsg_log_debug(@"Ignoring potential false positive app hang");
shouldReportAppHang = NO;
}

@autoreleasepool {
if (dispatch_semaphore_wait(self.processingStarted, DISPATCH_TIME_FOREVER) != 0) {
bsg_log_err(@"BSGAppHangDetector: dispatch_semaphore_wait failed unexpectedly");
return;
}

const dispatch_time_t deadline = self.processingDeadline;

if (dispatch_semaphore_wait(self.processingFinished, deadline) == 0) {
// Run loop finished within the deadline
continue;
}

BOOL shouldReportAppHang = YES;

if (dispatch_time(DISPATCH_TIME_NOW, 0) > dispatch_time(deadline, 1 * NSEC_PER_SEC)) {
// If this thread has woken up long after the deadline, the app may have been suspended.
bsg_log_debug(@"Ignoring potential false positive app hang");
shouldReportAppHang = NO;
}

#if defined(DEBUG) && DEBUG
if (shouldReportAppHang && bsg_ksmachisBeingTraced()) {
bsg_log_debug(@"Ignoring app hang because debugger is attached");
shouldReportAppHang = NO;
}
if (shouldReportAppHang && bsg_ksmachisBeingTraced()) {
bsg_log_debug(@"Ignoring app hang because debugger is attached");
shouldReportAppHang = NO;
}
#endif

if (shouldReportAppHang && !bsg_runContext->isForeground && !self.delegate.configuration.reportBackgroundAppHangs) {
bsg_log_debug(@"Ignoring app hang because app is in the background");
shouldReportAppHang = NO;
}

if (shouldReportAppHang) {
[self appHangDetected];
}

dispatch_semaphore_wait(self.processingFinished, DISPATCH_TIME_FOREVER);

if (shouldReportAppHang) {
[self appHangEnded];

if (shouldReportAppHang && !bsg_runContext->isForeground && !self.delegate.configuration.reportBackgroundAppHangs) {
bsg_log_debug(@"Ignoring app hang because app is in the background");
shouldReportAppHang = NO;
}

if (shouldReportAppHang) {
[self appHangDetected];
}

dispatch_semaphore_wait(self.processingFinished, DISPATCH_TIME_FOREVER);

if (shouldReportAppHang) {
[self appHangEnded];
}
}
}
}
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

* Fix memory leak in the app hang detection code.
[#1507](https://github.com/bugsnag/bugsnag-cocoa/pull/1507)

* Truncate additional data to reduce number of oversized payloads.
[#1501](https://github.com/bugsnag/bugsnag-cocoa/pull/1501)

Expand Down

0 comments on commit 64f4925

Please sign in to comment.