Skip to content
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

Switch to cleaner patterns than explicit Task.Run #73471

Merged
merged 37 commits into from
May 14, 2024

Conversation

CyrusNajmabadi
Copy link
Member

No description provided.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner May 14, 2024 17:57
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels May 14, 2024
@@ -106,7 +105,7 @@ public bool ExecuteCommand(TCommandArgs args, CommandExecutionContext context)
if (service == null)
return false;

Contract.ThrowIfNull(document);
Roslyn.Utilities.Contract.ThrowIfNull(document);
Copy link
Member Author

Choose a reason for hiding this comment

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

conflict between us and VSThreading with ConfigureAwait on Task.Yield. so i removed our using, which meant having to qualify this.

@@ -87,43 +88,41 @@ public async Task<(ImmutableArray<IMethodSymbol> symbols, bool isPartialResult)>
try
{
// Find applicable symbols in parallel
using var _1 = ArrayBuilder<Task<ImmutableArray<IMethodSymbol>?>>.GetInstance(out var tasks);
var peReferenceMethodSymbolsTask = ProducerConsumer<IMethodSymbol?>.RunParallelAsync(
Copy link
Member Author

Choose a reason for hiding this comment

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

@genlu for updated pattern.

@@ -156,6 +156,9 @@ internal partial class CodeFixService : ICodeFixService
SortedDictionary<TextSpan, List<DiagnosticData>> spanToDiagnostics,
CancellationToken cancellationToken)
{
// Ensure we yield here so the caller can continue on.
await AwaitExtensions.ConfigureAwait(Task.Yield(), false);
Copy link
Member Author

Choose a reason for hiding this comment

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

we have a lot of usage of both Roslyn.Utilities and VSThreading in this file. so i couldn't comment out the using for either easily. So this was how i got past the ambiguity about which ConfigureAwait to call.

@CyrusNajmabadi CyrusNajmabadi requested a review from ToddGrun May 14, 2024 18:03

return GetDiagnosticData();
return await ProduceProjectDiagnosticsAsync(
[project], static _ => [], includeProjectNonLocalResult: true, cancellationToken).ConfigureAwait(false);
Copy link
Member Author

Choose a reason for hiding this comment

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

note: thsi logic intentionally changed. the old logic said "if you give me a project id not in the solution, i give you all solution diagnostics". t he new logic says "if you give me a project not in the solution, you get back nothing". this mirrors the logic higher up in this file.

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

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

:shipit:

@CyrusNajmabadi CyrusNajmabadi enabled auto-merge May 14, 2024 21:05
@CyrusNajmabadi CyrusNajmabadi merged commit fe5edeb into dotnet:main May 14, 2024
25 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the taskRun branch May 14, 2024 22:38
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone May 14, 2024
@@ -253,6 +252,9 @@ private Task DelayAsync(CancellationToken cancellationToken)
private async Task FindResultsAsync(
IFindUsagesContext findContext, Document document, int position, CancellationToken cancellationToken)
{
// Ensure that we relinquish the thread so that the caller can proceed with their work.
await Task.Yield().ConfigureAwait(false);
Copy link
Member

@sharwell sharwell May 15, 2024

Choose a reason for hiding this comment

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

Another API for this is:

await TaskScheduler.Default.SwitchTo(true);

Copy link
Member

Choose a reason for hiding this comment

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

You can also call ConfigureAwait directly and not rely on it appearing as an extension method.

@@ -110,9 +111,10 @@ public void RequestDiagnosticRefresh()
priorityProvider ??= new DefaultCodeActionRequestPriorityProvider();

// always make sure that analyzer is called on background thread.
return Task.Run(() => analyzer.GetDiagnosticsForSpanAsync(
await TaskScheduler.Default;
Copy link
Member

Choose a reason for hiding this comment

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

📝 This is slightly different from before. If the caller is already on the background thread, it will no longer yield. You can use SwitchTo(true) to force it to yield if that behavior is important.

Copy link
Member Author

Choose a reason for hiding this comment

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

this was intentional. as the comment says, this is just to ensure that we're on the BG. if we're already there, no need to yield.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants