2020import io .sentry .Session ;
2121import io .sentry .UncaughtExceptionHandlerIntegration ;
2222import io .sentry .hints .AbnormalExit ;
23+ import io .sentry .hints .NativeCrashExit ;
2324import io .sentry .hints .SessionEnd ;
2425import io .sentry .hints .SessionStart ;
2526import io .sentry .transport .NoOpEnvelopeCache ;
@@ -119,10 +120,10 @@ private boolean storeInternal(final @NotNull SentryEnvelope envelope, final @Not
119120 }
120121 }
121122
122- if (HintUtils .hasType (hint , AbnormalExit .class )) {
123+ if (HintUtils .hasType (hint , AbnormalExit .class )
124+ || HintUtils .hasType (hint , NativeCrashExit .class )) {
123125 tryEndPreviousSession (hint );
124126 }
125- // TODO: adapt tryEndPreviousSession(hint); to CrashExit.class
126127
127128 if (HintUtils .hasType (hint , SessionStart .class )) {
128129 movePreviousSession (currentSessionFile , previousSessionFile );
@@ -202,20 +203,20 @@ private boolean storeInternal(final @NotNull SentryEnvelope envelope, final @Not
202203 @ SuppressWarnings ("JavaUtilDate" )
203204 private void tryEndPreviousSession (final @ NotNull Hint hint ) {
204205 final Object sdkHint = HintUtils .getSentrySdkHint (hint );
205- if (sdkHint instanceof AbnormalExit ) {
206- final File previousSessionFile = getPreviousSessionFile (directory .getAbsolutePath ());
206+ final File previousSessionFile = getPreviousSessionFile (directory .getAbsolutePath ());
207207
208- if (previousSessionFile .exists ()) {
209- options .getLogger ().log (WARNING , "Previous session is not ended, we'd need to end it." );
208+ if (previousSessionFile .exists ()) {
209+ options .getLogger ().log (WARNING , "Previous session is not ended, we'd need to end it." );
210210
211- try (final Reader reader =
212- new BufferedReader (
213- new InputStreamReader (new FileInputStream (previousSessionFile ), UTF_8 ))) {
214- final Session session = serializer .getValue ().deserialize (reader , Session .class );
215- if (session != null ) {
211+ try (final Reader reader =
212+ new BufferedReader (
213+ new InputStreamReader (new FileInputStream (previousSessionFile ), UTF_8 ))) {
214+ final Session session = serializer .getValue ().deserialize (reader , Session .class );
215+ if (session != null ) {
216+ Date timestamp = null ;
217+ if (sdkHint instanceof AbnormalExit ) {
216218 final AbnormalExit abnormalHint = (AbnormalExit ) sdkHint ;
217219 final @ Nullable Long abnormalExitTimestamp = abnormalHint .timestamp ();
218- Date timestamp = null ;
219220
220221 if (abnormalExitTimestamp != null ) {
221222 timestamp = DateUtils .getDateTime (abnormalExitTimestamp );
@@ -233,17 +234,33 @@ private void tryEndPreviousSession(final @NotNull Hint hint) {
233234
234235 final String abnormalMechanism = abnormalHint .mechanism ();
235236 session .update (Session .State .Abnormal , null , true , abnormalMechanism );
236- // we have to use the actual timestamp of the Abnormal Exit here to mark the session
237- // as finished at the time it happened
238- session .end (timestamp );
239- writeSessionToDisk (previousSessionFile , session );
237+ } else if (sdkHint instanceof NativeCrashExit ) {
238+ final NativeCrashExit nativeCrashHint = (NativeCrashExit ) sdkHint ;
239+ final @ NotNull Long nativeCrashExitTimestamp = nativeCrashHint .timestamp ();
240+
241+ timestamp = DateUtils .getDateTime (nativeCrashExitTimestamp );
242+ // sanity check if the native crash exit actually happened when the session was alive
243+ final Date sessionStart = session .getStarted ();
244+ if (sessionStart == null || timestamp .before (sessionStart )) {
245+ options
246+ .getLogger ()
247+ .log (
248+ WARNING ,
249+ "Native crash exit happened before previous session start, not ending the session." );
250+ return ;
251+ }
252+ session .update (Session .State .Crashed , null , true , null );
240253 }
241- } catch (Throwable e ) {
242- options .getLogger ().log (SentryLevel .ERROR , "Error processing previous session." , e );
254+ // we have to use the actual timestamp of the Abnormal or Crash Exit here to mark the
255+ // session as finished at the time it happened
256+ session .end (timestamp );
257+ writeSessionToDisk (previousSessionFile , session );
243258 }
244- } else {
245- options .getLogger ().log (DEBUG , "No previous session file to end." );
259+ } catch ( Throwable e ) {
260+ options .getLogger ().log (SentryLevel . ERROR , "Error processing previous session." , e );
246261 }
262+ } else {
263+ options .getLogger ().log (DEBUG , "No previous session file to end." );
247264 }
248265 }
249266
0 commit comments