Several changes to improve unloadability of assemblies.

This commit is contained in:
MapleWheels
2026-04-29 10:20:16 -04:00
parent 3f199a9500
commit 66717c7fe7
5 changed files with 17 additions and 10 deletions
@@ -558,7 +558,8 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
IsDisposed = true; IsDisposed = true;
this.Unload(); this.Unload();
this.DisposeInternal(); this.DisposeInternal();
GC.SuppressFinalize(this); // we want to call base finalizers
//GC.SuppressFinalize(this);
} }
~AssemblyLoader() ~AssemblyLoader()
@@ -579,6 +580,7 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
} }
var wf = new WeakReference<IAssemblyLoaderService>(this); var wf = new WeakReference<IAssemblyLoaderService>(this);
_loadedAssemblyData.Clear();
_onUnload?.Invoke(this); _onUnload?.Invoke(this);
} }
@@ -23,6 +23,7 @@ public sealed partial class ModConfigFileParserService :
public ModConfigFileParserService(IStorageService storageService) public ModConfigFileParserService(IStorageService storageService)
{ {
_storageService = storageService; _storageService = storageService;
_storageService.UseCaching = false;
} }
#region Dispose #region Dispose
@@ -45,6 +45,7 @@ public sealed class ModConfigService : IModConfigService
#if CLIENT #if CLIENT
_stylesParserService = stylesParserService; _stylesParserService = stylesParserService;
#endif #endif
_storageService.UseCaching = false;
} }
#region Dispose #region Dispose
@@ -174,6 +174,7 @@ public class PluginManagementService : IAssemblyManagementService
_pluginInjectorContainer?.Dispose(); _pluginInjectorContainer?.Dispose();
_pluginInjectorContainer = null; _pluginInjectorContainer = null;
ReflectionUtils.ResetCache();
foreach (var loader in _assemblyLoaders) foreach (var loader in _assemblyLoaders)
{ {
try try
@@ -184,14 +185,6 @@ public class PluginManagementService : IAssemblyManagementService
catch (Exception e) catch (Exception e)
{ {
_logger?.LogError($"Failed to dispose of {nameof(IAssemblyLoaderService)} for ContentPackage {loader.Key.Name}: \n{e.Message}"); _logger?.LogError($"Failed to dispose of {nameof(IAssemblyLoaderService)} for ContentPackage {loader.Key.Name}: \n{e.Message}");
if (loader.Value.Assemblies.Any())
{
foreach (var ass in loader.Value.Assemblies)
{
_logger?.LogWarning($"{nameof(PluginManagementService)}: Fallback manual unsubscription of assemblies: {ass.GetName()}");
ReflectionUtils.RemoveAssemblyFromCache(ass);
}
}
} }
} }
_assemblyLoaders.Clear(); _assemblyLoaders.Clear();
@@ -837,7 +830,9 @@ public class PluginManagementService : IAssemblyManagementService
_assemblyLoaders.Clear(); _assemblyLoaders.Clear();
_storageService.PurgeCache(); _storageService.PurgeCache();
GC.Collect();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true); GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true);
GC.WaitForFullGCComplete(1000);
#if DEBUG #if DEBUG
// Print still loaded assembly load ctx after giving some time // Print still loaded assembly load ctx after giving some time
@@ -917,6 +912,14 @@ public class PluginManagementService : IAssemblyManagementService
_eventService.Value.RemoveDispatcherEventService(_pluginEventService); _eventService.Value.RemoveDispatcherEventService(_pluginEventService);
_pluginEventService = null; _pluginEventService = null;
} }
try
{
_pluginInjectorContainer.Dispose();
}
catch (Exception e)
{
results.WithError(new ExceptionalError(e));
}
_pluginInjectorContainer = null; _pluginInjectorContainer = null;
_pluginInstances.Clear(); _pluginInstances.Clear();
@@ -114,8 +114,8 @@ namespace Barotrauma
public static void ResetCache() public static void ResetCache()
{ {
CachedNonAbstractTypes.Clear(); CachedNonAbstractTypes.Clear();
CachedNonAbstractTypes.TryAdd(typeof(ReflectionUtils).Assembly, typeof(ReflectionUtils).Assembly.GetTypes().Where(t => !t.IsAbstract).ToImmutableArray());
TypeSearchCache.Clear(); TypeSearchCache.Clear();
CachedNonAbstractTypes.TryAdd(typeof(ReflectionUtils).Assembly, typeof(ReflectionUtils).Assembly.GetTypes().Where(t => !t.IsAbstract).ToImmutableArray());
} }
public static Type? GetType(string nameWithNamespace) public static Type? GetType(string nameWithNamespace)