Skip to content

Commit bc5ca85

Browse files
0xadam-browncodex
andcommitted
fix(android): Release nav lease references when cleanup throws
Always forget departing navigation candidates when transaction cleanup or lease handoff fails. Preserve a successor selected before a callback throws. Add regression coverage for cleanup failures and successor lease ownership. Refs JAVA-274 Co-Authored-By: Codex <noreply@openai.com>
1 parent 3473e24 commit bc5ca85

2 files changed

Lines changed: 176 additions & 13 deletions

File tree

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,37 @@ internal class NavLeaseCoordinator {
155155
* A `null` scope (e.g., the SDK is disabled) still removes the candidate, but its transaction is
156156
* left to time out, its published data is left in place, and the lease is released rather than
157157
* handed off.
158+
*
159+
* Coordinator references are released even if scope cleanup or lease handoff throws. The
160+
* exception is left to the caller to handle; remaining scope cleanup may be incomplete.
158161
*/
159162
fun unregisterCandidate(owner: Any, scope: IScope?) {
160163
findCandidate(owner)?.let {
161-
finishTransaction(it, scope)
162-
candidates.remove(it)
163-
164-
if (publishedDataCandidate === it) {
165-
publishedDataCandidate = null
166-
if (scope != null) {
167-
it.onClearPublishedData(scope)
164+
try {
165+
finishTransaction(it, scope)
166+
candidates.remove(it)
167+
168+
if (publishedDataCandidate === it) {
169+
publishedDataCandidate = null
170+
if (scope != null) {
171+
it.onClearPublishedData(scope)
172+
}
168173
}
169-
}
170174

171-
if (leaseHolder === it) {
172-
handOffLease(mostRecentlyResumedCandidate(), scope)
175+
if (leaseHolder === it) {
176+
handOffLease(mostRecentlyResumedCandidate(), scope)
177+
}
178+
} finally {
179+
// Scope observers and transaction callbacks can throw. Never retain a departing owner
180+
// just because cleanup failed, and preserve any successor selected during handoff.
181+
candidates.remove(it)
182+
it.transaction = null
183+
if (publishedDataCandidate === it) {
184+
publishedDataCandidate = null
185+
}
186+
if (leaseHolder === it) {
187+
leaseHolder = null
188+
}
173189
}
174190
}
175191
}

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

Lines changed: 150 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package io.sentry.compose.navigation3.lifecycle
22

33
import androidx.lifecycle.Lifecycle
44
import com.google.common.truth.Truth.assertThat
5+
import io.sentry.IScope
56
import io.sentry.ITransaction
67
import io.sentry.Scope
78
import io.sentry.SentryOptions
89
import io.sentry.SpanStatus
10+
import org.junit.Assert.assertThrows
911
import org.junit.Test
1012
import org.mockito.kotlin.any
13+
import org.mockito.kotlin.doThrow
1114
import org.mockito.kotlin.mock
1215
import org.mockito.kotlin.never
1316
import org.mockito.kotlin.verify
@@ -765,27 +768,171 @@ class NavLeaseCoordinatorTest {
765768
assertThat(fixture.cleared).isEmpty()
766769
}
767770

771+
@Test
772+
fun `unregistering the leaseholder forgets the owner when finishing its transaction throws`() {
773+
val fixture = Fixture.create()
774+
val navLeaseCoordinator = fixture.navLeaseCoordinator
775+
val failure = IllegalStateException("finish failed")
776+
fixture.acquireLease(owner, transaction = transaction)
777+
doThrow(failure).whenever(transaction).finish(SpanStatus.OK)
778+
779+
val thrown =
780+
assertThrows(IllegalStateException::class.java) {
781+
navLeaseCoordinator.unregisterCandidate(owner, fixture.scope)
782+
}
783+
784+
assertThat(thrown).isSameInstanceAs(failure)
785+
assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
786+
assertThat(navLeaseCoordinator.isNavTransaction(transaction)).isFalse()
787+
788+
fixture.published.clear()
789+
navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
790+
fixture.acquireLease(incoming)
791+
792+
assertThat(fixture.published).containsExactly(incoming)
793+
assertThat(fixture.cleared).isEmpty()
794+
}
795+
796+
@Test
797+
fun `unregistering the leaseholder forgets the owner when unbinding its transaction throws`() {
798+
val fixture = Fixture.create()
799+
val navLeaseCoordinator = fixture.navLeaseCoordinator
800+
val failure = IllegalStateException("unbind failed")
801+
val failingScope = mock<IScope>()
802+
fixture.acquireLease(owner, transaction = transaction)
803+
doThrow(failure).whenever(failingScope).withTransaction(any())
804+
805+
val thrown =
806+
assertThrows(IllegalStateException::class.java) {
807+
navLeaseCoordinator.unregisterCandidate(owner, failingScope)
808+
}
809+
810+
assertThat(thrown).isSameInstanceAs(failure)
811+
assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
812+
assertThat(navLeaseCoordinator.isNavTransaction(transaction)).isFalse()
813+
814+
fixture.published.clear()
815+
navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
816+
fixture.acquireLease(incoming)
817+
818+
assertThat(fixture.published).containsExactly(incoming)
819+
assertThat(fixture.cleared).isEmpty()
820+
}
821+
822+
@Test
823+
fun `unregistering the leaseholder forgets the owner when clearing its published nav data throws`() {
824+
val fixture = Fixture.create()
825+
val navLeaseCoordinator = fixture.navLeaseCoordinator
826+
val failure = IllegalStateException("clear failed")
827+
fixture.acquireLease(owner, onClearPublishedData = { throw failure })
828+
829+
val thrown =
830+
assertThrows(IllegalStateException::class.java) {
831+
navLeaseCoordinator.unregisterCandidate(owner, fixture.scope)
832+
}
833+
834+
assertThat(thrown).isSameInstanceAs(failure)
835+
assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
836+
837+
fixture.published.clear()
838+
navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
839+
fixture.acquireLease(incoming)
840+
841+
assertThat(fixture.published).containsExactly(incoming)
842+
assertThat(navLeaseCoordinator.hasLease(incoming)).isTrue()
843+
}
844+
845+
@Test
846+
fun `unregistering the leaseholder forgets the owner when restoring a fallback transaction throws`() {
847+
val fixture = Fixture.create()
848+
val navLeaseCoordinator = fixture.navLeaseCoordinator
849+
val failure = IllegalStateException("fallback restoration failed")
850+
val failingScope = mock<IScope>()
851+
fixture.acquireLease(previous, transaction = transaction)
852+
fixture.acquireLease(owner)
853+
doThrow(failure).whenever(failingScope).withTransaction(any())
854+
855+
val thrown =
856+
assertThrows(IllegalStateException::class.java) {
857+
navLeaseCoordinator.unregisterCandidate(owner, failingScope)
858+
}
859+
860+
assertThat(thrown).isSameInstanceAs(failure)
861+
assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
862+
assertThat(navLeaseCoordinator.isNavTransaction(transaction)).isTrue()
863+
864+
fixture.published.clear()
865+
fixture.cleared.clear()
866+
navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
867+
fixture.acquireLease(incoming)
868+
869+
assertThat(fixture.published).containsExactly(incoming)
870+
assertThat(fixture.cleared).doesNotContain(owner)
871+
}
872+
873+
@Test
874+
fun `unregistering the leaseholder preserves the fallback lease when publishing fallback nav data throws`() {
875+
val fixture = Fixture.create()
876+
val navLeaseCoordinator = fixture.navLeaseCoordinator
877+
val failure = IllegalStateException("fallback publication failed")
878+
var failPublication = false
879+
navLeaseCoordinator.registerCandidate(
880+
previous,
881+
Lifecycle.State.RESUMED,
882+
onPublishData = {
883+
if (failPublication) {
884+
throw failure
885+
}
886+
},
887+
onClearPublishedData = {},
888+
)
889+
navLeaseCoordinator.onBackStackUpdate(previous, fixture.scope)
890+
fixture.acquireLease(owner, transaction = transaction)
891+
failPublication = true
892+
893+
val thrown =
894+
assertThrows(IllegalStateException::class.java) {
895+
navLeaseCoordinator.unregisterCandidate(owner, fixture.scope)
896+
}
897+
898+
assertThat(thrown).isSameInstanceAs(failure)
899+
assertThat(navLeaseCoordinator.hasLease(owner)).isFalse()
900+
assertThat(navLeaseCoordinator.hasLease(previous)).isTrue()
901+
assertThat(navLeaseCoordinator.isNavTransaction(transaction)).isFalse()
902+
903+
fixture.published.clear()
904+
navLeaseCoordinator.resumeCandidate(owner, fixture.scope)
905+
906+
assertThat(fixture.published).isEmpty()
907+
assertThat(navLeaseCoordinator.hasLease(previous)).isTrue()
908+
}
909+
768910
private class Fixture {
769911
val navLeaseCoordinator = NavLeaseCoordinator()
770912
val scope = Scope(SentryOptions())
771913
val published = mutableListOf<Any>()
772914
val cleared = mutableListOf<Any>()
773915

774-
fun register(owner: Any, state: Lifecycle.State) {
916+
fun register(
917+
owner: Any,
918+
state: Lifecycle.State,
919+
onClearPublishedData: (IScope) -> Unit = { cleared += owner },
920+
) {
775921
navLeaseCoordinator.registerCandidate(
776922
owner,
777923
state,
778924
onPublishData = { published += owner },
779-
onClearPublishedData = { cleared += owner },
925+
onClearPublishedData = onClearPublishedData,
780926
)
781927
}
782928

783929
fun acquireLease(
784930
owner: Any,
785931
state: Lifecycle.State = Lifecycle.State.RESUMED,
786932
transaction: ITransaction? = null,
933+
onClearPublishedData: (IScope) -> Unit = { cleared += owner },
787934
) {
788-
register(owner, state)
935+
register(owner, state, onClearPublishedData)
789936
navLeaseCoordinator.onBackStackUpdate(owner, scope)
790937
if (transaction != null) {
791938
navLeaseCoordinator.retainTransaction(owner, transaction)

0 commit comments

Comments
 (0)