Skip to content

It seems that IEvaluator.Reset does not actually clear the referenced assemblies #464

@frank-thole

Description

@frank-thole

This is the (very simplified) code I am using:

class ScriptContextManager
{
    private IEvaluator _evaluator = CSScript.Evaluator.With(eval => eval.IsAssemblyUnloadingEnabled = true);

    public void LoadAssembly(string assemblyPath)
    {
        var list1 = _evaluator.GetReferencedAssemblies();

        _evaluator.With(eval => eval.IsAssemblyUnloadingEnabled = true).ReferenceAssembly(assemblyPath);

        var list2 = _evaluator.GetReferencedAssemblies();
    }

    public void UnloadAndWait()
    {
        _evaluator = _evaluator.With(eval => eval.IsAssemblyUnloadingEnabled = true).Reset(false);

        var list3 = _evaluator.GetReferencedAssemblies();

        for (int i = 0; i < 1000; i++)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Thread.Sleep(10);
        }

        var list4 = _evaluator.GetReferencedAssemblies();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var manager = new ScriptContextManager();
        manager.LoadAssembly("C:\\Users\\frankt\\AppData\\Local\\Temp\\CSScriptTest\\2c808b71-b1c0-4c52-9ba1-2e07e971ef76\\MQTTnet.dll");
        manager.UnloadAndWait();
    }
}

list1 contains 23 assemblies.
list2 contains 24 assemblies, because MQTTnet.dll gets added.

list3 and list4 still contain 24 assemblies. In Visual Studio it can also be seen that MQTTnet.dll stays around in the Modules view.

Documentation says on Reset function: "Resetting means clearing all referenced assemblies, recreating evaluation infrastructure (e.g. compiler setting) and reconnection to or recreation of the underlying compiling services."

I am also unable to find in the cs-script repository where the "refAssemblies" list is cleared.

However, it seems this is causing the application to cling on to the MQTTnet.dll, and thus doesn't allow replacing the MQTTnet.dll for another version during the lifetime of the application.

I am not sure if the IsAssemblyUnloadingEnabled = true option is needed in this scenario. Without it there is no difference in behavior.

Am I doing something wrong, should I use some other code? Or is this an actual issue in the cs-script code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions