From 300409e6713ebd0c0e2b6dde91377d49119a50e7 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Wed, 19 Jul 2023 10:29:45 +0200 Subject: [PATCH 1/6] Do not send session updates for terminated sessions --- .../src/main/java/io/sentry/SentryClient.java | 10 +++-- sentry/src/main/java/io/sentry/Session.java | 4 ++ .../test/java/io/sentry/SentryClientTest.kt | 38 +++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/sentry/src/main/java/io/sentry/SentryClient.java b/sentry/src/main/java/io/sentry/SentryClient.java index 505a0842fe..bd981842e3 100644 --- a/sentry/src/main/java/io/sentry/SentryClient.java +++ b/sentry/src/main/java/io/sentry/SentryClient.java @@ -137,7 +137,10 @@ private boolean shouldApplyScopeData( @Nullable Session session = null; if (event != null) { - session = updateSessionData(event, hint, scope); + // https://develop.sentry.dev/sdk/sessions/#terminal-session-states + if (sessionBeforeUpdate == null || !sessionBeforeUpdate.isTerminated()) { + session = updateSessionData(event, hint, scope); + } if (!sample()) { options @@ -490,9 +493,8 @@ Session updateSessionData( } if (session.update(status, userAgent, crashedOrErrored, abnormalMechanism)) { - // if we have an uncaughtExceptionHint we can end the session. - if (HintUtils.hasType( - hint, UncaughtExceptionHandlerIntegration.UncaughtExceptionHint.class)) { + // if session terminated we can end it. + if (session.isTerminated()) { session.end(); } } diff --git a/sentry/src/main/java/io/sentry/Session.java b/sentry/src/main/java/io/sentry/Session.java index f118c93849..500da919fe 100644 --- a/sentry/src/main/java/io/sentry/Session.java +++ b/sentry/src/main/java/io/sentry/Session.java @@ -125,6 +125,10 @@ public Session( null); } + public boolean isTerminated() { + return status != State.Ok; + } + @SuppressWarnings({"JdkObsolete", "JavaUtilDate"}) public @Nullable Date getStarted() { if (started == null) { diff --git a/sentry/src/test/java/io/sentry/SentryClientTest.kt b/sentry/src/test/java/io/sentry/SentryClientTest.kt index 2540067606..0d822d0e67 100644 --- a/sentry/src/test/java/io/sentry/SentryClientTest.kt +++ b/sentry/src/test/java/io/sentry/SentryClientTest.kt @@ -1,6 +1,7 @@ package io.sentry import io.sentry.Scope.IWithPropagationContext +import io.sentry.Session.State.Crashed import io.sentry.clientreport.ClientReportTestHelper.Companion.assertClientReport import io.sentry.clientreport.DiscardReason import io.sentry.clientreport.DiscardedEvent @@ -953,6 +954,21 @@ class SentryClientTest { } } + @Test + fun `When event is non handled, end the session`() { + val scope = Scope(fixture.sentryOptions) + scope.startSession() + + val event = SentryEvent().apply { + exceptions = createNonHandledException() + } + fixture.getSut().updateSessionData(event, Hint(), scope) + scope.withSession { + assertEquals(Session.State.Crashed, it!!.status) + assertNull(it.init) + } + } + @Test fun `When event is handled, keep level as it is`() { val scope = Scope(fixture.sentryOptions) @@ -1137,6 +1153,28 @@ class SentryClientTest { } } + @Test + fun `when session is in terminal state, does not send session update`() { + val sut = fixture.getSut() + + val event = SentryEvent().apply { + exceptions = createNonHandledException() + } + val scope = Scope(fixture.sentryOptions) + val sessionPair = scope.startSession() + scope.withSession { it!!.update(Crashed, null, false) } + + assertNotNull(sessionPair) { + sut.captureEvent(event, scope, null) + verify(fixture.transport).send( + check { + assertNull(it.items.find { item -> item.header.type == SentryItemType.Session }) + }, + anyOrNull() + ) + } + } + @Test fun `when contexts property is set on the event, property from scope contexts is not applied`() { val sut = fixture.getSut() From 4aad4986b638c9c7ee0a67038b48ce70ee7acf6c Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Wed, 19 Jul 2023 10:30:18 +0200 Subject: [PATCH 2/6] api dump --- sentry/api/sentry.api | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index d4d0453b57..147552ab87 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -2057,6 +2057,7 @@ public final class io/sentry/Session : io/sentry/JsonSerializable, io/sentry/Jso public fun getTimestamp ()Ljava/util/Date; public fun getUnknown ()Ljava/util/Map; public fun getUserAgent ()Ljava/lang/String; + public fun isTerminated ()Z public fun serialize (Lio/sentry/ObjectWriter;Lio/sentry/ILogger;)V public fun setInitAsTrue ()V public fun setUnknown (Ljava/util/Map;)V From 084603c6a6b4566d848b9800a94232e31869dd45 Mon Sep 17 00:00:00 2001 From: getsentry-bot Date: Tue, 18 Jul 2023 17:06:14 +0000 Subject: [PATCH 3/6] release: 6.26.0 --- CHANGELOG.md | 2 +- gradle.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ca736c066..16d017e228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## Unreleased +## 6.26.0 ### Features - (Internal) Extend APIs for hybrid SDKs ([#2814](https://github.com/getsentry/sentry-java/pull/2814), [#2846](https://github.com/getsentry/sentry-java/pull/2846)) diff --git a/gradle.properties b/gradle.properties index a84965ba59..d07d245f2f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,7 +10,7 @@ android.useAndroidX=true android.defaults.buildfeatures.buildconfig=true # Release information -versionName=6.25.2 +versionName=6.26.0 # Override the SDK name on native crashes on Android sentryAndroidSdkName=sentry.native.android From dcd296e539297954f9c071b678f9dcfc0d8bae48 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Wed, 19 Jul 2023 10:39:49 +0200 Subject: [PATCH 4/6] Changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16d017e228..2c99c10648 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Do not send session updates for terminated sessions ([#2849](https://github.com/getsentry/sentry-java/pull/2849)) + ## 6.26.0 ### Features From 0c7629a35ed03a0702ed4e9ff2f5587788b11ba2 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Wed, 19 Jul 2023 10:40:43 +0200 Subject: [PATCH 5/6] Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec0dae5a5e..2c99c10648 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 6.26.0 +## Unreleased ### Fixes From b75e82777cbdbfa5bbedaad5722188debdbfb56a Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Wed, 19 Jul 2023 22:43:27 +0200 Subject: [PATCH 6/6] Change test to assert value that is gonna fail --- sentry/src/test/java/io/sentry/SentryClientTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry/src/test/java/io/sentry/SentryClientTest.kt b/sentry/src/test/java/io/sentry/SentryClientTest.kt index 0d822d0e67..41d3947a5e 100644 --- a/sentry/src/test/java/io/sentry/SentryClientTest.kt +++ b/sentry/src/test/java/io/sentry/SentryClientTest.kt @@ -965,7 +965,7 @@ class SentryClientTest { fixture.getSut().updateSessionData(event, Hint(), scope) scope.withSession { assertEquals(Session.State.Crashed, it!!.status) - assertNull(it.init) + assertNotNull(it.duration) } }