-
Notifications
You must be signed in to change notification settings - Fork 703
Refactor async task handling to use WaitAsync for cancellation support #10425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- This makes sure that blocking calls still yield in a timely manner
There was a problem hiding this 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 refactors DcpExecutor to make long-running Task.WhenAll calls cancellable by using WaitAsync and by offloading creation loops to Task.Run. It also removes redundant Task.Yield calls.
- Changed
Task.WhenAll(...).ConfigureAwait(false)
to.WaitAsync(cancellationToken).ConfigureAwait(false)
to respect cancellation. - Wrapped calls to
CreateResourceExecutablesAsyncCore
andCreateContainerAsyncCore
inTask.Run
with the cancellation token. - Removed
await Task.Yield()
as it’s no longer needed.
Comments suppressed due to low confidence (1)
src/Aspire.Hosting/Dcp/DcpExecutor.cs:792
- [nitpick] No unit tests were added to verify cancellation behavior. Consider adding tests that trigger cancellation during
WaitAsync
to ensure tasks are cancelled as expected.
await Task.WhenAll(containersTask, executablesTask).WaitAsync(cancellationToken).ConfigureAwait(false);
} | ||
|
||
return Task.WhenAll(tasks); | ||
return Task.WhenAll(tasks).WaitAsync(cancellationToken); |
Copilot
AI
Jul 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is declared to return Task
but returns a ValueTask
from WaitAsync
. You should await
the WaitAsync
inside the async method (with .ConfigureAwait(false)
) instead of returning it directly.
return Task.WhenAll(tasks).WaitAsync(cancellationToken); | |
return await Task.WhenAll(tasks).WaitAsync(cancellationToken).ConfigureAwait(false); |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great
/backport to release/9.4 |
Started backporting to release/9.4: https://github.com/dotnet/aspire/actions/runs/16303360659 |
Description
If there's a blocking API call in the creation of the service it will block ctrl + c. This is because we assume that the cancellation token will be respected but that is not the case with these blocking APIs. This changes the logic so that WhenAll will yield if the token is fired.
Checklist