Skip to content

Commit 6d8b5ae

Browse files
0xadam-brownclaude
andcommitted
fix(android): Release the nav transaction when unbinding throws
unbindTransaction cleared the scope before telling the coordinator to stop retaining the transaction. Scope.clearTransaction notifies the host app's scope observers in an unguarded loop, so one of them can throw and skip the coordinator call, leaving a retired transaction retained. Regaining the lease would then rebind it, attaching the new destination's spans to a transaction for a destination the user had already left. Tell the coordinator first. releaseTransaction runs no host app code and cannot throw, and nothing in the unbind depends on it having run. Add regression coverage for a scope observer throwing during the unbind. Refs JAVA-274 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent f673e42 commit 6d8b5ae

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

  • sentry-android-navigation3/src

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/lifecycle/NavScope.kt‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,16 @@ internal class NavScope(
6969
}
7070
}
7171

72-
/**
73-
* Unbinds [transaction] if it's bound, and, with or without the lease, tells the [coordinator] to
74-
* stop retaining it, so a retired transaction isn't restored when [owner] regains the lease.
75-
*/
7672
override fun unbindTransaction(transaction: ITransaction) {
73+
// Coordinator is called before clearTransaction() because the latter notifies the host app's
74+
// scope observer(s), which could throw. Clearing the transaction first would cause the
75+
// coordinator to retain a stale transaction.
76+
coordinator.releaseTransaction(owner, transaction)
7777
delegate.withTransaction { current ->
7878
if (current === transaction) {
7979
delegate.clearTransaction()
8080
}
8181
}
82-
coordinator.releaseTransaction(owner, transaction)
8382
}
8483

8584
override fun clearFinishedTransaction() {

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/lifecycle/NavScopeTest.kt‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import androidx.lifecycle.Lifecycle
44
import com.google.common.truth.Truth.assertThat
55
import io.sentry.ITransaction
66
import io.sentry.Scope
7+
import io.sentry.ScopeObserverAdapter
78
import io.sentry.SentryOptions
89
import io.sentry.protocol.App
910
import io.sentry.protocol.Browser
1011
import io.sentry.protocol.Device
1112
import java.util.Date
13+
import org.junit.Assert.assertThrows
1214
import org.junit.Test
1315
import org.mockito.kotlin.mock
1416
import org.mockito.kotlin.whenever
@@ -175,6 +177,28 @@ class NavScopeTest {
175177
assertThat(fixture.coordinator.isNavTransaction(transaction)).isFalse()
176178
}
177179

180+
@Test
181+
fun `unbindTransaction() - releases the retained nav transaction when a scope observer throws`() {
182+
val fixture = Fixture.create(ownsLease = true)
183+
val transaction = mock<ITransaction>()
184+
fixture.retainTransaction(transaction)
185+
fixture.realScope.transaction = transaction
186+
// Registered last, so that only clearTransaction() reaches it.
187+
fixture.realScope.options.addScopeObserver(
188+
object : ScopeObserverAdapter() {
189+
override fun setTransaction(transaction: String?) {
190+
throw ThrowingScopeObserverException()
191+
}
192+
}
193+
)
194+
195+
assertThrows(ThrowingScopeObserverException::class.java) {
196+
fixture.navScope.unbindTransaction(transaction)
197+
}
198+
199+
assertThat(fixture.coordinator.isNavTransaction(transaction)).isFalse()
200+
}
201+
178202
@Test
179203
fun `unbindTransaction() - preserves a different bound transaction`() {
180204
val fixture = Fixture.create(ownsLease = true)
@@ -332,6 +356,9 @@ class NavScopeTest {
332356
unknown = mapOf("unknown" to "unknown")
333357
}
334358

359+
/** Thrown by a scope observer, to stand in for one the host app registered. */
360+
private class ThrowingScopeObserverException : RuntimeException()
361+
335362
private class Fixture
336363
private constructor(
337364
val realScope: Scope,

0 commit comments

Comments
 (0)