From d8bed85c8f7c693701e9ea9b699b70c3b10fa45b Mon Sep 17 00:00:00 2001 From: Nicolas Gnyra Date: Mon, 13 Nov 2023 17:01:46 -0500 Subject: [PATCH] Fix exception when `Changed` is called in Zenject-invoked `Dispose` --- IPA.Loader/Config/ConfigRuntime.cs | 21 +++++++++++++------ .../GeneratedStoreImpl/IGeneratedStore.cs | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/IPA.Loader/Config/ConfigRuntime.cs b/IPA.Loader/Config/ConfigRuntime.cs index 609e579a..28e510a0 100644 --- a/IPA.Loader/Config/ConfigRuntime.cs +++ b/IPA.Loader/Config/ConfigRuntime.cs @@ -27,11 +27,11 @@ namespace IPA.Config private static readonly ConcurrentBag configs = new(); private static readonly AutoResetEvent configsChangedWatcher = new(false); - public static readonly BlockingCollection RequiresSave = new(); private static readonly ConcurrentDictionary watchers = new ConcurrentDictionary(new DirInfoEqComparer()); private static readonly ConcurrentDictionary> watcherTrackConfigs = new ConcurrentDictionary>(); + private static BlockingCollection requiresSave = new(); private static SingleThreadTaskScheduler loadScheduler; private static TaskFactory loadFactory; private static Thread saveThread; @@ -62,6 +62,11 @@ namespace IPA.Config AppDomain.CurrentDomain.ProcessExit += ShutdownRuntime; } + internal static void AddRequiresSave(IConfigStore configStore) + { + requiresSave?.Add(configStore); + } + private static void ShutdownRuntime(object sender, EventArgs e) => ShutdownRuntime(); internal static void ShutdownRuntime() @@ -79,6 +84,9 @@ namespace IPA.Config saveThread.Abort(); // eww, but i don't like any of the other potential solutions SaveAll(); + + requiresSave.Dispose(); + requiresSave = null; } catch { @@ -218,9 +226,14 @@ namespace IPA.Config private static void SaveThread() { + if (requiresSave == null) + { + return; + } + try { - foreach (var item in RequiresSave.GetConsumingEnumerable()) + foreach (var item in requiresSave.GetConsumingEnumerable()) { try { @@ -242,10 +255,6 @@ namespace IPA.Config { // we got aborted :( } - finally - { - RequiresSave.Dispose(); - } } private static void LegacySaveThread() diff --git a/IPA.Loader/Config/Stores/GeneratedStoreImpl/IGeneratedStore.cs b/IPA.Loader/Config/Stores/GeneratedStoreImpl/IGeneratedStore.cs index ee8bc0d8..d490227a 100644 --- a/IPA.Loader/Config/Stores/GeneratedStoreImpl/IGeneratedStore.cs +++ b/IPA.Loader/Config/Stores/GeneratedStoreImpl/IGeneratedStore.cs @@ -59,7 +59,7 @@ namespace IPA.Config.Stores public static void ImplSignalChanged(IGeneratedStore s) => FindImpl(s)?.SignalChanged(); public void SignalChanged() { - ConfigRuntime.RequiresSave.Add(this); + ConfigRuntime.AddRequiresSave(this); } internal static MethodInfo ImplInvokeChangedMethod = typeof(Impl).GetMethod(nameof(ImplInvokeChanged));