Skip to content

Commit 31fdf7b

Browse files
authored
Fix GC test that is flaky on Linux (#18408)
1 parent a305ccd commit 31fdf7b

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module FSharp.Compiler.Service.Tests.ProjectAnalysisTests
22

3+
open System.Threading.Tasks
4+
35
#nowarn "57" // Experimental stuff
46

57
let runningOnMono = try System.Type.GetType("Mono.Runtime") <> null with e -> false
@@ -128,8 +130,20 @@ module ClearLanguageServiceRootCachesTest =
128130
let weakTcImports = test ()
129131
checker.InvalidateConfiguration Project1.options
130132
checker.ClearLanguageServiceRootCachesAndCollectAndFinalizeAllTransients()
131-
GC.Collect()
132-
System.Threading.SpinWait.SpinUntil(fun () -> not weakTcImports.IsAlive)
133+
134+
task {
135+
GC.Collect()
136+
GC.WaitForPendingFinalizers()
137+
// Try collecting many times, because GC has some problems, especially on Linux.
138+
// See for example: https://github.com/dotnet/runtime/discussions/108081
139+
let mutable attempt = 1
140+
while weakTcImports.IsAlive && attempt < 10 do
141+
GC.Collect()
142+
GC.WaitForPendingFinalizers()
143+
attempt <- attempt + 1
144+
do! Task.Delay(attempt * 1000)
145+
Assert.False weakTcImports.IsAlive
146+
}
133147

134148
[<Fact>]
135149
let ``Test Project1 should have protected FullName and TryFullName return same results`` () =

0 commit comments

Comments
 (0)