Skip to content

Conversation

@thomhurst
Copy link
Owner

Summary

  • Fix flaky Test_Thread_IsNotThreadPoolThread_NewThread test that was failing with ThreadStateException: Thread is dead; state cannot be accessed
  • Replace Thread.Sleep() with ManualResetEventSlim synchronization in Test_Thread_IsAlive_NewThread, Test_Thread_IsBackground_Started, and Test_Thread_IsNotThreadPoolThread_NewThread
  • Remove duplicate using TUnit.Assertions.Extensions; import

Test plan

  • Verify the thread assertion tests pass on CI (macos-latest and windows-latest)
  • Verify no regressions in other tests

🤖 Generated with Claude Code

Replace Thread.Sleep() with ManualResetEventSlim synchronization to
ensure threads stay alive until assertions complete. This fixes flaky
test failures where ThreadStateException was thrown because the thread
completed before IsThreadPoolThread could be accessed.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@thomhurst
Copy link
Owner Author

Summary

Fixes flaky thread assertion tests by replacing timing-based synchronization (Thread.Sleep) with deterministic event-based synchronization (ManualResetEventSlim).

Critical Issues

None found ✅

The changes correctly address the race condition that was causing ThreadStateException: Thread is dead; state cannot be accessed. The new pattern ensures:

  • Threads are guaranteed to be running before assertions via startSignal.Wait()
  • Threads cleanly exit via completeSignal.Set() in finally blocks
  • No timing dependencies that could cause flakiness

Suggestions

None - the implementation is clean and follows proper synchronization patterns.

Verdict

APPROVE - No critical issues

The PR fixes a legitimate test flakiness issue with proper synchronization primitives. The duplicate import removal is a nice cleanup bonus.

@thomhurst thomhurst enabled auto-merge (squash) January 27, 2026 00:49
@thomhurst thomhurst merged commit e59bf65 into main Jan 27, 2026
13 checks passed
@thomhurst thomhurst deleted the fix/flaky-thread-assertion-tests branch January 27, 2026 01:17
This was referenced Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants