4141import java .util .List ;
4242import java .util .Locale ;
4343import java .util .Map ;
44- import java .util .concurrent .RejectedExecutionException ;
4544import java .util .concurrent .atomic .AtomicReference ;
4645import org .jetbrains .annotations .ApiStatus ;
4746import org .jetbrains .annotations .NotNull ;
@@ -175,39 +174,45 @@ public static SentryId captureEnvelope(
175174 return null ;
176175 }
177176
177+ final @ NotNull ISerializer serializer = options .getSerializer ();
178+ final @ NotNull EnvelopeEventState eventState ;
178179 try {
179- final @ NotNull ISerializer serializer = options .getSerializer ();
180- final @ NotNull EnvelopeEventState eventState = eventStateOf (envelope , serializer );
180+ eventState = eventStateOf (envelope , serializer );
181+ } catch (Exception e ) {
182+ // getEvent reads through a Callable, whose call() declares Exception
183+ options .getLogger ().log (SentryLevel .ERROR , "Failed to inspect envelope events" , e );
184+ return null ;
185+ }
181186
182- final @ NotNull List <SentryEnvelopeItem > envelopeItems = new ArrayList <>();
183- for (SentryEnvelopeItem item : envelope .getItems ()) {
184- envelopeItems .add (item );
185- }
187+ final @ NotNull List <SentryEnvelopeItem > envelopeItems = new ArrayList <>();
188+ for (SentryEnvelopeItem item : envelope .getItems ()) {
189+ envelopeItems .add (item );
190+ }
186191
187- // update session and add it to envelope if necessary
188- final @ Nullable Session .State status =
189- eventState == EnvelopeEventState .UNHANDLED ? Session .State .Crashed : null ;
190- final @ Nullable Session session =
191- updateSession (scopes , options , status , eventState != EnvelopeEventState .NONE );
192- if (session != null ) {
193- final SentryEnvelopeItem sessionItem = SentryEnvelopeItem .fromSession (serializer , session );
194- envelopeItems .add (sessionItem );
195- deleteCurrentSessionFile (
196- options ,
197- // should be sync if going to crash or already not a main thread
198- !maybeStartNewSession || !scopes .getOptions ().getThreadChecker ().isMainThread ());
199- if (maybeStartNewSession ) {
200- scopes .startSession ();
201- }
192+ // update session and add it to envelope if necessary
193+ final @ Nullable Session .State status =
194+ eventState == EnvelopeEventState .UNHANDLED ? Session .State .Crashed : null ;
195+ final @ Nullable Session session =
196+ updateSession (scopes , options , status , eventState != EnvelopeEventState .NONE );
197+ if (session != null ) {
198+ try {
199+ envelopeItems .add (SentryEnvelopeItem .fromSession (serializer , session ));
200+ } catch (IOException e ) {
201+ options .getLogger ().log (SentryLevel .ERROR , "Failed to add session to envelope" , e );
202+ return null ;
203+ }
204+ deleteCurrentSessionFile (
205+ options ,
206+ // should be sync if going to crash or already not a main thread
207+ !maybeStartNewSession || !scopes .getOptions ().getThreadChecker ().isMainThread ());
208+ if (maybeStartNewSession ) {
209+ scopes .startSession ();
202210 }
203-
204- final SentryEnvelope repackagedEnvelope =
205- new SentryEnvelope (envelope .getHeader (), envelopeItems );
206- return scopes .captureEnvelope (repackagedEnvelope );
207- } catch (Throwable t ) {
208- options .getLogger ().log (SentryLevel .ERROR , "Failed to capture envelope" , t );
209211 }
210- return null ;
212+
213+ final SentryEnvelope repackagedEnvelope =
214+ new SentryEnvelope (envelope .getHeader (), envelopeItems );
215+ return scopes .captureEnvelope (repackagedEnvelope );
211216 }
212217
213218 /**
@@ -262,7 +267,11 @@ public static SentryId captureEnvelopeNonTerminating(final @NotNull byte[] envel
262267 }
263268
264269 /**
265- * Flags and persists the current session for a non-terminating hybrid error.
270+ * Flags the current session for a non-terminating hybrid error and persists it before returning,
271+ * so the marker survives an immediate process death.
272+ *
273+ * <p>The write stays inside {@code withSession}: the scope's session lock is what keeps the
274+ * session from being ended or replaced mid-write.
266275 *
267276 * @param unhandled {@code true} if the error was unhandled ({@code mechanism.handled=false})
268277 */
@@ -287,32 +296,12 @@ private static void updateSessionNonTerminating(final boolean unhandled) {
287296 unhandled
288297 ? session .recordNonTerminatingUnhandledError ()
289298 : session .update (null , null , true , null );
290- if (recorded ) {
291- schedulePersistSession ( options , session . clone () );
299+ if (recorded && options . getEnvelopeDiskCache () instanceof EnvelopeCache ) {
300+ (( EnvelopeCache ) options . getEnvelopeDiskCache ()). persistCurrentSession ( session );
292301 }
293302 }));
294303 }
295304
296- /**
297- * This function is mostly called from Flutter where capturing envelope is run in a background
298- * thread.
299- *
300- * <p>Nonetheless we should run this in the same executor service as the deleteCurrentSessionFile
301- * function for consistency.
302- */
303- private static void schedulePersistSession (
304- final @ NotNull SentryOptions options , final @ NotNull Session session ) {
305- if (!(options .getEnvelopeDiskCache () instanceof EnvelopeCache )) {
306- return ;
307- }
308- final @ NotNull EnvelopeCache cache = (EnvelopeCache ) options .getEnvelopeDiskCache ();
309- try {
310- options .getExecutorService ().submit (() -> cache .persistCurrentSession (session ));
311- } catch (RejectedExecutionException e ) {
312- options .getLogger ().log (WARNING , "Submission of session persisting rejected." , e );
313- }
314- }
315-
316305 /** What the events inside an envelope amount to, from the session's point of view. */
317306 private enum EnvelopeEventState {
318307 /** No event carried an exception. */
0 commit comments