Skip to content

DebounceDispatcher: Fix race condition causing flaky test#12334

Merged
ScarletKuro merged 2 commits intodevfrom
copilot/fix-flaky-debounce-test
Dec 21, 2025
Merged

DebounceDispatcher: Fix race condition causing flaky test#12334
ScarletKuro merged 2 commits intodevfrom
copilot/fix-flaky-debounce-test

Conversation

Copy link
Contributor

Copilot AI commented Dec 21, 2025

The DebounceAsync_LeadingMode_DebounceSubsequentCalls test was flaky, failing intermittently with execution count of 3 instead of expected 2.

Root Cause

Race condition in DebounceDispatcher.cs: the CancellationTokenSource was captured outside the lock, allowing multiple concurrent calls to capture the same instance.

// Before - race condition
finally { _lock.Release(); }
var localCts = _cancellationTokenSource;  // ← captured after lock release

// After - fixed
CancellationTokenSource? localCts = null;
// ... inside lock:
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
localCts = _cancellationTokenSource;  // ← captured before lock release
finally { _lock.Release(); }

Changes

Implementation (DebounceDispatcher.cs)

  • Declare localCts at method scope
  • Capture CTS reference inside lock before release

Test (DebounceDispatcherTests.cs)

  • Store task references and explicitly await them instead of fire-and-forget with Task.Delay(150)
  • Eliminates timing assumptions, ensures proper synchronization
Original prompt

The Test "DebounceAsync_LeadingMode_DebounceSubsequentCalls" is flaky


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix flakiness in debounce async test Fix race condition in DebounceDispatcher causing flaky test Dec 21, 2025
Copilot AI requested a review from ScarletKuro December 21, 2025 17:43
@ScarletKuro ScarletKuro requested a review from Copilot December 21, 2025 17:57
@ScarletKuro ScarletKuro marked this pull request as ready for review December 21, 2025 18:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a race condition in DebounceDispatcher that caused the DebounceAsync_LeadingMode_DebounceSubsequentCalls test to fail intermittently with incorrect execution counts.

Key Changes

  • Race condition fix: Moved CancellationTokenSource capture inside the lock to prevent concurrent calls from capturing the same instance
  • Test improvement: Changed from fire-and-forget pattern with Task.Delay to explicitly storing and awaiting task references for proper synchronization

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/MudBlazor/Utilities/Debounce/DebounceDispatcher.cs Declares localCts at method scope and captures the CancellationTokenSource reference inside the lock before release, preventing race conditions where multiple concurrent calls could capture the same instance
src/MudBlazor.UnitTests/Utilities/Debounce/DebounceDispatcherTests.cs Stores task references from concurrent DebounceAsync calls and explicitly awaits them, replacing the fire-and-forget pattern with Task.Delay to eliminate timing assumptions and ensure proper test synchronization

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mudbot mudbot bot added the bug Unexpected behavior or functionality not working as intended label Dec 21, 2025
@ScarletKuro ScarletKuro merged commit 70d4d27 into dev Dec 21, 2025
15 checks passed
@ScarletKuro ScarletKuro deleted the copilot/fix-flaky-debounce-test branch December 21, 2025 18:04
@danielchalmers danielchalmers changed the title Fix race condition in DebounceDispatcher causing flaky test DebounceDispatcher: Fix race condition causing flaky test Dec 21, 2025
This was referenced Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants