Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Comments

Fix race condition in URL snapshot watch tests#12804

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/fix-failing-url-snapshot-test
Closed

Fix race condition in URL snapshot watch tests#12804
Copilot wants to merge 2 commits intomainfrom
copilot/fix-failing-url-snapshot-test

Conversation

Copy link
Contributor

Copilot AI commented Nov 7, 2025

Description

Two tests in WithUrlsTests were timing out intermittently due to a race between starting the app and watching for URL notifications. The watch task could complete iteration setup after URLs were already published, causing it to never observe the snapshots.

Fixed by adding WaitForResourceAsync to ensure the resource reaches Running state before checking watch results. This guarantees URLs are published and available in the notification stream before the test proceeds.

Changed tests:

  • MultipleUrlsForSingleEndpointAreIncludedInUrlSnapshot
  • EndpointUrlsAreInitiallyInactive

Pattern applied:

await app.StartAsync();

// Ensure URLs published before checking watch task
await app.ResourceNotifications.WaitForResourceAsync(
    servicea.Resource.Name, 
    KnownResourceStates.Running
).DefaultTimeout(TestConstants.LongTimeoutTimeSpan);

await watchTask.DefaultTimeout(TestConstants.LongTimeoutTimeSpan);

This matches the synchronization pattern already used in other tests in the same file (ExpectedNumberOfUrlsForReplicatedResource, UrlsAreInExpectedStateForResourcesGivenTheirLifecycle, UrlsAreMarkedAsInternalDependingOnDisplayLocation).

Fixes #12775

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No (test-only change)
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?
Original prompt

This section details on the original issue you should resolve

<issue_title>Failing test: Aspire.Hosting.Tests.WithUrlsTests.MultipleUrlsForSingleEndpointAreIncludedInUrlSnapshot</issue_title>
<issue_description>## Build Information
Build: https://github.com/dotnet/aspire/actions/runs/18912256311
Build error leg or test failing: Aspire.Hosting.Tests.WithUrlsTests.MultipleUrlsForSingleEndpointAreIncludedInUrlSnapshot
Pull request:

Error Message

Fill the error message using step by step known issues guidance.

{
  "ErrorMessage": "",
  "ErrorPattern": "",
  "BuildRetry": false,
  "ExcludeConsoleLog": false
}

Error Details

Polly.Timeout.TimeoutRejectedException : The operation didn't complete within the allowed timeout of '00:00:10'.
---- System.Threading.Tasks.TaskCanceledException : A task was canceled.
Stack Trace
at Polly.Utils.ExceptionUtilities.TrySetStackTrace[T](T exception)
   at Polly.Timeout.TimeoutResilienceStrategy.ExecuteCore[TResult,TState](Func`3 callback, ResilienceContext context, TState state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.ExecutionContextCallback(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext()
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(IAsyncStateMachineBox box, Boolean allowInlining)
   at System.Threading.Tasks.Task.RunContinuations(Object continuationObject)
   at System.Threading.Tasks.Task`1.TrySetResult(TResult result)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.SetExistingTaskResult(Task`1 task, TResult result)
   at System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.SetResult(TResult result)
   at Polly.ResiliencePipeline.<>c.<<ExecuteAsync>b__3_0>d.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.ExecutionContextCallback(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext()
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(IAsyncStateMachineBox box, Boolean allowInlining)
   at System.Threading.Tasks.Task.RunContinuations(Object continuationObject)
   at System.Threading.Tasks.Task.TrySetCanceled(CancellationToken tokenToRecord, Object cancellationException)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.SetException(Exception exception, Task`1& taskField)
   at Aspire.Hosting.Dcp.DcpExecutor.<>c__DisplayClass62_0.<<CreateServicesAsync>b__1>d.MoveNext() in /_/src/Aspire.Hosting/Dcp/DcpExecutor.cs:line 743
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
   at Aspire.Hosting.Dcp.KubernetesService.WatchAsync[T](String namespaceParameter, CancellationToken cancellationToken)+MoveNext() in /_/src/Aspire.Hosting/Dcp/KubernetesService.cs:line 298
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
   at Aspire.Hosting.Utils.PeriodicRestartAsyncEnumerable.CreateAsync[T](Func`3 enumerableFactory, TimeSpan restartInterval, CancellationToken cancellationToken)+MoveNext() in /_/src/Aspire.Hosting/Utils/PeriodicRestartAsyncEnumerable.cs:line 60
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
   at k8s.Watcher`1.CreateWatchEventEnumerator(Func`1 streamReaderCreator, Action`1 onError, CancellationToken cancellationToken)+MoveNext()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callbac...

</details>

- Fixes dotnet/aspire#12505

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

Copilot AI changed the title [WIP] Fix failing test for multiple URLs in snapshot Fix race condition in URL snapshot watch tests Nov 7, 2025
Copilot AI requested a review from radical November 7, 2025 16:14
@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 2025

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 12804

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 12804"

@radical radical marked this pull request as ready for review November 7, 2025 16:29
@radical radical requested a review from mitchdenny as a code owner November 7, 2025 16:29
Copilot AI review requested due to automatic review settings November 7, 2025 16:29
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 adds explicit waits for resources to reach the Running state in two test methods to address potential race conditions in URL snapshot assertions.

  • Adds WaitForResourceAsync calls to ensure resources reach Running state before URL snapshot verification
  • Applies the fix to EndpointUrlsAreInitiallyInactive and MultipleUrlsForSingleEndpointAreIncludedInUrlSnapshot tests

@radical radical closed this Jan 15, 2026
@dotnet-policy-service dotnet-policy-service bot added this to the 13.2 milestone Jan 15, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Feb 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants