Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: make thread assertion tests deterministic - #4570

#4570
Merged
thomhurst merged 1 commit into
mainthomhurst/TUnit:mainfrom
fix/flaky-thread-assertion-teststhomhurst/TUnit:fix/flaky-thread-assertion-testsCopy head branch name to clipboard
Jan 27, 2026
Merged

fix: make thread assertion tests deterministic#4570
thomhurst merged 1 commit into
mainthomhurst/TUnit:mainfrom
fix/flaky-thread-assertion-teststhomhurst/TUnit:fix/flaky-thread-assertion-testsCopy head branch name to clipboard

Conversation

@thomhurst

Copy link
Copy Markdown
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 <noreply@anthropic.com>
@thomhurst

Copy link
Copy Markdown
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.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.