Skip to content

Commit

Permalink
feat: stop profiler when app moves to background (#2331)
Browse files Browse the repository at this point in the history
* feat: stop profiler when app moves to background
  • Loading branch information
armcknight authored Oct 27, 2022
1 parent 7d3d1a3 commit d47c2c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Profile concurrent transactions (#2227)

### Fixes

- Stop profiler when app moves to background (#2331)

## 7.29.0

### Features
Expand Down
19 changes: 19 additions & 0 deletions Sources/Sentry/SentryProfiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ + (void)startForSpanID:(SentrySpanId *)spanID
selector:@selector(timeoutAbort)
userInfo:nil
repeats:NO];
# if SENTRY_HAS_UIKIT
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(backgroundAbort)
name:UIApplicationWillResignActiveNotification
object:nil];
# endif // SENTRY_HAS_UIKIT
_gCurrentProfiler->_hub = hub;
}

Expand Down Expand Up @@ -259,6 +265,19 @@ + (void)timeoutAbort
[self stopProfilerForReason:SentryProfilerTruncationReasonTimeout];
}

+ (void)backgroundAbort
{
std::lock_guard<std::mutex> l(_gProfilerLock);

if (_gCurrentProfiler == nil) {
SENTRY_LOG_DEBUG(@"No current profiler to stop.");
return;
}

SENTRY_LOG_DEBUG(@"Stopping profiler %@ due to timeout.", _gCurrentProfiler);
[self stopProfilerForReason:SentryProfilerTruncationReasonAppMovedToBackground];
}

+ (void)stopProfilerForReason:(SentryProfilerTruncationReason)reason
{
[_gCurrentProfiler->_timeoutTimer invalidate];
Expand Down

0 comments on commit d47c2c0

Please sign in to comment.