Skip to content

Defer BenchmarkDotNetSynchronizationContext install past discovery#3117

Closed
DrewScoggins wants to merge 1 commit into
dotnet:masterfrom
DrewScoggins:fix/discovery-deadlock-defer-synccontext
Closed

Defer BenchmarkDotNetSynchronizationContext install past discovery#3117
DrewScoggins wants to merge 1 commit into
dotnet:masterfrom
DrewScoggins:fix/discovery-deadlock-defer-synccontext

Conversation

@DrewScoggins
Copy link
Copy Markdown
Member

Below is Copilot's analysis of another issue that we are hitting with moving forward to the BDN that contains the async refactor. If this is us using ParamsSource and ArgumentsSource wrong, I am happy to make the changes in our code, but I was not sure what the right solution was.

The sync entrypoints BenchmarkSwitcher.Run/RunAll/RunAllJoined and BenchmarkRunner.Run overloads were installing
BenchmarkDotNetSynchronizationContext (a single-threaded post/pump context) before benchmark discovery. Discovery evaluates user [ParamsSource]/[ArgumentsSource] members. If any of them perform sync-over-async work (e.g. someTask.GetAwaiter().GetResult()), the async continuation captures the BDN ctx and is queued; the pump thread is blocked inside .GetResult(); classic single-threaded deadlock.

Repro in dotnet/performance:
SslStreamTests.GetTls13Support() does HandshakeAsync(...).GetAwaiter().GetResult()
inside a [ArgumentsSource(nameof(TlsProtocols))] code path. With
BDN 0.16.0 (post Async Refactor 4229d4f), --list flat hangs
forever. Pre-Refactor sync Run() did not install the SyncCtx and
the same code worked for years.

Fix: defer SyncCtx install. Sync entrypoints now route through a tiny ExecuteSynchronously(ValueTask) helper that:

  • returns immediately if the task is already completed (covers --list/--info/--help/no-benchmarks early exits)
  • otherwise installs the BDN ctx and pumps until completion (preserves SyncCtx semantics for execution awaits)

Internal awaits use ConfigureAwait(false) so nothing is actually posted to the BDN ctx in modern code; the SyncCtx is essentially decorative during the pump.

The sync entrypoints BenchmarkSwitcher.Run/RunAll/RunAllJoined and
BenchmarkRunner.Run<T> overloads were installing
BenchmarkDotNetSynchronizationContext (a single-threaded post/pump
context) before benchmark discovery. Discovery evaluates user
[ParamsSource]/[ArgumentsSource] members. If any of them perform
sync-over-async work (e.g. someTask.GetAwaiter().GetResult()), the
async continuation captures the BDN ctx and is queued; the pump thread
is blocked inside .GetResult(); classic single-threaded deadlock.

Repro in dotnet/performance:
    SslStreamTests.GetTls13Support() does HandshakeAsync(...).GetAwaiter().GetResult()
    inside a [ArgumentsSource(nameof(TlsProtocols))] code path. With
    BDN 0.16.0 (post Async Refactor 4229d4f), `--list flat` hangs
    forever. Pre-Refactor sync Run() did not install the SyncCtx and
    the same code worked for years.

Fix: defer SyncCtx install. Sync entrypoints now route through a tiny
ExecuteSynchronously<T>(ValueTask<T>) helper that:
  - returns immediately if the task is already completed
    (covers --list/--info/--help/no-benchmarks early exits)
  - otherwise installs the BDN ctx and pumps until completion
    (preserves SyncCtx semantics for execution awaits)

Internal awaits use ConfigureAwait(false) so nothing is actually posted
to the BDN ctx in modern code; the SyncCtx is essentially decorative
during the pump.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@timcassell
Copy link
Copy Markdown
Collaborator

This change causes the async chain to lose the context, causing ConfigureAwait(true) to be incorrect. Sync-over-async is well-known to be prone to deadlock and is a code smell, I don't think we should be encouraging its use. Rather the correct fix is to update ParamsSource and ArgumentsSource to support IAsyncEnumerable.

@timcassell
Copy link
Copy Markdown
Collaborator

To unblock you now, you can switch to the Run*Async APIs that do not install the sync context.

@DrewScoggins
Copy link
Copy Markdown
Member Author

Sounds good. I appreciate all your time looking at these PRs, I know there have been a lot lately, and I don't have the deepest understanding of BDN. So appreciate your help.

@DrewScoggins
Copy link
Copy Markdown
Member Author

Going to go ahead and close this.

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.

2 participants