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

Weakly reference Activity for transaction finished callback #2203

Merged
merged 5 commits into from Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### Fixes

- 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
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to reproduce the issue with and without the fix using LeakCanary.
Upgrade leakcanary to the latest version -> https://github.com/square/leakcanary/releases/tag/v2.9.1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried with 2.9.1 but it didn't find anything regardless of WeakReference or old implementation.

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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What log level should this be?

"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