From 026168ea5ca7223ee2f61522b2af4d7e4505f690 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Mon, 15 Jul 2019 19:36:01 +0100 Subject: [PATCH] dispose CancellationTokenSources Commit migrated from https://github.com/dotnet/corefx/commit/58c85e29cfaa4b6bd8002a5c90c5262ca0cb29cf --- .../tests/StressTests/HttpStress/Program.cs | 67 ++++++++++++---------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/src/libraries/System.Net.Http/tests/StressTests/HttpStress/Program.cs b/src/libraries/System.Net.Http/tests/StressTests/HttpStress/Program.cs index d46f818..570eb66 100644 --- a/src/libraries/System.Net.Http/tests/StressTests/HttpStress/Program.cs +++ b/src/libraries/System.Net.Http/tests/StressTests/HttpStress/Program.cs @@ -572,35 +572,36 @@ public class Program { long opIndex = i % clientOperations.Length; (string operation, Func func) = clientOperations[opIndex]; - // request-specific context - var requestContext = new RequestContext(client, random, taskNum, cancellationProbability); - try + using (var requestContext = new RequestContext(client, random, taskNum, cancellationProbability)) { - await func(requestContext); - - Increment(ref success[opIndex]); - } - catch (OperationCanceledException) when (requestContext.CancellationToken.IsCancellationRequested) - { - Increment(ref cancel[opIndex]); - } - catch (Exception e) - { - Increment(ref fail[opIndex]); + try + { + await func(requestContext); - if (e is HttpRequestException hre && hre.InnerException is SocketException se && se.SocketErrorCode == SocketError.AddressAlreadyInUse) + Increment(ref success[opIndex]); + } + catch (OperationCanceledException) when (requestContext.CancellationToken.IsCancellationRequested) { - Interlocked.Increment(ref reuseAddressFailure); + Increment(ref cancel[opIndex]); } - else + catch (Exception e) { - lock (Console.Out) + Increment(ref fail[opIndex]); + + if (e is HttpRequestException hre && hre.InnerException is SocketException se && se.SocketErrorCode == SocketError.AddressAlreadyInUse) { - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine($"Error from iteration {i} ({operation}) in task {taskNum} with {success.Sum()} successes / {fail.Sum()} fails:"); - Console.ResetColor(); - Console.WriteLine(e); - Console.WriteLine(); + Interlocked.Increment(ref reuseAddressFailure); + } + else + { + lock (Console.Out) + { + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine($"Error from iteration {i} ({operation}) in task {taskNum} with {success.Sum()} successes / {fail.Sum()} fails:"); + Console.ResetColor(); + Console.WriteLine(e); + Console.WriteLine(); + } } } } @@ -655,24 +656,26 @@ public class Program } /// Client context containing information pertaining to a single request. - private sealed class RequestContext + private sealed class RequestContext : IDisposable { private readonly Random _random; + private readonly CancellationTokenSource _cts; public RequestContext(HttpClient httpClient, Random random, int taskNum, double cancellationProbability) { _random = random; TaskNum = taskNum; HttpClient = httpClient; - CancellationToken = - (GetRandomBoolean(cancellationProbability)) - ? CreateCancellationTokenWithRandomizedCancellationDelay() - : CancellationToken.None; - CancellationToken CreateCancellationTokenWithRandomizedCancellationDelay(int maxDelayMs = 5) + if(GetRandomBoolean(cancellationProbability)) { - var delay = TimeSpan.FromMilliseconds(GetRandomInt(maxDelayMs)); - return new CancellationTokenSource(delay).Token; + var delay = TimeSpan.FromMilliseconds(GetRandomInt(maxValue: 5)); + _cts = new CancellationTokenSource(delay); + CancellationToken = _cts.Token; + } + else + { + CancellationToken = CancellationToken.None; } } public int TaskNum { get; } @@ -700,6 +703,8 @@ public class Program public Version GetRandomVersion(Version[] versions) => versions[_random.Next(0, versions.Length)]; + + public void Dispose() => _cts?.Dispose(); } /// HttpContent that partially serializes and then waits for cancellation to be requested. -- 2.7.4