Skip to content

Commit

Permalink
Merge branch 'next' into plat-13372-unhandledCallbackFix
Browse files Browse the repository at this point in the history
  • Loading branch information
richardelms authored Jan 7, 2025
2 parents b101410 + 2c13735 commit 8803e89
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Bugsnag/Assets/Bugsnag/Runtime/BugsnagAutoInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace BugsnagUnity

public class BugsnagAutoInit
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void OnBeforeSceneLoadRuntimeMethod()
{
var settings = Resources.Load<BugsnagSettingsObject>("Bugsnag/BugsnagSettingsObject");
Expand Down
9 changes: 1 addition & 8 deletions Bugsnag/Assets/Bugsnag/Runtime/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ private void SetupAdvancedExceptionInterceptor()

public Client(INativeClient nativeClient)
{
InitMainthreadDispatcher();
NativeClient = nativeClient;
_errorBuilder = new ErrorBuilder(nativeClient);
CacheManager = new CacheManager(Configuration);
Expand Down Expand Up @@ -144,11 +143,6 @@ public Client(INativeClient nativeClient)
InitLogHandlers();
}

private void InitMainthreadDispatcher()
{
MainThreadDispatchBehaviour.Instance();
}

private bool IsUnity2019OrHigher()
{
var version = Application.unityVersion;
Expand Down Expand Up @@ -384,8 +378,7 @@ private void Notify(Error[] exceptions, HandledState handledState, Func<IEvent,
{
try
{
var asyncHandler = MainThreadDispatchBehaviour.Instance();
asyncHandler.Enqueue(() => { NotifyOnMainThread(exceptions, handledState, callback,logType, correlation); });
MainThreadDispatchBehaviour.Enqueue(() => { NotifyOnMainThread(exceptions, handledState, callback,logType, correlation); });
}
catch
{
Expand Down
4 changes: 2 additions & 2 deletions Bugsnag/Assets/Bugsnag/Runtime/Delivery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void Deliver(IPayload payload)
}
try
{
MainThreadDispatchBehaviour.Instance().Enqueue(PushToServer(payload));
MainThreadDispatchBehaviour.Enqueue(PushToServer(payload));
}
catch
{
Expand Down Expand Up @@ -526,7 +526,7 @@ public void StartDeliveringCachedPayloads()
try
{
_finishedCacheDeliveries.Clear();
MainThreadDispatchBehaviour.Instance().Enqueue(DeliverCachedPayloads());
MainThreadDispatchBehaviour.Enqueue(DeliverCachedPayloads());
}
catch
{
Expand Down
106 changes: 40 additions & 66 deletions Bugsnag/Assets/Bugsnag/Runtime/MainThreadDispatchBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
/*
Copyright 2015 Pim de Witte All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.LowLevel;

/// Author: Pim de Witte (pimdewitte.com) and contributors
/// <summary>
/// A thread-safe class which holds a queue with actions to execute on the next Update() method. It can be used to make calls to the main thread for
/// things such as UI Manipulation in Unity. It was developed for use in combination with the Firebase Unity plugin, which uses separate threads for event handling
/// </summary>
namespace BugsnagUnity
{
public class MainThreadDispatchBehaviour : MonoBehaviour
public class BugsnagCoroutineRunner : MonoBehaviour
{
private static BugsnagCoroutineRunner _instance;

private static MainThreadDispatchBehaviour _instance;

private static readonly Queue<Action> _executionQueue = new Queue<Action>();
public static BugsnagCoroutineRunner Instance
{
get
{
if (_instance == null)
{
var runnerObject = new GameObject("BugsnagCoroutineRunner");
_instance = runnerObject.AddComponent<BugsnagCoroutineRunner>();
DontDestroyOnLoad(runnerObject);
}
return _instance;
}
}
}
public class MainThreadDispatchBehaviour
{

private static readonly Queue<Action> _executionQueue = new Queue<Action>();

public static MainThreadDispatchBehaviour Instance()
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void InitializeLoop()
{
if (_instance == null)
var playerLoop = PlayerLoop.GetCurrentPlayerLoop();
var newSystem = new PlayerLoopSystem
{
_instance = new GameObject("Bugsnag main thread dispatcher").AddComponent<MainThreadDispatchBehaviour>();
}
return _instance;
updateDelegate = OnUpdate
};

var systems = new List<PlayerLoopSystem>(playerLoop.subSystemList);
systems.Insert(0, newSystem);
playerLoop.subSystemList = systems.ToArray();
PlayerLoop.SetPlayerLoop(playerLoop);
}

public void Update()
private static void OnUpdate()
{
lock (_executionQueue)
{
Expand All @@ -54,50 +55,23 @@ public void Update()
}
}

/// <summary>
/// Locks the queue and adds the IEnumerator to the queue
/// </summary>
/// <param name="action">IEnumerator function that will be executed from the main thread.</param>
public void Enqueue(IEnumerator action)
public static void Enqueue(IEnumerator action)
{
lock (_executionQueue)
{
_executionQueue.Enqueue(() =>
{
StartCoroutine(action);
BugsnagCoroutineRunner.Instance.StartCoroutine(action);
});
}
}

/// <summary>
/// Locks the queue and adds the Action to the queue
/// </summary>
/// <param name="action">function that will be executed from the main thread.</param>
public void Enqueue(Action action)
public static void Enqueue(Action action)
{
Enqueue(ActionWrapper(action));
}
IEnumerator ActionWrapper(Action a)
{
a();
yield return null;
}

public void EnqueueWithDelayCoroutine(Action action, float delay)
{
StartCoroutine(DelayAction(action, delay));
}

private IEnumerator DelayAction(Action action, float delay)
{
yield return new WaitForSeconds(delay);
action.Invoke();
}

private void Awake()
{
DontDestroyOnLoad(gameObject);
lock (_executionQueue)
{
_executionQueue.Enqueue(action);
}
}

}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## TBD

### Enhancements

- Allow the notifier to be started much earlier in the Unity lifecycle. [#862](https://github.com/bugsnag/bugsnag-unity/pull/862)

### Bug Fixes

- Fixed an issue where session handled/unhandled event counts were not updated if the handled status of the event was changed in a callback. [#865](https://github.com/bugsnag/bugsnag-unity/pull/865)
Expand Down

0 comments on commit 8803e89

Please sign in to comment.