From 3c063ee7dd51715ce86dfb5445dcc9f5ace0eed0 Mon Sep 17 00:00:00 2001 From: David Poulsen Date: Thu, 1 Aug 2024 11:15:45 +0200 Subject: [PATCH] [BUG] Deadlock prone code in HttpConnection.cs #689: Changed synchronous 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 --- src/OpenSearch.Net/Connection/HttpConnection.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/OpenSearch.Net/Connection/HttpConnection.cs b/src/OpenSearch.Net/Connection/HttpConnection.cs index dfb8506fd9..bb9d4eb598 100644 --- a/src/OpenSearch.Net/Connection/HttpConnection.cs +++ b/src/OpenSearch.Net/Connection/HttpConnection.cs @@ -107,8 +107,9 @@ public virtual TResponse Request(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; } @@ -120,7 +121,9 @@ public virtual TResponse Request(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)