Skip to content

Commit 09f8e28

Browse files
Copilotabonie
andcommitted
Add timeout to flaky MailboxProcessor race condition tests to prevent test host crash
Co-authored-by: abonie <[email protected]>
1 parent 9f4a4f6 commit 09f8e28

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

XUNIT3_BUILD_ISSUES.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,31 @@ All build issues have been resolved. The migration is complete and verified.
3737
- All projects build successfully
3838
- 5,939 tests pass
3939
- No build errors
40-
- One pre-existing flaky test may cause timeout (MailboxProcessorType.TryReceive) but this is not related to xUnit3 migration
40+
41+
## Known Pre-existing Flaky Tests (Not Related to xUnit3 Migration)
42+
43+
### MailboxProcessorType Race Condition Tests
44+
45+
The following tests in `tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/MailboxProcessorType.fs` are known to be flaky and can cause test host process crashes:
46+
47+
- `Receive Races with Post on timeout` (lines 242-280)
48+
- `TryReceive Races with Post on timeout` (lines 283-321)
49+
50+
**Root Cause Analysis:**
51+
1. These tests run tight loops (10,000 iterations) with race conditions between `Receive`/`TryReceive` and `Post`
52+
2. Each iteration uses `finishedEv.WaitOne(100)` with a 100ms timeout
53+
3. The tests use `AutoResetEvent` synchronization primitives that can deadlock under thread pool starvation
54+
4. The `isErrored.IsCompleted` check combined with `raise` can cause unhandled exceptions that crash the test host
55+
56+
**Why This Can Crash Test Host:**
57+
- If a race condition leads to a deadlock, the `while not (finishedEv.WaitOne(100))` loop spins forever
58+
- If `isErrored` completes with an error, the `raise <| Exception(...)` throws an unhandled exception
59+
- Under CI conditions with resource contention, thread pool starvation can cause these patterns to fail
60+
61+
**This is a pre-existing issue** that predates the xUnit3 migration. The tests are intentionally testing race conditions which makes them inherently flaky.
62+
63+
**Potential Fix Options (for future consideration):**
64+
1. Add `[<Fact(Timeout = 60000)>]` to limit test runtime
65+
2. Reduce iteration count from 10,000 to a smaller number
66+
3. Add `[<Fact(Skip = "Flaky race condition test")>]` to skip in CI
67+
4. Refactor to use more deterministic synchronization patterns

tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Control/MailboxProcessorType.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ type MailboxProcessorType() =
203203

204204
Assert.AreEqual(Some("Scanned 1 Disposed"), result)
205205

206-
[<Fact>]
206+
[<Fact(Timeout = 120000)>] // 2 minute timeout to prevent test host crash from race conditions
207207
member this.``Receive Races with Post``() =
208208
let receiveEv = new AutoResetEvent(false)
209209
let postEv = new AutoResetEvent(false)
@@ -238,7 +238,7 @@ type MailboxProcessorType() =
238238
postEv.Set() |> ignore
239239
post.Wait()
240240

241-
[<Fact>]
241+
[<Fact(Timeout = 120000)>] // 2 minute timeout to prevent test host crash from race conditions
242242
member this.``Receive Races with Post on timeout``() =
243243
let receiveEv = new AutoResetEvent(false)
244244
let postEv = new AutoResetEvent(false)
@@ -279,7 +279,7 @@ type MailboxProcessorType() =
279279
postEv.Set() |> ignore
280280
post.Wait()
281281

282-
[<Fact>]
282+
[<Fact(Timeout = 120000)>] // 2 minute timeout to prevent test host crash from race conditions
283283
member this.``TryReceive Races with Post on timeout``() =
284284
let receiveEv = new AutoResetEvent(false)
285285
let postEv = new AutoResetEvent(false)

0 commit comments

Comments
 (0)