Repository navigation
[efficiency-improver] Default SynchronousAwaiter.Await to a blocking wait instead of CPU-spinning - #11786
Conversation
…spinning busy-wait RecordStart/RecordResult (FrameworkHandlerAdapter) and test-case discovery (TestCaseDiscoverySinkAdapter) call Await() once per test/test-case, so this runs on every test in a VSTest-bridged run. The previous default (busyWait: true) spun on SpinWait.SpinOnce() in a tight loop until the awaited publish task completed, instead of blocking the thread. For the common case (an already-completed task, e.g. an unbounded channel write) this made no difference, but whenever the awaited publish routed to an inline IBlockingDataConsumer doing real synchronous work, spinning burned CPU cycles with no latency benefit over a kernel-level block. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The change selects an existing blocking path; the remaining documentation correction is non-blocking.
Review effort: Balanced
Findings: 1
What changed in this PR
Changes VSTest bridge synchronous waits to use the existing blocking path by default, avoiding explicit polling of incomplete tasks.
Changes:
- Defaults
busyWaittofalse, retaining opt-in spinning. - Records the changed default in the internal API baseline.
Confidence at a glance
🟢 VSTest bridge
Small change selecting an existing implementation; tests were not rerun.| File | Description |
|---|---|
| src/Platform/Microsoft.Testing.Extensions.VSTestBridge/InternalAPI.Unshipped.txt | Tracks the default-value change. |
| src/Platform/Microsoft.Testing.Extensions.VSTestBridge/Helpers/SynchronousAwaiter.cs | Defaults to blocking and adds explanatory comments. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Note
🤖 Automated review by GitHub Copilot. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.
Review outcome: No actionable findings — the default-flip is internal-only, call sites are uniform, and the blocking wait cannot deadlock in this host.
Change classification
- VSTestBridge internal helper:
SynchronousAwaiter.Await'sbusyWaitdefault flips fromtruetofalse, soRecordStart/RecordResult/RecordAttachments/SendMessage/SendTestCasenow block onGetAwaiter().GetResult()instead of spinning onSpinWait. - Internal API tracking:
InternalAPI.Unshipped.txtrecords the old signature as*REMOVED*and adds the new default, matching the repo's established churn convention forinternalsurface (e.g.Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt).
Confidence at a glance
🟢 Correctness & design — verified rationale, no regression
- Why it applies: Behavioral default change on a shared internal helper used on every test start/end and discovered test case.
- Evidence: All 7 call sites (
FrameworkHandlerAdapter.cs:108,140,160,MessageLoggerAdapter.cs:58,62,66,TestCaseDiscoverySinkAdapter.cs:94) call.Await()with no explicitbusyWaitargument, so they uniformly pick up the new default — no call site was missed or left spinning unintentionally. - Gap or disposition: Both branches of the
if (busyWait)inSynchronousAwaiter.csstill end in an identicalvalueTask.GetAwaiter().GetResult()call (pre-existing structure, not introduced here); the new 6-line comment adequately explains why blocking is now the default.
🟢 Concurrency & lifecycle — no deadlock risk
- Why it applies: Flipping to
GetAwaiter().GetResult()as the default raises the classic "blocking call deadlocks under a capturedSynchronizationContext" concern (Dimension 2, rule 4). - Evidence:
AsynchronousMessageBus.PublishAsyncand its downstream consumers (e.g.BlockingConsumerDataProcessor.PublishAsync) use.ConfigureAwait(false)throughout, and these adapters run inside the MTP console test host, which never installs a customSynchronizationContext. There is no continuation that needs to resume on the calling thread, so blocking cannot deadlock. - Gap or disposition: None — the PR's own code comment correctly frames this as "parks on a kernel wait handle" rather than a context-marshaling deadlock risk.
🟢 API & compatibility — internal-only, convention followed
- Why it applies: Overload/default-parameter changes are flagged as a potential source-break risk per the repo's overload-resolution guidance, and
PublicAPI/InternalAPItracking files must be updated. - Evidence:
SynchronousAwaiterandAwaitareinternal, so no public API or binary-compatibility contract is affected.InternalAPI.Unshipped.txtcorrectly pairs a*REMOVED*old-default entry with the new-default entry, consistent with how other internal signature changes are tracked elsewhere in the repo (Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt). - Gap or disposition: None.
🟢 Performance — rationale is sound
- Why it applies: The change is explicitly a performance optimization for large test suites.
- Evidence:
BlockingConsumerDataProcessorperforms real inline synchronous consumer work under a semaphore; for that path,SpinWait.SpinOnce()pegs a core with no latency benefit over a kernel-level block, exactly as the added comment states. For the common already-completed-task case (unbounded channel write), both old and new defaults are equally cheap. - Gap or disposition: None — no measurable downside identified; this should reduce CPU usage in large VSTest-bridged suites without changing observed behavior.
🟡 Tests — no dedicated coverage, pre-existing gap
- Why it applies: Dimension 13 (new/changed branches need test coverage).
- Evidence: No test file exists for
SynchronousAwaiterintest/UnitTests/Microsoft.Testing.Extensions.VSTestBridge.UnitTests/, and this PR does not add one. - Gap or disposition: This is a pre-existing gap (the helper was untested before this change too), not a regression introduced by this diff. The behavioral change itself is low-risk given the
ConfigureAwait(false)chain analysis above, so this does not block merge, but a small unit test asserting bothbusyWait: trueand the new default (false) observe and rethrow exceptions correctly would close the gap cheaply. - Action: Recommended, not required: add a focused
SynchronousAwaiterTestscovering exception propagation for bothbusyWaitvalues.
⚪ Not applicable: Security & protocol, Performance (allocations beyond the stated rationale), Cross-TFM compatibility, Localization, Flakiness, Analyzers, Build/dependencies/scripts, Scope & PR discipline, PowerShell scripting hygiene.
Updated comments to clarify behavior of GetAwaiter().GetResult() and its impact on thread management. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

…spinning busy-wait
RecordStart/RecordResult (FrameworkHandlerAdapter) and test-case discovery (TestCaseDiscoverySinkAdapter) call Await() once per test/test-case, so this runs on every test in a VSTest-bridged run. The previous default (busyWait: true) spun on SpinWait.SpinOnce() in a tight loop until the awaited publish task completed, instead of blocking the thread. For the common case (an already-completed task, e.g. an unbounded channel write) this made no difference, but whenever the awaited publish routed to an inline IBlockingDataConsumer doing real synchronous work, spinning burned CPU cycles with no latency benefit over a kernel-level block.
Fixes #11779