From 7f34be5319af78de3cb5fe12438f2756a864bddd Mon Sep 17 00:00:00 2001
From: Carlos Sanchez Lopez <1175054+carlossanlop@users.noreply.github.com>
Date: Wed, 15 Jul 2020 13:10:46 -0700
Subject: [PATCH] Apply CA2016: Forward CancellationToken to methods that can
take it (#37607)
* Forward CancellationToken to methods that can take it
* Address suggestions
* address suggestions
* Revert using
* Make rule 2016 a warning in runtime
* Extra cases found
* Revert changes that should not pass a token, change rule to info to verify build and test CI results
* Pass CancellationToken.None to base.CreateContentReadStreamAsync, as in its default overload
* Address stack overflow in MultipartContent
* HostingAbstractionsHostExtensions address using
* Update nuget package version to latest, Info->Warning again, fix one more case.
* System.Diagnostics.ProcessWaitState.Unix token
* Remove two unnecessary tokens
---
eng/Analyzers.props | 2 +-
eng/CodeAnalysis.ruleset | 2 +-
.../Quic/Implementations/Mock/MockConnection.cs | 4 ++--
.../Quic/Implementations/Mock/MockListener.cs | 2 +-
.../src/System/Net/WebSockets/ManagedWebSocket.cs | 2 +-
.../src/HostingAbstractionsHostExtensions.cs | 8 ++++---
.../Collections/Concurrent/BlockingCollection.cs | 2 +-
.../System/Diagnostics/ProcessWaitState.Unix.cs | 4 ++--
.../IO/Compression/dec/BrotliStream.Decompress.cs | 3 ++-
.../IsolatedStorage/IsolatedStorageFileStream.cs | 2 +-
.../src/System.Net.Http.Json.csproj | 2 ++
.../Net/Http/Json/HttpContentJsonExtensions.cs | 18 +++++-----------
.../Json/HttpContentJsonExtensions.netcoreapp.cs | 24 +++++++++++++++++++++
.../Json/HttpContentJsonExtensions.netstandard.cs | 25 ++++++++++++++++++++++
.../Http/BrowserHttpHandler/BrowserHttpHandler.cs | 4 ++--
.../System/Net/Http/MessageProcessingHandler.cs | 2 +-
.../src/System/Net/Http/MultipartContent.cs | 4 +++-
.../SocketsHttpHandler/DecompressionHandler.cs | 3 ++-
.../Http/SocketsHttpHandler/Http3RequestStream.cs | 2 +-
.../System/Net/Windows/WebSockets/WebSocketBase.cs | 2 +-
.../src/System/IO/FileStream.Unix.cs | 2 +-
.../Channels/SingleConsumerUnboundedChannel.cs | 4 ++--
22 files changed, 86 insertions(+), 37 deletions(-)
create mode 100644 src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.netcoreapp.cs
create mode 100644 src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.netstandard.cs
diff --git a/eng/Analyzers.props b/eng/Analyzers.props
index 8d2a67c..2c9b1c8 100644
--- a/eng/Analyzers.props
+++ b/eng/Analyzers.props
@@ -6,7 +6,7 @@
-
+
diff --git a/eng/CodeAnalysis.ruleset b/eng/CodeAnalysis.ruleset
index e88f8fc..42ea385 100644
--- a/eng/CodeAnalysis.ruleset
+++ b/eng/CodeAnalysis.ruleset
@@ -122,7 +122,7 @@
-
+
diff --git a/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Implementations/Mock/MockConnection.cs b/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Implementations/Mock/MockConnection.cs
index 361474b..cba2f93 100644
--- a/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Implementations/Mock/MockConnection.cs
+++ b/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Implementations/Mock/MockConnection.cs
@@ -93,7 +93,7 @@ namespace System.Net.Quic.Implementations.Mock
int bytesRead = 0;
do
{
- bytesRead += await socket.ReceiveAsync(buffer.AsMemory().Slice(bytesRead), SocketFlags.None).ConfigureAwait(false);
+ bytesRead += await socket.ReceiveAsync(buffer.AsMemory().Slice(bytesRead), SocketFlags.None, cancellationToken).ConfigureAwait(false);
} while (bytesRead != buffer.Length);
int peerListenPort = BinaryPrimitives.ReadInt32LittleEndian(buffer);
@@ -163,7 +163,7 @@ namespace System.Net.Quic.Implementations.Mock
int bytesRead = 0;
do
{
- bytesRead += await socket.ReceiveAsync(buffer.AsMemory().Slice(bytesRead), SocketFlags.None).ConfigureAwait(false);
+ bytesRead += await socket.ReceiveAsync(buffer.AsMemory().Slice(bytesRead), SocketFlags.None, cancellationToken).ConfigureAwait(false);
} while (bytesRead != buffer.Length);
long streamId = BinaryPrimitives.ReadInt64LittleEndian(buffer);
diff --git a/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Implementations/Mock/MockListener.cs b/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Implementations/Mock/MockListener.cs
index f4c0cfd..e7b2454 100644
--- a/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Implementations/Mock/MockListener.cs
+++ b/src/libraries/Common/src/System/Net/Http/aspnetcore/Quic/Implementations/Mock/MockListener.cs
@@ -45,7 +45,7 @@ namespace System.Net.Quic.Implementations.Mock
int bytesRead = 0;
do
{
- bytesRead += await socket.ReceiveAsync(buffer.AsMemory().Slice(bytesRead), SocketFlags.None).ConfigureAwait(false);
+ bytesRead += await socket.ReceiveAsync(buffer.AsMemory().Slice(bytesRead), SocketFlags.None, cancellationToken).ConfigureAwait(false);
} while (bytesRead != buffer.Length);
int peerListenPort = BinaryPrimitives.ReadInt32LittleEndian(buffer);
diff --git a/src/libraries/Common/src/System/Net/WebSockets/ManagedWebSocket.cs b/src/libraries/Common/src/System/Net/WebSockets/ManagedWebSocket.cs
index e44b306..77e89ea 100644
--- a/src/libraries/Common/src/System/Net/WebSockets/ManagedWebSocket.cs
+++ b/src/libraries/Common/src/System/Net/WebSockets/ManagedWebSocket.cs
@@ -368,7 +368,7 @@ namespace System.Net.WebSockets
// pass around (the CancellationTokenRegistration), so if it is cancelable, just immediately go to the fallback path.
// Similarly, it should be rare that there are multiple outstanding calls to SendFrameAsync, but if there are, again
// fall back to the fallback path.
- return cancellationToken.CanBeCanceled || !_sendFrameAsyncLock.Wait(0) ?
+ return cancellationToken.CanBeCanceled || !_sendFrameAsyncLock.Wait(0, default) ?
SendFrameFallbackAsync(opcode, endOfMessage, payloadBuffer, cancellationToken) :
SendFrameLockAcquiredNonCancelableAsync(opcode, endOfMessage, payloadBuffer);
}
diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs
index ac0368b..f97c652 100644
--- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs
+++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs
@@ -26,9 +26,10 @@ namespace Microsoft.Extensions.Hosting
/// The timeout for stopping gracefully. Once expired the
/// server may terminate any remaining active connections.
/// The that represents the asynchronous operation.
- public static Task StopAsync(this IHost host, TimeSpan timeout)
+ public static async Task StopAsync(this IHost host, TimeSpan timeout)
{
- return host.StopAsync(new CancellationTokenSource(timeout).Token);
+ using CancellationTokenSource cts = new CancellationTokenSource(timeout);
+ await host.StopAsync(cts.Token).ConfigureAwait(false);
}
///
@@ -103,7 +104,8 @@ namespace Microsoft.Extensions.Hosting
await waitForStop.Task.ConfigureAwait(false);
// Host will use its default ShutdownTimeout if none is specified.
- await host.StopAsync().ConfigureAwait(false);
+ // The cancellation token may have been triggered to unblock waitForStop. Don't pass it here because that would trigger an abortive shutdown.
+ await host.StopAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
diff --git a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/BlockingCollection.cs b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/BlockingCollection.cs
index 7e6b08c..ec80b76 100644
--- a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/BlockingCollection.cs
+++ b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/BlockingCollection.cs
@@ -418,7 +418,7 @@ namespace System.Collections.Concurrent
CancellationTokenSource? linkedTokenSource = null;
try
{
- waitForSemaphoreWasSuccessful = _freeNodes.Wait(0);
+ waitForSemaphoreWasSuccessful = _freeNodes.Wait(0, default);
if (waitForSemaphoreWasSuccessful == false && millisecondsTimeout != 0)
{
linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessWaitState.Unix.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessWaitState.Unix.cs
index 1224636..03e93a7 100644
--- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessWaitState.Unix.cs
+++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessWaitState.Unix.cs
@@ -500,7 +500,7 @@ namespace System.Diagnostics
/// Spawns an asynchronous polling loop for process completion.
/// A token to monitor to exit the polling loop.
/// The task representing the loop.
- private Task WaitForExitAsync(CancellationToken cancellationToken = default(CancellationToken))
+ private Task WaitForExitAsync(CancellationToken cancellationToken = default)
{
Debug.Assert(Monitor.IsEntered(_gate));
Debug.Assert(_waitInProgress == null);
@@ -549,7 +549,7 @@ namespace System.Diagnostics
_waitInProgress = null;
}
}
- });
+ }, cancellationToken);
}
private bool TryReapChild()
diff --git a/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs b/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs
index 8b995f4..b5c86a0 100644
--- a/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs
+++ b/src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs
@@ -133,7 +133,8 @@ namespace System.IO.Compression
_bufferOffset = 0;
int numRead = 0;
- while (_bufferCount < _buffer.Length && ((numRead = await _stream.ReadAsync(new Memory(_buffer, _bufferCount, _buffer.Length - _bufferCount)).ConfigureAwait(false)) > 0))
+ while (_bufferCount < _buffer.Length &&
+ ((numRead = await _stream.ReadAsync(new Memory(_buffer, _bufferCount, _buffer.Length - _bufferCount), cancellationToken).ConfigureAwait(false)) > 0))
{
_bufferCount += numRead;
if (_bufferCount > _buffer.Length)
diff --git a/src/libraries/System.IO.IsolatedStorage/src/System/IO/IsolatedStorage/IsolatedStorageFileStream.cs b/src/libraries/System.IO.IsolatedStorage/src/System/IO/IsolatedStorage/IsolatedStorageFileStream.cs
index 7311503..54f0f83 100644
--- a/src/libraries/System.IO.IsolatedStorage/src/System/IO/IsolatedStorage/IsolatedStorageFileStream.cs
+++ b/src/libraries/System.IO.IsolatedStorage/src/System/IO/IsolatedStorage/IsolatedStorageFileStream.cs
@@ -236,7 +236,7 @@ namespace System.IO.IsolatedStorage
public override Task FlushAsync(CancellationToken cancellationToken)
{
- return _fs.FlushAsync();
+ return _fs.FlushAsync(cancellationToken);
}
public override void SetLength(long value)
diff --git a/src/libraries/System.Net.Http.Json/src/System.Net.Http.Json.csproj b/src/libraries/System.Net.Http.Json/src/System.Net.Http.Json.csproj
index 3440bd8..ee6e5c6 100644
--- a/src/libraries/System.Net.Http.Json/src/System.Net.Http.Json.csproj
+++ b/src/libraries/System.Net.Http.Json/src/System.Net.Http.Json.csproj
@@ -16,6 +16,7 @@
+
@@ -24,6 +25,7 @@
+
diff --git a/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.cs b/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.cs
index b666f22..753c031 100644
--- a/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.cs
+++ b/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.cs
@@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace System.Net.Http.Json
{
- public static class HttpContentJsonExtensions
+ public static partial class HttpContentJsonExtensions
{
public static Task