From db03be5ad08d7fa6a45e6e64900c8ec3da7e1519 Mon Sep 17 00:00:00 2001 From: Ivan Gechev Date: Fri, 13 Mar 2026 15:06:36 +0200 Subject: [PATCH 1/2] fix: add ConfigureAwait(false) to prevent sync context deadlocks and short-circuit on 204 NoContent --- CloudConvert.API/RestHelper.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/CloudConvert.API/RestHelper.cs b/CloudConvert.API/RestHelper.cs index 675e15c..df2d7cf 100644 --- a/CloudConvert.API/RestHelper.cs +++ b/CloudConvert.API/RestHelper.cs @@ -22,12 +22,17 @@ internal RestHelper(HttpClient httpClient) internal async Task RequestAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - var response = await _httpClient.SendAsync(request, cancellationToken); - var responseRaw = await response.Content.ReadAsStringAsync(cancellationToken); + var response = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); - // Handle empty response body (e.g., HTTP 204 No Content) - // System.Text.Json throws when trying to deserialize an empty string - if (string.IsNullOrWhiteSpace(responseRaw) || response.StatusCode == System.Net.HttpStatusCode.NoContent) + // Short-circuit earlier as 204 will never have a body to read + if (response.StatusCode == System.Net.HttpStatusCode.NoContent) + { + return default; + } + + var responseRaw = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + + if (string.IsNullOrWhiteSpace(responseRaw)) { return default; } @@ -37,8 +42,8 @@ internal async Task RequestAsync(HttpRequestMessage request, CancellationT internal async Task RequestAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - var response = await _httpClient.SendAsync(request, cancellationToken); - return await response.Content.ReadAsStringAsync(cancellationToken); + var response = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false); + return await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); } } } From a30689c4a0b42b1e495460a5c90b8a2b83ffdb26 Mon Sep 17 00:00:00 2001 From: Ivan Gechev Date: Fri, 13 Mar 2026 15:06:42 +0200 Subject: [PATCH 2/2] fix: add ConfigureAwait(false) to prevent sync context deadlocks --- CloudConvert.API/WebApiHandler.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CloudConvert.API/WebApiHandler.cs b/CloudConvert.API/WebApiHandler.cs index b8096de..35479b0 100644 --- a/CloudConvert.API/WebApiHandler.cs +++ b/CloudConvert.API/WebApiHandler.cs @@ -12,11 +12,12 @@ protected override async Task SendAsync(HttpRequestMessage { try { - var response = await base.SendAsync(request, cancellationToken); + var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); if ((int)response.StatusCode >= 400) { - throw new WebApiException((await response.Content.ReadAsStringAsync(cancellationToken)).TrimLengthWithEllipsis(20000)); + var errorBody = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + throw new WebApiException(errorBody.TrimLengthWithEllipsis(20000)); } return response;