Skip to content

Commit 49e18cb

Browse files
authored
Replace Task with Thread with increased stack size (#1883)
The increase in on macOS and Windows, on Linux it is effectively a decrease.
1 parent 2aca667 commit 49e18cb

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

tests/IronPython.Tests/Cases/CaseExecuter.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System.Runtime.CompilerServices;
1212
using System.Runtime.InteropServices;
1313
using System.Text.RegularExpressions;
14-
using System.Threading.Tasks;
14+
using System.Threading;
1515

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

292-
var task = Task<int>.Run(() => {
292+
int res = 0;
293+
int maxStackSize = 2 * 1024 * 1024; // 2 MiB
294+
var thread = new Thread(() => {
293295
try {
294-
return engine.Operations.ConvertTo<int>(compiledCode.Execute(scope) ?? 0);
296+
res = engine.Operations.ConvertTo<int>(compiledCode.Execute(scope) ?? 0);
295297
} catch (SystemExitException ex) {
296-
return ex.GetExitCode(out _);
298+
res = ex.GetExitCode(out object otherCode);
299+
} catch (ThreadAbortException) {
300+
#pragma warning disable SYSLIB0006 // 'Thread.ResetAbort is not supported and throws PlatformNotSupportedException.'
301+
Thread.ResetAbort();
302+
#pragma warning restore SYSLIB0006
303+
}
304+
}, maxStackSize) {
305+
IsBackground = true
306+
};
307+
308+
thread.Start();
309+
310+
if (!thread.Join(testcase.Options.Timeout)) {
311+
if(!ClrModule.IsNetCoreApp) {
312+
#pragma warning disable SYSLIB0006 // 'Thread.Abort is not supported and throws PlatformNotSupportedException.'
313+
thread.Abort();
314+
#pragma warning restore SYSLIB0006
297315
}
298-
});
299-
if (!task.Wait(testcase.Options.Timeout)) {
300316
NUnit.Framework.TestContext.Error.WriteLine($"{testcase.Name} timed out after {testcase.Options.Timeout / 1000.0} seconds.");
301317
return -1;
302318
}
303-
return task.Result;
319+
return res;
304320
} finally {
305321
Environment.CurrentDirectory = cwd;
306322
}

0 commit comments

Comments
 (0)