Skip to content

Commit

Permalink
Experimental replace Task with Thread with increased stack size
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp committed Jan 22, 2025
1 parent 705014b commit 78b21f0
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions Src/IronPythonTest/Cases/CaseExecuter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Threading;

using IronPython;
using IronPython.Hosting;
Expand Down Expand Up @@ -289,18 +289,34 @@ private int GetResult(TestInfo testcase, ScriptEngine engine, ScriptSource sourc
engine.GetSysModule().SetVariable("argv", PythonList.FromArrayNoCopy(new object[] { source.Path }));
var compiledCode = source.Compile(new IronPython.Compiler.PythonCompilerOptions() { ModuleName = "__main__" });

var task = Task<int>.Run(() => {
int res = 0;
int maxStackSize = 2 * 1024 * 1024; // 2 MiB
var thread = new Thread(() => {
try {
return engine.Operations.ConvertTo<int>(compiledCode.Execute(scope) ?? 0);
res = engine.Operations.ConvertTo<int>(compiledCode.Execute(scope) ?? 0);
} catch (SystemExitException ex) {
return ex.GetExitCode(out _);
res = ex.GetExitCode(out object otherCode);
} catch (ThreadAbortException) {
#pragma warning disable SYSLIB0006 // 'Thread.ResetAbort is not supported and throws PlatformNotSupportedException.'
Thread.ResetAbort();
#pragma warning restore SYSLIB0006
}
}, maxStackSize) {
IsBackground = true
};

thread.Start();

if (!thread.Join(testcase.Options.Timeout)) {
if(!ClrModule.IsNetCoreApp) {
#pragma warning disable SYSLIB0006 // 'Thread.Abort is not supported and throws PlatformNotSupportedException.'
thread.Abort();
#pragma warning restore SYSLIB0006
}
});
if (!task.Wait(testcase.Options.Timeout)) {
NUnit.Framework.TestContext.Error.WriteLine($"{testcase.Name} timed out after {testcase.Options.Timeout / 1000.0} seconds.");
return -1;
}
return task.Result;
return res;
} finally {
Environment.CurrentDirectory = cwd;
}
Expand Down

0 comments on commit 78b21f0

Please sign in to comment.