Replace `.Result` with synchronous `HttpClient.Send()` in `TestJsonCommand` by euxaristia · Pull Request #27624 · PowerShell/PowerShell · GitHub
Skip to content

Replace .Result with synchronous HttpClient.Send() in TestJsonCommand#27624

Open
euxaristia wants to merge 1 commit into
PowerShell:masterfrom
euxaristia:fix/http-result-deadlock
Open

Replace .Result with synchronous HttpClient.Send() in TestJsonCommand#27624
euxaristia wants to merge 1 commit into
PowerShell:masterfrom
euxaristia:fix/http-result-deadlock

Conversation

@euxaristia

Copy link
Copy Markdown

PR Summary

Replace client.GetStringAsync(uri).Result with client.Send(new HttpRequestMessage(HttpMethod.Get, uri)) + response.Content.ReadAsString() in the ProcessJson method.

PR Context

The .Result property on a Task<T> can cause deadlocks (e.g., when the SynchronizationContext is captured and the continuation is posted back to a single-threaded context) and contributes to thread-pool starvation. The replacement uses HttpClient.Send() with readResponse: false to avoid buffering the entire response, then reads the content synchronously with ReadAsString(). This achieves the same synchronous blocking semantics without the deadlock risk.

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

@euxaristia euxaristia requested a review from a team as a code owner June 21, 2026 22:09
Copilot AI review requested due to automatic review settings June 21, 2026 22:09

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 updates the Test-Json cmdlet’s schema auto-fetch implementation to avoid blocking on Task.Result by switching the HTTP retrieval logic from HttpClient.GetStringAsync(...).Result to a synchronous HttpClient.Send(...) flow.

Changes:

  • Replaces GetStringAsync(...).Result with HttpClient.Send(...) + synchronous content read in SchemaRegistry.Global.Fetch.
  • Introduces explicit HttpResponseMessage disposal via using var.

Comment on lines +149 to +150
…mmand`

The use of `.Result` on a `Task<string>` can cause deadlocks and
thread-pool starvation when called from contexts with a
synchronization context. Replace with `HttpClient.Send()` +
`Content.ReadAsString()` for the synchronous HTTP call path.
@euxaristia euxaristia force-pushed the fix/http-result-deadlock branch from f90085f to e28381c Compare June 21, 2026 23:47
@euxaristia

Copy link
Copy Markdown
Author

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