Skip to content

Switch all benchmark runners from BenchmarkSwitcher.Run to RunAsync#5227

Open
DrewScoggins wants to merge 3 commits into
dotnet:mainfrom
DrewScoggins:UddateBDNRunners
Open

Switch all benchmark runners from BenchmarkSwitcher.Run to RunAsync#5227
DrewScoggins wants to merge 3 commits into
dotnet:mainfrom
DrewScoggins:UddateBDNRunners

Conversation

@DrewScoggins
Copy link
Copy Markdown
Member

BDN 0.16's sync entrypoints install BenchmarkDotNetSynchronizationContext (a single-threaded message pump) before benchmark discovery. Any code that does sync-over-async (e.g. ML.NET internals, SslStream handshakes) deadlocks against that pump. The async entrypoint avoids this by not installing the SyncCtx on the caller thread.

This applies the same fix that was already made to MicroBenchmarks in c14e7c4 to all remaining real-world benchmark runners:

  • Akade.IndexedSet.Benchmarks
  • bepuphysics2
  • ILLink
  • ImageSharp
  • Microsoft.ML.Benchmarks
  • PowerShell.Benchmarks
  • Roslyn

BDN 0.16's sync entrypoints install BenchmarkDotNetSynchronizationContext
(a single-threaded message pump) before benchmark discovery. Any code that
does sync-over-async (e.g. ML.NET internals, SslStream handshakes)
deadlocks against that pump. The async entrypoint avoids this by not
installing the SyncCtx on the caller thread.

This applies the same fix that was already made to MicroBenchmarks in
c14e7c4 to all remaining real-world benchmark runners:
- Akade.IndexedSet.Benchmarks
- bepuphysics2
- ILLink
- ImageSharp
- Microsoft.ML.Benchmarks
- PowerShell.Benchmarks
- Roslyn

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 20, 2026 17:36
@DrewScoggins DrewScoggins requested a review from LoopedBard3 May 20, 2026 17:36
LoopedBard3
LoopedBard3 previously approved these changes May 20, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the real-world benchmark runner entrypoints to use BenchmarkDotNet’s async runner APIs to avoid deadlocks caused by BenchmarkDotNetSynchronizationContext being installed by the sync entrypoints during benchmark discovery.

Changes:

  • Switched multiple real-world benchmark runners from BenchmarkSwitcher.Run(...) to RunAsync(...).
  • Updated entrypoints to async (including Task<int> where exit codes are already propagated via ToExitCode()).
  • Added explanatory comments about avoiding the single-threaded BDN synchronization context.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/benchmarks/real-world/Roslyn/Program.cs Switches Roslyn runner to RunAsync and preserves exit-code behavior via ToExitCode().
src/benchmarks/real-world/PowerShell.Benchmarks/Program.cs Switches to RunAsync with async Task<int> Main and preserves exit-code behavior.
src/benchmarks/real-world/Microsoft.ML.Benchmarks/Program.cs Switches to RunAsync with async Task<int> Main and preserves exit-code behavior.
src/benchmarks/real-world/ImageSharp/Program.cs Switches to RunAsync with async Task Main; currently does not propagate failure exit codes.
src/benchmarks/real-world/ILLink/Program.cs Switches to RunAsync with async Task<int> Main and preserves exit-code behavior.
src/benchmarks/real-world/bepuphysics2/Program.cs Switches to RunAsync with async Task Main; currently does not propagate failure exit codes.
src/benchmarks/real-world/Akade.IndexedSet.Benchmarks/Program.cs Switches to RunAsync via top-level await; currently does not propagate failure exit codes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +8 to +11
await BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).RunAsync(args, RecommendedConfig.Create(
artifactsPath: new DirectoryInfo(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location)!, "BenchmarkDotNet.Artifacts")),
mandatoryCategories: ImmutableHashSet.Create(Categories.AkadeIndexedSet))); No newline at end of file
mandatoryCategories: ImmutableHashSet.Create(Categories.AkadeIndexedSet)))
.ConfigureAwait(false); No newline at end of file
Comment on lines +22 to +29
public static async Task Main(string[] args)
{
await BenchmarkSwitcher
.FromAssembly(typeof(Program).Assembly)
.RunAsync(args, RecommendedConfig.Create(
artifactsPath: new DirectoryInfo(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "BenchmarkDotNet.Artifacts")),
mandatoryCategories: ImmutableHashSet.Create(Categories.ImageSharp)))
.ConfigureAwait(false);
Comment on lines +12 to +19
public static async Task Main(string[] args)
{
await BenchmarkSwitcher
.FromAssembly(typeof(BepuPhysics2Benchmarks).Assembly)
.Run(args, RecommendedConfig.Create(
.RunAsync(args, RecommendedConfig.Create(
artifactsPath: new DirectoryInfo(Path.Combine(Path.GetDirectoryName(typeof(BepuPhysics2Benchmarks).Assembly.Location), "BenchmarkDotNet.Artifacts")),
mandatoryCategories: ImmutableHashSet.Create(Categories.BepuPhysics)))
.ToExitCode();
.ConfigureAwait(false);
Capture the summaries returned by BenchmarkSwitcher.RunAsync and return
summaries.ToExitCode() so CI can detect benchmark failures and critical
validation errors via a non-zero process exit code.

Addresses Copilot review comments on PR #5227.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
BDN 0.16 installs BenchmarkDotNetSynchronizationContext in the child
process that executes each benchmark. ML.NET's ApplyWordEmbedding
downloads a pretrained model using sync-over-async I/O internally,
which deadlocks on the single-threaded SyncCtx.

Save and clear the SynchronizationContext before the ML.NET pipeline
code and restore it afterward, so the async download can complete
without deadlocking.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants