Skip to content

Commit db03be5

Browse files
committed
fix: add ConfigureAwait(false) to prevent sync context deadlocks and short-circuit on 204 NoContent
1 parent d5e3de8 commit db03be5

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

CloudConvert.API/RestHelper.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ internal RestHelper(HttpClient httpClient)
2222

2323
internal async Task<T> RequestAsync<T>(HttpRequestMessage request, CancellationToken cancellationToken)
2424
{
25-
var response = await _httpClient.SendAsync(request, cancellationToken);
26-
var responseRaw = await response.Content.ReadAsStringAsync(cancellationToken);
25+
var response = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
2726

28-
// Handle empty response body (e.g., HTTP 204 No Content)
29-
// System.Text.Json throws when trying to deserialize an empty string
30-
if (string.IsNullOrWhiteSpace(responseRaw) || response.StatusCode == System.Net.HttpStatusCode.NoContent)
27+
// Short-circuit earlier as 204 will never have a body to read
28+
if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
29+
{
30+
return default;
31+
}
32+
33+
var responseRaw = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
34+
35+
if (string.IsNullOrWhiteSpace(responseRaw))
3136
{
3237
return default;
3338
}
@@ -37,8 +42,8 @@ internal async Task<T> RequestAsync<T>(HttpRequestMessage request, CancellationT
3742

3843
internal async Task<string> RequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
3944
{
40-
var response = await _httpClient.SendAsync(request, cancellationToken);
41-
return await response.Content.ReadAsStringAsync(cancellationToken);
45+
var response = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
46+
return await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
4247
}
4348
}
4449
}

0 commit comments

Comments
 (0)