Skip to content

Commit

Permalink
Weakly reference Activity for transaction finished callback (#2203)
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed Aug 3, 2022
1 parent 4eb446a commit 3c24dd4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- Prevent NPE by checking SentryTracer.timer for null again inside synchronized ([#2200](https://github.com/getsentry/sentry-java/pull/2200))
- Weakly reference Activity for transaction finished callback ([#2203](https://github.com/getsentry/sentry-java/pull/2203))
- `attach-screenshot` set on Manual init. didn't work ([#2186](https://github.com/getsentry/sentry-java/pull/2186))
- Remove extra space from `spring.factories` causing issues in old versions of Spring Boot ([#2181](https://github.com/getsentry/sentry-java/pull/2181))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -146,6 +147,7 @@ private void stopPreviousTransactions() {
}

private void startTracing(final @NotNull Activity activity) {
WeakReference<Activity> weakActivity = new WeakReference<>(activity);
if (performanceEnabled && !isRunningTransaction(activity) && hub != null) {
// as we allow a single transaction running on the bound Scope, we finish the previous ones
stopPreviousTransactions();
Expand All @@ -167,7 +169,20 @@ private void startTracing(final @NotNull Activity activity) {
(Date) null,
true,
(finishingTransaction) -> {
activityFramesTracker.setMetrics(activity, finishingTransaction.getEventId());
@Nullable Activity unwrappedActivity = weakActivity.get();
if (unwrappedActivity != null) {
activityFramesTracker.setMetrics(
unwrappedActivity, finishingTransaction.getEventId());
} else {
if (options != null) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Unable to track activity frames as the Activity %s has been destroyed.",
activityName);
}
}
});
} else {
// start transaction with app start timestamp
Expand All @@ -178,7 +193,20 @@ private void startTracing(final @NotNull Activity activity) {
appStartTime,
true,
(finishingTransaction) -> {
activityFramesTracker.setMetrics(activity, finishingTransaction.getEventId());
@Nullable Activity unwrappedActivity = weakActivity.get();
if (unwrappedActivity != null) {
activityFramesTracker.setMetrics(
unwrappedActivity, finishingTransaction.getEventId());
} else {
if (options != null) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Unable to track activity frames as the Activity %s has been destroyed.",
activityName);
}
}
});
// start specific span for app start

Expand Down

0 comments on commit 3c24dd4

Please sign in to comment.