Skip to content

Commit 49cc001

Browse files
evgeny-polMax
authored andcommitted
start SendUnhandledExceptionReports coroutine only when needed (#187)
1 parent a6c9bdc commit 49cc001

File tree

1 file changed

+14
-33
lines changed
  • Assets/AppCenter/Plugins/AppCenterSDK/Crashes/Shared

1 file changed

+14
-33
lines changed

Assets/AppCenter/Plugins/AppCenterSDK/Crashes/Shared/Crashes.cs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class Crashes
2727

2828
#if UNITY_ANDROID
2929
private static Queue<Exception> _unhandledExceptions = new Queue<Exception>();
30-
private static bool _unhandledExceptionsExists = false;
3130
#endif
3231

3332
public static void PrepareEventHandlers()
@@ -78,18 +77,16 @@ public static void OnHandleUnresolvedException(object sender, UnhandledException
7877
{
7978
return;
8079
}
81-
8280
var exception = args.ExceptionObject as Exception;
8381
if (exception != null)
8482
{
8583
Debug.Log("Unhandled exception: " + exception.ToString());
86-
8784
#if UNITY_ANDROID
8885
lock (_unhandledExceptions)
8986
{
9087
_unhandledExceptions.Enqueue(exception);
91-
_unhandledExceptionsExists = true;
9288
}
89+
UnityCoroutineHelper.StartCoroutine(SendUnhandledExceptionReports);
9390
#else
9491
var exceptionWrapper = CreateWrapperException(exception);
9592
CrashesInternal.TrackException(exceptionWrapper.GetRawObject());
@@ -138,9 +135,7 @@ public static void ReportUnhandledExceptions(bool enabled)
138135
{
139136
return;
140137
}
141-
142138
_reportUnhandledExceptions = enabled;
143-
144139
if (enabled)
145140
{
146141
SubscribeToUnhandledExceptions();
@@ -282,10 +277,6 @@ private static void SubscribeToUnhandledExceptions()
282277
Application.logMessageReceived += OnHandleLog;
283278
System.AppDomain.CurrentDomain.UnhandledException += OnHandleUnresolvedException;
284279
#endif
285-
286-
#if !UNITY_EDITOR && UNITY_ANDROID
287-
UnityCoroutineHelper.StartCoroutine(SendUnhandledExceptionReports);
288-
#endif
289280
}
290281

291282
private static void UnsubscribeFromUnhandledExceptions()
@@ -307,37 +298,27 @@ private static void HandleAppCenterInitialized()
307298
#if UNITY_ANDROID
308299
private static IEnumerator SendUnhandledExceptionReports()
309300
{
301+
yield return null; // ensure that code is executed in main thread
310302
while (true)
311303
{
312-
if (!_reportUnhandledExceptions)
313-
{
314-
yield break;
315-
}
316-
317-
if (_unhandledExceptionsExists)
304+
Exception exception = null;
305+
lock (_unhandledExceptions)
318306
{
319-
Exception exception = null;
320-
lock (_unhandledExceptions)
307+
if (_unhandledExceptions.Count > 0)
321308
{
322-
if (_unhandledExceptions.Count > 0)
323-
{
324-
exception = _unhandledExceptions.Dequeue();
325-
}
326-
327-
if (_unhandledExceptions.Count == 0)
328-
{
329-
_unhandledExceptionsExists = false;
330-
}
309+
exception = _unhandledExceptions.Dequeue();
331310
}
332-
333-
if (exception != null)
311+
else
334312
{
335-
var exceptionWrapper = CreateWrapperException(exception);
336-
CrashesInternal.TrackException(exceptionWrapper.GetRawObject());
313+
yield break;
337314
}
338315
}
339-
340-
yield return null;
316+
if (exception != null)
317+
{
318+
var exceptionWrapper = CreateWrapperException(exception);
319+
CrashesInternal.TrackException(exceptionWrapper.GetRawObject());
320+
}
321+
yield return null; // report remaining exceptions on next frames
341322
}
342323
}
343324
#endif

0 commit comments

Comments
 (0)