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 011fa43
Showing 1 changed file with 40 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

0 comments on commit 011fa43

Please sign in to comment.