Defer BenchmarkDotNetSynchronizationContext install past discovery#3117
Closed
DrewScoggins wants to merge 1 commit into
Closed
Defer BenchmarkDotNetSynchronizationContext install past discovery#3117DrewScoggins wants to merge 1 commit into
DrewScoggins wants to merge 1 commit into
Conversation
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>
Collaborator
|
This change causes the async chain to lose the context, causing |
Collaborator
|
To unblock you now, you can switch to the |
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. |
Member
Author
|
Going to go ahead and close this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
ParamsSourceandArgumentsSourcewrong, 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 flathangsforever. 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:
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.