Skip to content

Commit 4a7a38b

Browse files
Prevent premature end of the Benchmark process at Ctrl-C, fixes dotnet#2483
Set ConsoleCancelEventArgs.Cancel to true so that Benchmark process continues and PowerPlan is reverted at end of aborted Benchmark.
1 parent 346bbab commit 4a7a38b

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

docs/articles/configs/configoptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Available config options are:
1212
* `ConfigOptions.Default` - No configuration option is set - this is the default.
1313
* `ConfigOptions.KeepBenchmarkFiles` - All auto-generated files should be kept after running the benchmarks (by default they are removed).
1414
* `ConfigOptions.JoinSummary` - All benchmarks results should be joined into a single summary (by default we have a summary per type).
15-
* `ConfigOptions.StopOnFirstError` - Benchmarking should be stopped after the first error (by default it's not).
15+
* `ConfigOptions.StopOnFirstError` - Benchmarking should be stopped after the first error (by default it's not). This also stops benchmarking when Ctrl+C or Ctrl+Break is pressed (by default, Ctrl+C or Ctrl+Break only aborts current benchmark. Remaining benchmarks will continue to run).
1616
* `ConfigOptions.DisableOptimizationsValidator` - Mandatory optimizations validator should be entirely turned off.
1717
* `ConfigOptions.DontOverwriteResults` - The exported result files should not be overwritten (by default they are overwritten).
1818
* `ConfigOptions.DisableLogFile` - Disables the log file written on disk.

docs/articles/guides/console-args.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ dotnet run -c Release -- --filter * --runtimes net6.0 net8.0 --statisticalTest 5
231231
* `--coreRun` path(s) to CoreRun (optional).
232232
* `--ilcPackages` path to ILCompiler for NativeAOT.
233233
* `--info` prints environment configuration including BenchmarkDotNet, OS, CPU and .NET version
234-
* `--stopOnFirstError` stop on first error.
234+
* `--stopOnFirstError` Benchmarking should be stopped after the first error (by default it's not). This also stops benchmarking when Ctrl+C or Ctrl+Break is pressed (by default, Ctrl+C or Ctrl+Break only aborts current benchmark. Remaining benchmarks will continue to run).
235235
* `--help` display this help screen.
236236
* `--version` display version information.
237237
* `--keepFiles` (default: false) determines if all auto-generated files should be kept or removed after running the benchmarks.

docs/articles/samples/IntroStopOnFirstError.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ uid: BenchmarkDotNet.Samples.IntroStopOnFirstError
66

77
BenchmarkDotNet can be configured to stop on first error. You just have to add `StopOnFirstError` attribute to your class or use `--stopOnFirstError` command line argument.
88

9+
This also stops benchmarking when Ctrl+C or Ctrl+Break is pressed.
10+
11+
By default, remaining benchmarks will continue to run when Ctrl+C or Ctrl+Break is pressed.
12+
913
### Source code
1014

1115
[!code-csharp[IntroStopOnFirstError.cs](../../../samples/BenchmarkDotNet.Samples/IntroStopOnFirstError.cs)]

src/BenchmarkDotNet/Attributes/StopOnFirstErrorAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace BenchmarkDotNet.Attributes
66
{
77
/// <summary>
8-
/// determines if running should be stop after first error
8+
/// Determines if running should be stop after first error or when Ctrl+C or Ctrl+Break is pressed.
99
/// </summary>
1010
[PublicAPI]
1111
[AttributeUsage(AttributeTargets.Class)]

src/BenchmarkDotNet/Helpers/ConsoleExitHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ private void Detach()
5252
private void ProcessOnExited(object sender, EventArgs e) => Detach();
5353

5454
// the user has clicked Ctrl+C so we kill the entire process tree
55-
private void CancelKeyPressHandlerCallback(object sender, ConsoleCancelEventArgs e) => KillProcessTree();
55+
private void CancelKeyPressHandlerCallback(object sender, ConsoleCancelEventArgs e)
56+
{
57+
e.Cancel = true; // Prevent premature termination of current process.
58+
KillProcessTree();
59+
}
5660

5761
// the user has closed the console window so we kill the entire process tree
5862
private void ProcessExitEventHandlerHandlerCallback(object sender, EventArgs e) => KillProcessTree();

0 commit comments

Comments
 (0)