Replace `.Wait()` and `.Result` with `.GetAwaiter().GetResult()` in help download by euxaristia · Pull Request #27620 · PowerShell/PowerShell · GitHub
Skip to content

Replace .Wait() and .Result with .GetAwaiter().GetResult() in help download#27620

Open
euxaristia wants to merge 2 commits into
PowerShell:masterfrom
euxaristia:fix/async-wait-result
Open

Replace .Wait() and .Result with .GetAwaiter().GetResult() in help download#27620
euxaristia wants to merge 2 commits into
PowerShell:masterfrom
euxaristia:fix/async-wait-result

Conversation

@euxaristia

Copy link
Copy Markdown

PR Summary

Replace .Wait() and .Result calls in DownloadHelpContent and WriteResponseToFile with .GetAwaiter().GetResult() to avoid deadlock risks and AggregateException wrapping.

PR Context

The .Wait() and .Result methods on Task wrap exceptions in AggregateException and can cause deadlocks in synchronization contexts (e.g., if the SynchronizationContext is captured and the continuation is posted back to a single-threaded context). Using .GetAwaiter().GetResult() unwraps the inner exception and does not capture the synchronization context.

In WriteResponseToFile, the original code also used a redundant copyStreamOp.Wait() + copyStreamOp.Exception pattern. The new code uses a try/catch block for cleaner error handling.

PR Checklist

  • PR title is meaningful and in present tense, imperative mood
  • Changes are summarized in the PR description
  • All files have the Microsoft copyright header
  • Ready for review -- this is not a Draft PR
  • Breaking changes: None
  • User-facing changes: No
  • Testing: N/A -- refactoring only, no behavioral change

…elp download

The `.Wait()` and `.Result` methods on `Task` wrap exceptions in `AggregateException` and can cause deadlocks in synchronization contexts. Use `.GetAwaiter().GetResult()` which unwraps the inner exception and avoids synchronization context capture.
Copilot AI review requested due to automatic review settings June 21, 2026 22:07

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.

Pull request overview

This PR refactors the updatable help download path to avoid using Task.Wait() / Task.Result, switching to .GetAwaiter().GetResult() to prevent AggregateException wrapping and reduce deadlock risk in DownloadHelpContentHttpClient and WriteResponseToFile.

Changes:

  • Replaced GetAsync(...).Wait()/.Result with .GetAwaiter().GetResult() and adjusted cancellation handling.
  • Refactored WriteResponseToFile to remove the Wait() + Exception pattern in favor of try/catch around the synchronous wait.

Comment on lines +793 to +796
Comment on lines 809 to 818
…essage

Return false on cancellation (not _stopping) so the caller doesn't treat a canceled download as success. Wrap HttpResponseMessage in using to avoid holding connections.
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