Skip to content

Commit

Permalink
[BUG] Deadlock prone code in HttpConnection.cs #689: Changed synchron…
Browse files Browse the repository at this point in the history
…ous methods in HttpConnection.cs to use Task.Run/await to avoid deadlocks in accordance with: https://learn.microsoft.com/en-us/archive/blogs/jpsanders/asp-net-do-not-use-task-result-in-main-context
  • Loading branch information
DavidPoulsenHGS committed Aug 1, 2024
1 parent f3bae74 commit 3c063ee
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/OpenSearch.Net/Connection/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ public virtual TResponse Request<TResponse>(RequestData requestData)

if (requestData.ThreadPoolStats)
threadPoolStats = ThreadPoolStats.GetStats();

responseMessage = client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult();
var task = Task.Run(() => client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead));
task.Wait();
responseMessage = task.Result;
statusCode = (int)responseMessage.StatusCode;
d.EndState = statusCode;
}
Expand All @@ -120,7 +121,9 @@ public virtual TResponse Request<TResponse>(RequestData requestData)
if (responseMessage.Content != null)
{
receive = DiagnosticSource.Diagnose(DiagnosticSources.HttpConnection.ReceiveBody, requestData, statusCode);
responseStream = responseMessage.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
var task = Task.Run(() => responseMessage.Content.ReadAsStreamAsync());
task.Wait();
responseStream = task.Result;
}
}
catch (TaskCanceledException e)
Expand Down

0 comments on commit 3c063ee

Please sign in to comment.