8000 Merge pull request #630 from hrumhurum/gp-fix-b · dotnet-script/dotnet-script@2c21516 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c21516

Browse files
authored
Merge pull request #630 from hrumhurum/gp-fix-b
Memory leak in AppDomain.AssemblyResolve event subscription
2 parents bf52486 + 751e1be commit 2c21516

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/Dotnet.Script.Core/ScriptRunner.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,38 @@ public async Task<TReturn> Execute<TReturn>(string dllPath, IEnumerable<string>
3434
var runtimeDepsMap = ScriptCompiler.CreateScriptDependenciesMap(runtimeDeps);
3535
var assembly = Assembly.LoadFrom(dllPath); // this needs to be called prior to 'AppDomain.CurrentDomain.AssemblyResolve' event handler added
3636

37-
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => ResolveAssembly(args, runtimeDepsMap);
37+
Assembly OnResolve(object sender, ResolveEventArgs args) => ResolveAssembly(args, runtimeDepsMap);
3838

39-
var type = assembly.GetType("Submission#0");
40-
var method = type.GetMethod("<Factory>", BindingFlags.Static | BindingFlags.Public);
41-
42-
var globals = new CommandLineScriptGlobals(ScriptConsole.Out, CSharpObjectFormatter.Instance);
43-
foreach (var arg in commandLineArgs)
44-
globals.Args.Add(arg);
45-
46-
var submissionStates = new object[2];
47-
submissionStates[0] = globals;
48-
49-
var resultTask = method.Invoke(null, new[] { submissionStates }) as Task<TReturn>;
39+
AppDomain.CurrentDomain.AssemblyResolve += OnResolve;
5040
try
5141
{
52-
_ = await resultTask;
42+
var type = assembly.GetType("Submission#0");
43+
var method = type.GetMethod("<Factory>", BindingFl 8000 ags.Static | BindingFlags.Public);
44+
45+
var globals = new CommandLineScriptGlobals(ScriptConsole.Out, CSharpObjectFormatter.Instance);
46+
foreach (var arg in commandLineArgs)
47+
globals.Args.Add(arg);
48+
49+
var submissionStates = new object[2];
50+
submissionStates[0] = globals;
51+
52+
var resultTask = method.Invoke(null, new[] { submissionStates }) as Task<TReturn>;
53+
try
54+
{
55+
_ = await resultTask;
56+
}
57+
catch (System.Exception ex)
58+
{
59+
ScriptConsole.WriteError(ex.ToString());
60+
throw new ScriptRuntimeException("Script execution resulted in an exception.", ex);
61+
}
62+
63+
return await resultTask;
5364
}
54-
catch (System.Exception ex)
65+
finally
5566
{
56-
ScriptConsole.WriteError(ex.ToString());
57-
throw new ScriptRuntimeException("Script execution resulted in an exception.", ex);
67+
AppDomain.CurrentDomain.AssemblyResolve -= OnResolve;
5868
}
59-
60-
return await resultTask;
6169
}
6270

6371
public Task<TReturn> Execute<TReturn>(ScriptContext context)

0 commit comments

Comments
 (0)
0