From e1660fe1bd4ae10182b001334093ac9b2bc6ffc6 Mon Sep 17 00:00:00 2001 From: Jakub Majocha <1760221+majocha@users.noreply.github.com> Date: Tue, 25 Mar 2025 11:47:04 +0100 Subject: [PATCH] Fix GC test that is flaky on Linux --- .../ProjectAnalysisTests.fs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs b/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs index 71bffac7974..0e12a33fe2f 100644 --- a/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs +++ b/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs @@ -1,5 +1,7 @@ module FSharp.Compiler.Service.Tests.ProjectAnalysisTests +open System.Threading.Tasks + #nowarn "57" // Experimental stuff let runningOnMono = try System.Type.GetType("Mono.Runtime") <> null with e -> false @@ -128,8 +130,20 @@ module ClearLanguageServiceRootCachesTest = let weakTcImports = test () checker.InvalidateConfiguration Project1.options checker.ClearLanguageServiceRootCachesAndCollectAndFinalizeAllTransients() - GC.Collect() - System.Threading.SpinWait.SpinUntil(fun () -> not weakTcImports.IsAlive) + + task { + GC.Collect() + GC.WaitForPendingFinalizers() + // Try collecting many times, because GC has some problems, especially on Linux. + // See for example: https://github.com/dotnet/runtime/discussions/108081 + let mutable attempt = 1 + while weakTcImports.IsAlive && attempt < 10 do + GC.Collect() + GC.WaitForPendingFinalizers() + attempt <- attempt + 1 + do! Task.Delay(attempt * 1000) + Assert.False weakTcImports.IsAlive + } [] let ``Test Project1 should have protected FullName and TryFullName return same results`` () =