diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e2655c8f9..61ae01217b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * Enchancement: Simplify Sentry configuration in Spring integration (#1259) * Fix: Fix SentryTransaction#getStatus NPE (#1273) * Enchancement: Optimize SentryTracingFilter when hub is disabled. +* Fix: SentryTransaction#finish should not clear another transaction from the scope (#1278) Breaking Changes: * Enchancement: SentryExceptionResolver should not send handled errors by default (#1248). diff --git a/sentry/src/main/java/io/sentry/Hub.java b/sentry/src/main/java/io/sentry/Hub.java index 5a84b84d4a..eb69c70669 100644 --- a/sentry/src/main/java/io/sentry/Hub.java +++ b/sentry/src/main/java/io/sentry/Hub.java @@ -556,7 +556,10 @@ public void flush(long timeoutMillis) { e); } finally { if (item != null) { - item.getScope().clearTransaction(); + final Scope scope = item.getScope(); + if (scope.getTransaction() == transaction) { + scope.clearTransaction(); + } } } } diff --git a/sentry/src/test/java/io/sentry/HubTest.kt b/sentry/src/test/java/io/sentry/HubTest.kt index b22adff2b8..f0aae53ff6 100644 --- a/sentry/src/test/java/io/sentry/HubTest.kt +++ b/sentry/src/test/java/io/sentry/HubTest.kt @@ -1028,18 +1028,38 @@ class HubTest { } @Test - fun `when captureTransaction, scope transaction is cleared`() { + fun `when transaction is set on scope, captureTransaction clears it from the scope`() { val options = SentryOptions() options.cacheDirPath = file.absolutePath options.dsn = "https://key@sentry.io/proj" options.setSerializer(mock()) val sut = Hub(options) - sut.captureTransaction(SentryTransaction("name", "op"), null) + val transaction = SentryTransaction(TransactionContext("name", "op", true), sut) + sut.configureScope { it.setTransaction(transaction) } + sut.captureTransaction(transaction, null) sut.configureScope { assertNull(it.transaction) } } + + @Test + fun `when different transaction is set on scope, captureTransaction does not clear it from the scope`() { + val options = SentryOptions() + options.cacheDirPath = file.absolutePath + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val sut = Hub(options) + + val transaction = SentryTransaction(TransactionContext("name", "op", true), sut) + val anotherTransaction = SentryTransaction(TransactionContext("name", "op", true), sut) + sut.configureScope { it.setTransaction(anotherTransaction) } + sut.captureTransaction(transaction, null) + sut.configureScope { + assertNotNull(it.transaction) + assertEquals(anotherTransaction, it.transaction) + } + } //endregion //region startTransaction tests