Skip to content

[efficiency-improver] Default SynchronousAwaiter.Await to a blocking wait instead of CPU-spinning - #11786

Merged
Amaury Levé (Evangelink) merged 2 commits into
mainfrom
efficiency/synchronousawaiter-default-blocking-wait-374f9557123af77b
Oct 7, 2026
Merged

Amaury Levé (Evangelink) merged 2 commits into
mainfrom
efficiency/synchronousawaiter-default-blocking-wait-374f9557123af77b

Conversation

@Evangelink

Copy link
Copy Markdown
Member

…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

…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>
Copilot AI balanced review requested due to automatic review settings October 7, 2026 09:10
@github-actions github-actions Bot added the state/needs-review Awaiting review from the team. label Oct 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The change selects an existing blocking path; the remaining documentation correction is non-blocking.

Review effort: Balanced
Findings: 1 Low severity

Open (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 busyWait to false, 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.

@microsoft-github-policy-service microsoft-github-policy-service Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Oct 7, 2026
@github-actions github-actions Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Oct 7, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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's busyWait default flips from true to false, so RecordStart/RecordResult/RecordAttachments/SendMessage/SendTestCase now block on GetAwaiter().GetResult() instead of spinning on SpinWait.
  • Internal API tracking: InternalAPI.Unshipped.txt records the old signature as *REMOVED* and adds the new default, matching the repo's established churn convention for internal surface (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 explicit busyWait argument, 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) in SynchronousAwaiter.cs still end in an identical valueTask.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 captured SynchronizationContext" concern (Dimension 2, rule 4).
  • Evidence: AsynchronousMessageBus.PublishAsync and 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 custom SynchronizationContext. 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/InternalAPI tracking files must be updated.
  • Evidence: SynchronousAwaiter and Await are internal, so no public API or binary-compatibility contract is affected. InternalAPI.Unshipped.txt correctly 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: BlockingConsumerDataProcessor performs 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 SynchronousAwaiter in test/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 both busyWait: true and the new default (false) observe and rethrow exceptions correctly would close the gap cheaply.
  • Action: Recommended, not required: add a focused SynchronousAwaiterTests covering exception propagation for both busyWait values.

⚪ 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.

@microsoft-github-policy-service microsoft-github-policy-service Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Oct 7, 2026
@github-actions github-actions Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Oct 7, 2026
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>
Copilot AI balanced review requested due to automatic review settings October 7, 2026 09:34
@github-actions github-actions Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Oct 7, 2026
@Evangelink
Amaury Levé (Evangelink) merged commit 1c6a51f into main Oct 7, 2026
13 of 15 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the efficiency/synchronousawaiter-default-blocking-wait-374f9557123af77b branch October 7, 2026 09:34
@github-actions github-actions Bot removed the state/needs-review Awaiting review from the team. label Oct 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The narrow change selects an existing implementation, preserves exception propagation, and updates API tracking consistently.

Review effort: Balanced
Findings: None

Resolved since last review (1)

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.

[efficiency-improver] Default SynchronousAwaiter.Await to a blocking wait instead of CPU-spinning

2 participants