Skip to content

Commit

Permalink
fix: properly dispose response on exception (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwwoda authored May 4, 2022
1 parent 73792a5 commit 8415bd3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Box.V2/Request/HttpRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public async Task<IBoxResponse<T>> ExecuteAsyncWithoutRetry<T>(IBoxRequest reque
{
// Need to account for special cases when the return type is a stream
var isStream = typeof(T) == typeof(Stream);
HttpResponseMessage response = null;

try
{
Expand All @@ -42,13 +43,14 @@ public async Task<IBoxResponse<T>> ExecuteAsyncWithoutRetry<T>(IBoxRequest reque

HttpRequestMessage httpRequest = GetHttpRequest(request, isMultiPartRequest, isBinaryRequest);
Debug.WriteLine(string.Format("RequestUri: {0}", httpRequest.RequestUri));
HttpResponseMessage response = await GetResponse(request, isStream, httpRequest).ConfigureAwait(false);
response = await GetResponse(request, isStream, httpRequest).ConfigureAwait(false);
BoxResponse<T> boxResponse = await GetBoxResponse<T>(isStream, response).ConfigureAwait(false);

return boxResponse;
}
catch (Exception ex)
{
response?.Dispose();
Debug.WriteLine(string.Format("Exception: {0}", ex.Message));
throw;
}
Expand All @@ -61,6 +63,7 @@ public async Task<IBoxResponse<T>> ExecuteAsync<T>(IBoxRequest request)
var isStream = typeof(T) == typeof(Stream);
var retryCounter = 0;
var expBackoff = new ExponentialBackoff();
HttpResponseMessage response = null;

try
{
Expand All @@ -73,7 +76,7 @@ public async Task<IBoxResponse<T>> ExecuteAsync<T>(IBoxRequest request)
using (HttpRequestMessage httpRequest = GetHttpRequest(request, isMultiPartRequest, isBinaryRequest))
{
Debug.WriteLine(string.Format("RequestUri: {0}", httpRequest.RequestUri));
HttpResponseMessage response = await GetResponse(request, isStream, httpRequest).ConfigureAwait(false);
response = await GetResponse(request, isStream, httpRequest).ConfigureAwait(false);
//need to wait for Retry-After seconds and then retry request
var retryAfterHeader = response.Headers.RetryAfter;

Expand All @@ -94,6 +97,7 @@ public async Task<IBoxResponse<T>> ExecuteAsync<T>(IBoxRequest request)
(response.StatusCode == HttpStatusCode.Accepted && retryAfterHeader != null))
&& retryCounter++ < RetryLimit)
{
response?.Dispose();
TimeSpan delay = expBackoff.GetRetryTimeout(retryCounter);

Debug.WriteLine("HttpCode : {0}. Waiting for {1} seconds to retry request. RequestUri: {2}", response.StatusCode, delay.Seconds, httpRequest.RequestUri);
Expand All @@ -111,6 +115,7 @@ public async Task<IBoxResponse<T>> ExecuteAsync<T>(IBoxRequest request)
}
catch (Exception ex)
{
response?.Dispose();
Debug.WriteLine(string.Format("Exception: {0}", ex.Message));
throw;
}
Expand Down

0 comments on commit 8415bd3

Please sign in to comment.