Skip to content

Commit af315f4

Browse files
authored
[2020-02][corlib] ThreadAbortException protection for ArraySortHelper (#20468)
* [test] Abort a thread doing a long-running Array.Sort The custom comparer is wrapped in a try/finally. Verify that it doesn't turn thread abort exceptions into some other exception. Regression test for #15418 * Bump corert To pick up fix for #15418 * disable new test on platforms without Thread.Abort
1 parent ca11fb0 commit af315f4

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

mcs/class/corlib/Test/System/ArrayTest.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Collections;
1313
using System.Globalization;
1414
using System.Reflection;
15+
using System.Threading;
1516
using System.Collections.Generic;
1617

1718
namespace MonoTests.System
@@ -3745,5 +3746,66 @@ public unsafe void PointerArraysBoxing ()
37453746
Assert.Throws<NotSupportedException> (() => { var _ = x.GetValue (0); }, "#2");
37463747
Assert.Throws<NotSupportedException> (() => { x.SetValue (0, 0); }, "#3");
37473748
}
3749+
3750+
3751+
#if MONO_FEATURE_THREAD_ABORT
3752+
public struct J
3753+
{
3754+
public int i;
3755+
3756+
public J(int i_) { i = i_; }
3757+
}
3758+
3759+
struct JComp : IComparer<J>
3760+
{
3761+
public int Compare(J x, J y)
3762+
{
3763+
int val = 0;
3764+
Thread.Sleep (Timeout.Infinite);
3765+
return val;
3766+
}
3767+
}
3768+
3769+
class ArraySortAbortData {
3770+
internal ManualResetEventSlim mre;
3771+
internal bool threw;
3772+
3773+
internal ArraySortAbortData () {
3774+
mre = new ManualResetEventSlim ();
3775+
threw = false;
3776+
}
3777+
}
3778+
3779+
[Test]
3780+
public void ArraySortAbort ()
3781+
{
3782+
var d = new ArraySortAbortData();
3783+
var t = new Thread(RunArraySort);
3784+
t.Start(d);
3785+
d.mre.Wait();
3786+
Thread.Sleep(400);
3787+
t.Abort();
3788+
t.Join();
3789+
Assert.IsFalse (d.threw);
3790+
}
3791+
3792+
public static void RunArraySort(object data)
3793+
{
3794+
var d = data as ArraySortAbortData;
3795+
int n = 10;
3796+
var a = new J[n];
3797+
for (int i = 0; i < n; ++i)
3798+
{
3799+
a[i] = new J(n - i);
3800+
}
3801+
d.mre.Set();
3802+
try {
3803+
Array.Sort(a, 0, n, new JComp());
3804+
} catch (InvalidOperationException) {
3805+
// t.Abort in ArraySortAbort should _not_ end up here
3806+
d.threw = true;
3807+
}
3808+
}
3809+
#endif
37483810
}
37493811
}

0 commit comments

Comments
 (0)