Skip to content

Commit 45f7fc4

Browse files
kgjkotasCopilot
authored
Make IsAlive test reliable and re-enable it for crossgen2 (#117987)
* Make IsAlive test reliable and re-enable it for crossgen2 Co-authored-by: Jan Kotas <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 22a2c4d commit 45f7fc4

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/tests/GC/API/WeakReference/IsAlive.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
// Tests WeakReference.IsAlive : IsAlive=true if GC has not occurred on the object
4+
// Tests WeakReference.IsAlive : IsAlive=true if GC has not occurred on the object
55

66

77
using System;
@@ -11,35 +11,40 @@
1111

1212
public class Test_IsAlive {
1313
public static int[] array;
14-
14+
public static WeakReference weak;
15+
1516
[MethodImplAttribute(MethodImplOptions.NoInlining)]
1617
public static void CreateArray() {
1718
array = new int[50];
19+
// Create the weak reference inside of CreateArray to prevent a dangling 'array' reference
20+
// from surviving inside of TestEntryPoint.
21+
weak = new WeakReference(array);
1822
}
19-
23+
2024
[MethodImplAttribute(MethodImplOptions.NoInlining)]
2125
public static void DestroyArray() {
2226
array = null;
2327
}
24-
28+
2529
[Fact]
2630
public static int TestEntryPoint() {
2731
CreateArray();
2832

29-
WeakReference weak = new WeakReference(array);
30-
3133
bool ans1 = weak.IsAlive;
3234
Console.WriteLine(ans1);
33-
34-
if(ans1==false) { // GC.Collect() has already occurred..under GCStress
35-
Console.WriteLine("Test for WeakReference.IsAlive passed!");
36-
return 100;
35+
if (ans1 != true)
36+
{
37+
// This should be impossible; it would indicate that either the array was collected while reachable from
38+
// our static field, or that the WeakReference failed to track the array even though it's still alive.
39+
Console.WriteLine("Test for WeakReference.IsAlive failed!");
40+
return 2;
3741
}
3842

39-
//else, do an expicit collect.
43+
// Release our strong reference (via static field) so that the collector will no longer see array as reachable.
4044
DestroyArray();
45+
// Perform a blocking full collection which will hopefully collect the array.
4146
GC.Collect();
42-
47+
4348
bool ans2 = weak.IsAlive;
4449
Console.WriteLine(ans2);
4550

src/tests/issues.targets

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,6 @@
573573
<ExcludeList Include="$(XunitTestBinBase)/JIT/Methodical/Arrays/misc/arrres_il_r/**">
574574
<Issue>https://github.com/dotnet/runtime/issues/109311</Issue>
575575
</ExcludeList>
576-
<ExcludeList Include="$(XunitTestBinBase)/GC/API/WeakReference/IsAlive/**">
577-
<Issue>https://github.com/dotnet/runtime/issues/109312</Issue>
578-
</ExcludeList>
579576
<ExcludeList Include="$(XunitTestBinBase)/JIT/Directed/lifetime/lifetime2/**">
580577
<Issue>https://github.com/dotnet/runtime/issues/109313</Issue>
581578
</ExcludeList>

0 commit comments

Comments
 (0)