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 task yielding pattern #73514

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ 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);
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

using (Logger.LogBlock(FunctionId, KeyValueLogMessage.Create(LogType.UserAction), cancellationToken))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.VisualStudio.Threading;
using Nerdbank.Streams;
using Roslyn.Utilities;
using StreamJsonRpc;
Expand All @@ -11,7 +12,7 @@ namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests;
/// <summary>
/// A wrapper which takes a service but actually sends calls to it through JsonRpc to ensure we can actually use the service across a wire.
/// </summary>
internal sealed class BrokeredServiceProxy<T> : IAsyncDisposable where T : class
internal sealed class BrokeredServiceProxy<T> : System.IAsyncDisposable where T : class
{
/// <summary>
/// A task that cane awaited to assert the rest of the fields in this class being assigned and non-null.
Expand All @@ -31,8 +32,8 @@ public BrokeredServiceProxy(T service)

async Task CreateServerAsync()
{
// Ensure caller can proceed.
await Task.Yield().ConfigureAwait(false);
// Always yield to ensure caller can proceed.
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

var serverMultiplexingStream = await MultiplexingStream.CreateAsync(serverStream);
var serverChannel = await serverMultiplexingStream.AcceptChannelAsync("");
Expand All @@ -46,8 +47,8 @@ async Task CreateServerAsync()

async Task CreateClientAsync()
{
// Ensure caller can proceed.
await Task.Yield().ConfigureAwait(false);
// Always yield to ensure caller can proceed.
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

var clientMultiplexingStream = await MultiplexingStream.CreateAsync(clientStream);
var clientChannel = await clientMultiplexingStream.OfferChannelAsync("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public async Task<FirstFixResult> GetMostSevereFixAsync(
CancellationToken cancellationToken)
{
// Ensure we yield here so the caller can continue on.
await AwaitExtensions.ConfigureAwait(Task.Yield(), false);
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

await foreach (var collection in StreamFixesAsync(
document, spanToDiagnostics, fixAllForInSpan: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ private async Task PerformSearchWorkerAsync(
ISearchCallback searchCallback,
CancellationToken cancellationToken)
{
// Ensure we yield immedaitely so our caller can proceed with other work.
await Task.Yield().ConfigureAwait(false);
// Ensure we yield immediately so our caller can proceed with other work.
await TaskScheduler.Default.SwitchTo(alwaysYield: true);

var searchValue = searchQuery.QueryString.Trim();
if (string.IsNullOrWhiteSpace(searchValue))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ static async Task WaitForHighPriorityTasksAsync(CancellationToken cancellationTo
if (task.IsCompleted)
{
// Make sure to yield so continuations of 'task' can make progress.
await AwaitExtensions.ConfigureAwait(Task.Yield(), false);
await TaskScheduler.Default.SwitchTo(alwaysYield: true);
}
else
{
Expand Down
Loading