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

Fix: ActivityLifecycleIntegration#appStartSpan memory leak #1732

Merged
merged 3 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Feat: Allow setting proguard via Options and/or external resources (#1728)
* Feat: Add breadcrumbs for the Apollo integration (#1726)
* Fix: Don't set lastEventId for transactions (#1727)
* Fix: ActivityLifecycleIntegration#appStartSpan memory leak (#1732)

## 5.2.0-beta.3

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Config {
val okhttp = "com.squareup.okhttp3:okhttp"
// only bump gson if https://github.com/google/gson/issues/1597 is fixed
val gson = "com.google.code.gson:gson:2.8.5"
val leakCanary = "com.squareup.leakcanary:leakcanary-android:2.6"
val leakCanary = "com.squareup.leakcanary:leakcanary-android:2.7"
marandaneto marked this conversation as resolved.
Show resolved Hide resolved

private val lifecycleVersion = "2.2.0"
val lifecycleProcess = "androidx.lifecycle:lifecycle-process:$lifecycleVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,19 @@ public synchronized void onActivitySaveInstanceState(
public synchronized void onActivityDestroyed(final @NonNull Activity activity) {
addBreadcrumb(activity, "destroyed");

// in case the appStartSpan isn't completed yet, we finish it as cancelled to avoid
// memory leak
if (appStartSpan != null && !appStartSpan.isFinished()) {
appStartSpan.finish(SpanStatus.CANCELLED);
}

// in case people opt-out enableActivityLifecycleTracingAutoFinish and forgot to finish it,
// we make sure to finish it when the activity gets destroyed.
stopTracing(activity, true);

// set it to null in case its been just finished as cancelled
appStartSpan = null;

// clear it up, so we don't start again for the same activity if the activity is in the activity
// stack still.
// if the activity is opened again and not in memory, transactions will be created normally.
Expand All @@ -309,6 +318,12 @@ WeakHashMap<Activity, ITransaction> getActivitiesWithOngoingTransactions() {
return activitiesWithOngoingTransactions;
}

@TestOnly
@Nullable
ISpan getAppStartSpan() {
return appStartSpan;
}

private void setColdStart(final @Nullable Bundle savedInstanceState) {
if (!firstActivityCreated && performanceEnabled) {
// if Activity has savedInstanceState then its a warm start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertSame
import kotlin.test.assertTrue

Expand Down Expand Up @@ -429,6 +430,38 @@ class ActivityLifecycleIntegrationTest {
assertTrue(sut.activitiesWithOngoingTransactions.isEmpty())
}

@Test
fun `When Activity is destroyed, sets appStartSpan status to cancelled and finish it`() {
val sut = fixture.getSut()
fixture.options.tracesSampleRate = 1.0
sut.register(fixture.hub, fixture.options)

setAppStartTime()

val activity = mock<Activity>()
sut.onActivityCreated(activity, fixture.bundle)
sut.onActivityDestroyed(activity)

val span = fixture.transaction.children.first()
assertEquals(span.status, SpanStatus.CANCELLED)
assertTrue(span.isFinished)
}

@Test
fun `When Activity is destroyed, sets appStartSpan to null`() {
val sut = fixture.getSut()
fixture.options.tracesSampleRate = 1.0
sut.register(fixture.hub, fixture.options)

setAppStartTime()

val activity = mock<Activity>()
sut.onActivityCreated(activity, fixture.bundle)
sut.onActivityDestroyed(activity)

assertNull(sut.appStartSpan)
}

@Test
fun `When new Activity and transaction is created, finish previous ones`() {
val sut = fixture.getSut()
Expand Down