feat: attempt to prevent some plugin disposal crashes by stopping Framework::Update event dispatching before unloading

This commit is contained in:
goat 2021-04-20 22:01:17 +02:00
parent 35d88ac554
commit de98b75336
No known key found for this signature in database
GPG key ID: F18F057873895461
2 changed files with 42 additions and 31 deletions

View file

@ -358,6 +358,10 @@ namespace Dalamud
{ {
try try
{ {
// this must be done before unloading plugins, to prevent crashes due to errors
// in plugin cleanup
this.Framework.DispatchUpdateEvents = false;
// this must be done before unloading plugins, or it can cause a race condition // this must be done before unloading plugins, or it can cause a race condition
// due to rendering happening on another thread, where a plugin might receive // due to rendering happening on another thread, where a plugin might receive
// a render call after it has been disposed, which can crash if it attempts to // a render call after it has been disposed, which can crash if it attempts to

View file

@ -17,6 +17,8 @@ namespace Dalamud.Game.Internal {
public sealed class Framework : IDisposable { public sealed class Framework : IDisposable {
private readonly Dalamud dalamud; private readonly Dalamud dalamud;
internal bool DispatchUpdateEvents { get; set; } = true;
[UnmanagedFunctionPointer(CallingConvention.ThisCall)] [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate bool OnUpdateDetour(IntPtr framework); private delegate bool OnUpdateDetour(IntPtr framework);
@ -40,7 +42,7 @@ namespace Dalamud.Game.Internal {
/// </summary> /// </summary>
public FrameworkAddressResolver Address { get; } public FrameworkAddressResolver Address { get; }
#region Stats #region Stats
public static bool StatsEnabled { get; set; } public static bool StatsEnabled { get; set; }
public static Dictionary<string, List<double>> StatsHistory = new Dictionary<string, List<double>>(); public static Dictionary<string, List<double>> StatsHistory = new Dictionary<string, List<double>>();
private static Stopwatch statsStopwatch = new Stopwatch(); private static Stopwatch statsStopwatch = new Stopwatch();
@ -126,6 +128,8 @@ namespace Dalamud.Game.Internal {
Log.Error(ex, "Exception while handling Framework::Update hook."); Log.Error(ex, "Exception while handling Framework::Update hook.");
} }
if (this.DispatchUpdateEvents)
{
try { try {
if (StatsEnabled && OnUpdateEvent != null) { if (StatsEnabled && OnUpdateEvent != null) {
// Stat Tracking for Framework Updates // Stat Tracking for Framework Updates
@ -159,6 +163,7 @@ namespace Dalamud.Game.Internal {
} catch (Exception ex) { } catch (Exception ex) {
Log.Error(ex, "Exception while dispatching Framework::Update event."); Log.Error(ex, "Exception while dispatching Framework::Update event.");
} }
}
return this.updateHook.Original(framework); return this.updateHook.Original(framework);
} }
@ -166,6 +171,8 @@ namespace Dalamud.Game.Internal {
private IntPtr HandleFrameworkDestroy() { private IntPtr HandleFrameworkDestroy() {
Log.Information("Framework::OnDestroy!"); Log.Information("Framework::OnDestroy!");
this.DispatchUpdateEvents = false;
// Store the pointer to the original trampoline location // Store the pointer to the original trampoline location
var originalPtr = Marshal.GetFunctionPointerForDelegate(this.destroyHook.Original); var originalPtr = Marshal.GetFunctionPointerForDelegate(this.destroyHook.Original);