From 291f7bb4b019911179346a1d039dca5ebc670493 Mon Sep 17 00:00:00 2001 From: Katelyn Gadd Date: Wed, 23 Jul 2025 09:27:41 -0700 Subject: [PATCH 1/2] Make IsAlive test reliable and re-enable it for crossgen2 --- src/tests/GC/API/WeakReference/IsAlive.cs | 29 +++++++++++++---------- src/tests/issues.targets | 3 --- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/tests/GC/API/WeakReference/IsAlive.cs b/src/tests/GC/API/WeakReference/IsAlive.cs index db3a01040d4cf3..0fe1589a5b25ed 100644 --- a/src/tests/GC/API/WeakReference/IsAlive.cs +++ b/src/tests/GC/API/WeakReference/IsAlive.cs @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// Tests WeakReference.IsAlive : IsAlive=true if GC has not occurred on the object +// Tests WeakReference.IsAlive : IsAlive=true if GC has not occurred on the object using System; @@ -11,35 +11,40 @@ public class Test_IsAlive { public static int[] array; - + public static WeakReference weak; + [MethodImplAttribute(MethodImplOptions.NoInlining)] public static void CreateArray() { array = new int[50]; + // HACK: Create the weak reference inside of CreateArray to prevent a dangling 'array' reference + // from surviving inside of TestEntryPoint + weak = new WeakReference(array); } - + [MethodImplAttribute(MethodImplOptions.NoInlining)] public static void DestroyArray() { array = null; } - + [Fact] public static int TestEntryPoint() { CreateArray(); - WeakReference weak = new WeakReference(array); - bool ans1 = weak.IsAlive; Console.WriteLine(ans1); - - if(ans1==false) { // GC.Collect() has already occurred..under GCStress - Console.WriteLine("Test for WeakReference.IsAlive passed!"); - return 100; + if (ans1 != true) + { + // This should be impossible; it would indicate that either the array was collected while reachable from + // our static field, or that the WeakReference failed to track the array even though it's still alive. + Console.WriteLine("Test for WeakReference.IsAlive failed!"); + return 2; } - //else, do an expicit collect. + // Release our strong reference (via static field) so that the collector will no longer see array as reachable. DestroyArray(); + // Perform a blocking full collection which will hopefully collect the array. GC.Collect(); - + bool ans2 = weak.IsAlive; Console.WriteLine(ans2); diff --git a/src/tests/issues.targets b/src/tests/issues.targets index 5d91deeba59eb2..954936a1c3bf26 100644 --- a/src/tests/issues.targets +++ b/src/tests/issues.targets @@ -576,9 +576,6 @@ https://github.com/dotnet/runtime/issues/109311 - - https://github.com/dotnet/runtime/issues/109312 - https://github.com/dotnet/runtime/issues/109313 From 904f0c1e7f844469a2be1efd474f206bfda41ab1 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Wed, 23 Jul 2025 17:03:43 -0700 Subject: [PATCH 2/2] Update src/tests/GC/API/WeakReference/IsAlive.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/tests/GC/API/WeakReference/IsAlive.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/GC/API/WeakReference/IsAlive.cs b/src/tests/GC/API/WeakReference/IsAlive.cs index 0fe1589a5b25ed..2aa125cf3a8eac 100644 --- a/src/tests/GC/API/WeakReference/IsAlive.cs +++ b/src/tests/GC/API/WeakReference/IsAlive.cs @@ -16,8 +16,8 @@ public class Test_IsAlive { [MethodImplAttribute(MethodImplOptions.NoInlining)] public static void CreateArray() { array = new int[50]; - // HACK: Create the weak reference inside of CreateArray to prevent a dangling 'array' reference - // from surviving inside of TestEntryPoint + // Create the weak reference inside of CreateArray to prevent a dangling 'array' reference + // from surviving inside of TestEntryPoint. weak = new WeakReference(array); }