diff --git a/src/csharp/Grpc.Core/Channel.cs b/src/csharp/Grpc.Core/Channel.cs index f5eec969f5e..ec60354639e 100644 --- a/src/csharp/Grpc.Core/Channel.cs +++ b/src/csharp/Grpc.Core/Channel.cs @@ -173,7 +173,7 @@ namespace Grpc.Core { throw new OperationCanceledException("Channel has reached FatalFailure state."); } - await WaitForStateChangedAsync(currentState, deadline); + await WaitForStateChangedAsync(currentState, deadline).ConfigureAwait(false); currentState = handle.CheckConnectivityState(false); } } @@ -198,7 +198,7 @@ namespace Grpc.Core handle.Dispose(); - await Task.Run(() => GrpcEnvironment.Release()); + await Task.Run(() => GrpcEnvironment.Release()).ConfigureAwait(false); } internal ChannelSafeHandle Handle diff --git a/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs b/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs index b4a7335c7ce..d6e34a0f044 100644 --- a/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs +++ b/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs @@ -70,12 +70,12 @@ namespace Grpc.Core.Internal } var taskSource = new AsyncCompletionTaskSource(); call.StartReadMessage(taskSource.CompletionDelegate); - var result = await taskSource.Task; + var result = await taskSource.Task.ConfigureAwait(false); this.current = result; if (result == null) { - await call.StreamingCallFinishedTask; + await call.StreamingCallFinishedTask.ConfigureAwait(false); return false; } return true; diff --git a/src/csharp/Grpc.Core/Internal/NativeMetadataCredentialsPlugin.cs b/src/csharp/Grpc.Core/Internal/NativeMetadataCredentialsPlugin.cs index 2a571cd6c98..4c086048d26 100644 --- a/src/csharp/Grpc.Core/Internal/NativeMetadataCredentialsPlugin.cs +++ b/src/csharp/Grpc.Core/Internal/NativeMetadataCredentialsPlugin.cs @@ -96,7 +96,7 @@ namespace Grpc.Core.Internal try { var metadata = new Metadata(); - await interceptor(serviceUrl, metadata); + await interceptor(serviceUrl, metadata).ConfigureAwait(false); using (var metadataArray = MetadataArraySafeHandle.Create(metadata)) { diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs index 59f4c5727c5..0ade701a538 100644 --- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs +++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs @@ -78,13 +78,13 @@ namespace Grpc.Core.Internal var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken); try { - Preconditions.CheckArgument(await requestStream.MoveNext()); + Preconditions.CheckArgument(await requestStream.MoveNext().ConfigureAwait(false)); var request = requestStream.Current; // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. - Preconditions.CheckArgument(!await requestStream.MoveNext()); - var result = await handler(request, context); + Preconditions.CheckArgument(!await requestStream.MoveNext().ConfigureAwait(false)); + var result = await handler(request, context).ConfigureAwait(false); status = context.Status; - await responseStream.WriteAsync(result); + await responseStream.WriteAsync(result).ConfigureAwait(false); } catch (Exception e) { @@ -93,13 +93,13 @@ namespace Grpc.Core.Internal } try { - await responseStream.WriteStatusAsync(status, context.ResponseTrailers); + await responseStream.WriteStatusAsync(status, context.ResponseTrailers).ConfigureAwait(false); } catch (OperationCanceledException) { // Call has been already cancelled. } - await finishedTask; + await finishedTask.ConfigureAwait(false); } } @@ -134,11 +134,11 @@ namespace Grpc.Core.Internal var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken); try { - Preconditions.CheckArgument(await requestStream.MoveNext()); + Preconditions.CheckArgument(await requestStream.MoveNext().ConfigureAwait(false)); var request = requestStream.Current; // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. - Preconditions.CheckArgument(!await requestStream.MoveNext()); - await handler(request, responseStream, context); + Preconditions.CheckArgument(!await requestStream.MoveNext().ConfigureAwait(false)); + await handler(request, responseStream, context).ConfigureAwait(false); status = context.Status; } catch (Exception e) @@ -149,13 +149,13 @@ namespace Grpc.Core.Internal try { - await responseStream.WriteStatusAsync(status, context.ResponseTrailers); + await responseStream.WriteStatusAsync(status, context.ResponseTrailers).ConfigureAwait(false); } catch (OperationCanceledException) { // Call has been already cancelled. } - await finishedTask; + await finishedTask.ConfigureAwait(false); } } @@ -190,11 +190,11 @@ namespace Grpc.Core.Internal var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken); try { - var result = await handler(requestStream, context); + var result = await handler(requestStream, context).ConfigureAwait(false); status = context.Status; try { - await responseStream.WriteAsync(result); + await responseStream.WriteAsync(result).ConfigureAwait(false); } catch (OperationCanceledException) { @@ -209,13 +209,13 @@ namespace Grpc.Core.Internal try { - await responseStream.WriteStatusAsync(status, context.ResponseTrailers); + await responseStream.WriteStatusAsync(status, context.ResponseTrailers).ConfigureAwait(false); } catch (OperationCanceledException) { // Call has been already cancelled. } - await finishedTask; + await finishedTask.ConfigureAwait(false); } } @@ -250,7 +250,7 @@ namespace Grpc.Core.Internal var context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken); try { - await handler(requestStream, responseStream, context); + await handler(requestStream, responseStream, context).ConfigureAwait(false); status = context.Status; } catch (Exception e) @@ -260,13 +260,13 @@ namespace Grpc.Core.Internal } try { - await responseStream.WriteStatusAsync(status, context.ResponseTrailers); + await responseStream.WriteStatusAsync(status, context.ResponseTrailers).ConfigureAwait(false); } catch (OperationCanceledException) { // Call has been already cancelled. } - await finishedTask; + await finishedTask.ConfigureAwait(false); } } @@ -284,8 +284,8 @@ namespace Grpc.Core.Internal var finishedTask = asyncCall.ServerSideCallAsync(); var responseStream = new ServerResponseStream(asyncCall); - await responseStream.WriteStatusAsync(new Status(StatusCode.Unimplemented, "No such method."), Metadata.Empty); - await finishedTask; + await responseStream.WriteStatusAsync(new Status(StatusCode.Unimplemented, "No such method."), Metadata.Empty).ConfigureAwait(false); + await finishedTask.ConfigureAwait(false); } } diff --git a/src/csharp/Grpc.Core/Internal/ServerRequestStream.cs b/src/csharp/Grpc.Core/Internal/ServerRequestStream.cs index 3fccb88abba..e7be82c3185 100644 --- a/src/csharp/Grpc.Core/Internal/ServerRequestStream.cs +++ b/src/csharp/Grpc.Core/Internal/ServerRequestStream.cs @@ -70,7 +70,7 @@ namespace Grpc.Core.Internal } var taskSource = new AsyncCompletionTaskSource(); call.StartReadMessage(taskSource.CompletionDelegate); - var result = await taskSource.Task; + var result = await taskSource.Task.ConfigureAwait(false); this.current = result; return result != null; } diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index 7c94d215618..0fadabe554c 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -148,10 +148,10 @@ namespace Grpc.Core } handle.ShutdownAndNotify(HandleServerShutdown, environment); - await shutdownTcs.Task; + await shutdownTcs.Task.ConfigureAwait(false); DisposeHandle(); - await Task.Run(() => GrpcEnvironment.Release()); + await Task.Run(() => GrpcEnvironment.Release()).ConfigureAwait(false); } /// @@ -169,7 +169,7 @@ namespace Grpc.Core handle.ShutdownAndNotify(HandleServerShutdown, environment); handle.CancelAllCalls(); - await shutdownTcs.Task; + await shutdownTcs.Task.ConfigureAwait(false); DisposeHandle(); } @@ -268,7 +268,7 @@ namespace Grpc.Core { callHandler = NoSuchMethodCallHandler.Instance; } - await callHandler.HandleCall(newRpc, environment); + await callHandler.HandleCall(newRpc, environment).ConfigureAwait(false); } catch (Exception e) { @@ -288,7 +288,7 @@ namespace Grpc.Core // after server shutdown, the callback returns with null call if (!newRpc.Call.IsInvalid) { - Task.Run(async () => await HandleCallAsync(newRpc)); + Task.Run(async () => await HandleCallAsync(newRpc)).ConfigureAwait(false); } } diff --git a/src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs b/src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs index cdf1e510261..02a47568e79 100644 --- a/src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs +++ b/src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs @@ -48,9 +48,9 @@ namespace Grpc.Core.Utils public static async Task ForEachAsync(this IAsyncStreamReader streamReader, Func asyncAction) where T : class { - while (await streamReader.MoveNext()) + while (await streamReader.MoveNext().ConfigureAwait(false)) { - await asyncAction(streamReader.Current); + await asyncAction(streamReader.Current).ConfigureAwait(false); } } @@ -61,7 +61,7 @@ namespace Grpc.Core.Utils where T : class { var result = new List(); - while (await streamReader.MoveNext()) + while (await streamReader.MoveNext().ConfigureAwait(false)) { result.Add(streamReader.Current); } @@ -77,11 +77,11 @@ namespace Grpc.Core.Utils { foreach (var element in elements) { - await streamWriter.WriteAsync(element); + await streamWriter.WriteAsync(element).ConfigureAwait(false); } if (complete) { - await streamWriter.CompleteAsync(); + await streamWriter.CompleteAsync().ConfigureAwait(false); } } @@ -93,7 +93,7 @@ namespace Grpc.Core.Utils { foreach (var element in elements) { - await streamWriter.WriteAsync(element); + await streamWriter.WriteAsync(element).ConfigureAwait(false); } } }