Skip to content

Fix race condition in AssetCachingTests and re-enable test#123167

Closed
Copilot wants to merge 5 commits intomainfrom
copilot/fix-race-condition-in-assetcachingtests
Closed

Fix race condition in AssetCachingTests and re-enable test#123167
Copilot wants to merge 5 commits intomainfrom
copilot/fix-race-condition-in-assetcachingtests

Conversation

Copy link
Contributor

Copilot AI commented Jan 14, 2026

Description

The BlazorApp_BasedOnFingerprinting_LoadsWasmAssetsFromCache test reassigned counterLoaded inside the Test lambda after OnConsoleMessage captured it in a closure, causing the handler to potentially signal the wrong task.

Fixed by:

  • Split single counterLoaded into firstCounterLoaded and secondCounterLoaded (no reassignment)
  • Added thread-safe loadCount counter using Interlocked.Increment
  • Handler now checks count: 1 completes first task, 2 completes second task
  • Removed [ActiveIssue] attribute to re-enable the test
// Before: Race condition from reassignment
var counterLoaded = new TaskCompletionSource();
OnConsoleMessage = (type, msg) => {
    if (msg.Contains("Counter.OnAfterRender"))
        counterLoaded.SetResult();  // Captures counterLoaded
},
Test = async (page) => {
    await counterLoaded.Task;
    counterLoaded = new();  // ⚠️ Reassignment breaks closure
    await counterLoaded.Task;
}

// After: Separate tasks, no reassignment
var firstCounterLoaded = new TaskCompletionSource();
var secondCounterLoaded = new TaskCompletionSource();
var loadCount = 0;
OnConsoleMessage = (type, msg) => {
    if (msg.Contains("Counter.OnAfterRender")) {
        var count = Interlocked.Increment(ref loadCount);
        if (count == 1) firstCounterLoaded.SetResult();
        else if (count == 2) secondCounterLoaded.SetResult();
    }
},
Test = async (page) => {
    await firstCounterLoaded.Task;
    await page.ReloadAsync();
    await secondCounterLoaded.Task;
}

Customer Impact

Intermittent test failures blocking CI/CD pipelines. No customer-facing runtime impact.

Regression

No. Pre-existing concurrency bug in test infrastructure.

Testing

Code review verified closure semantics and thread-safety. Test is now re-enabled and will run in CI to validate stability.

Risk

Low. Test-only change, 7 net lines added. Logic simplified by eliminating variable reassignment. Test re-enabled after fixing race condition.

Original prompt

The AssetCachingTests.BlazorApp_BasedOnFingerprinting_LoadsWasmAssetsFromCache test currently reassigns the counterLoaded TaskCompletionSource variable inside its Test lambda, which may cause a race condition: multiple invocations can capture the wrong task in the closure, leading to failures like:

at System.Collections.Generic.List`1.Enumerator.MoveNext()
at Wasm.Build.Tests.Blazor.AssetCachingTests.<>c__DisplayClass1_0.<<BlazorApp_BasedOnFingerprinting_LoadsWasmAssetsFromCache>b__1>d.MoveNext() in /_/src/mono/wasm/Wasm.Build.Tests/Blazor/AssetCachingTests.cs:line 76
...

This problem is due to re-initializing counterLoaded while closures to the previous instance are still in use in the OnConsoleMessage lambda. The fix is to:

  • Declare counterLoaded at a broader scope, accessible to both lambdas.
  • Before each awaited navigation, assign a new TaskCompletionSource and ensure OnConsoleMessage sets the result for the currently-scoped object.

This eliminates the chance that OnConsoleMessage is signaling a stale or wrong TCS instance and resolves the race condition and intermittent failures likely caused by this bug.

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Jan 14, 2026
Copilot AI and others added 3 commits January 14, 2026 03:26
…ionSource variables

Co-authored-by: lewing <24063+lewing@users.noreply.github.com>
Co-authored-by: lewing <24063+lewing@users.noreply.github.com>
Co-authored-by: lewing <24063+lewing@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix race condition in AssetCachingTests for Wasm asset loading Fix race condition in AssetCachingTests from TaskCompletionSource reassignment Jan 14, 2026
Copilot AI requested a review from lewing January 14, 2026 03:31
@lewing
Copy link
Member

lewing commented Jan 14, 2026

interestingly

[ActiveIssue("https://github.com/dotnet/runtime/issues/122338")] // add it back to eng\testing\scenarios\BuildWasmAppsJobsList.txt

Co-authored-by: lewing <24063+lewing@users.noreply.github.com>
Copilot AI changed the title Fix race condition in AssetCachingTests from TaskCompletionSource reassignment Fix race condition in AssetCachingTests and re-enable test Jan 14, 2026
Copilot AI requested a review from lewing January 14, 2026 05:02
@lewing lewing changed the base branch from main to release/10.0 January 14, 2026 05:04
@lewing lewing changed the base branch from release/10.0 to main January 14, 2026 05:05
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jan 14, 2026
@lewing lewing removed the linkable-framework Issues associated with delivering a linker friendly framework label Jan 14, 2026
@lewing lewing closed this Jan 14, 2026
@jkotas jkotas deleted the copilot/fix-race-condition-in-assetcachingtests branch January 24, 2026 03:48
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