diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java index 2ab41868f4e5..eea314cb5fda 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java @@ -99,6 +99,7 @@ public ServiceResponse getPutOrPatchResult(Response respons exception.setResponse(response); if (responseBody != null) { exception.setBody((CloudError) mapperAdapter.deserialize(responseBody.string(), CloudError.class)); + responseBody.close(); } throw exception; } @@ -108,6 +109,7 @@ public ServiceResponse getPutOrPatchResult(Response respons // Check provisioning state while (!AzureAsyncOperation.getTerminalStatuses().contains(pollingState.getStatus())) { + System.err.println(pollingState.getStatus()); Thread.sleep(pollingState.getDelayInMilliseconds()); if (pollingState.getAzureAsyncOperationHeaderLink() != null @@ -186,6 +188,7 @@ public AsyncPollingTask getPutOrPatchResultAsync(Response r try { if (responseBody != null) { exception.setBody((CloudError) mapperAdapter.deserialize(responseBody.string(), CloudError.class)); + responseBody.close(); } } catch (Exception e) { /* ignore serialization errors on top of service errors */ } callback.failure(exception); @@ -272,6 +275,7 @@ public ServiceResponse getPostOrDeleteResult(Response respo exception.setResponse(response); if (responseBody != null) { exception.setBody((CloudError) mapperAdapter.deserialize(responseBody.string(), CloudError.class)); + responseBody.close(); } throw exception; } @@ -281,6 +285,7 @@ public ServiceResponse getPostOrDeleteResult(Response respo // Check provisioning state while (!AzureAsyncOperation.getTerminalStatuses().contains(pollingState.getStatus())) { Thread.sleep(pollingState.getDelayInMilliseconds()); + System.err.println(pollingState.getStatus()); if (pollingState.getAzureAsyncOperationHeaderLink() != null && !pollingState.getAzureAsyncOperationHeaderLink().isEmpty()) { @@ -358,6 +363,7 @@ public AsyncPollingTask getPostOrDeleteResultAsync(Response try { if (responseBody != null) { exception.setBody((CloudError) mapperAdapter.deserialize(responseBody.string(), CloudError.class)); + responseBody.close(); } } catch (Exception e) { /* ignore serialization errors on top of service errors */ } callback.failure(exception); @@ -581,6 +587,7 @@ private void updateStateFromAzureAsyncOperationHeader(PollingState pollin AzureAsyncOperation body = null; if (response.body() != null) { body = mapperAdapter.deserialize(response.body().string(), AzureAsyncOperation.class); + response.body().close(); } if (body == null || body.getStatus() == null) { @@ -588,6 +595,7 @@ private void updateStateFromAzureAsyncOperationHeader(PollingState pollin exception.setResponse(response); if (response.errorBody() != null) { exception.setBody((CloudError) mapperAdapter.deserialize(response.errorBody().string(), CloudError.class)); + response.errorBody().close(); } throw exception; } @@ -619,12 +627,14 @@ public void success(ServiceResponse result) { AzureAsyncOperation body = null; if (result.getBody() != null) { body = mapperAdapter.deserialize(result.getBody().string(), AzureAsyncOperation.class); + result.getBody().close(); } if (body == null || body.getStatus() == null) { CloudException exception = new CloudException("no body"); exception.setResponse(result.getResponse()); if (result.getResponse().errorBody() != null) { exception.setBody((CloudError) mapperAdapter.deserialize(result.getResponse().errorBody().string(), CloudError.class)); + result.getResponse().errorBody().close(); } failure(exception); } else { @@ -664,8 +674,10 @@ private Response poll(String url) throws CloudException, IOExcepti exception.setResponse(response); if (response.body() != null) { exception.setBody((CloudError) mapperAdapter.deserialize(response.body().string(), CloudError.class)); + response.body().close(); } else if (response.errorBody() != null) { exception.setBody((CloudError) mapperAdapter.deserialize(response.errorBody().string(), CloudError.class)); + response.errorBody().close(); } throw exception; } @@ -704,8 +716,10 @@ public void onResponse(Call call, Response response) exception.setResponse(response); if (response.body() != null) { exception.setBody((CloudError) mapperAdapter.deserialize(response.body().string(), CloudError.class)); + response.body().close(); } else if (response.errorBody() != null) { exception.setBody((CloudError) mapperAdapter.deserialize(response.errorBody().string(), CloudError.class)); + response.errorBody().close(); } callback.failure(exception); return; diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/PollingState.java b/azure-client-runtime/src/main/java/com/microsoft/azure/PollingState.java index aadbdb1c8d1c..50255ba60089 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/PollingState.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/PollingState.java @@ -60,6 +60,7 @@ public PollingState(Response response, Integer retryTimeout, Type PollingResource resource = null; if (response.body() != null) { responseContent = response.body().string(); + response.body().close(); } if (responseContent != null && !responseContent.isEmpty()) { this.resource = mapperAdapter.deserialize(responseContent, resourceType); @@ -95,6 +96,7 @@ public void updateFromResponseOnPutPatch(Response response) throws String responseContent = null; if (response.body() != null) { responseContent = response.body().string(); + response.body().close(); } if (responseContent == null || responseContent.isEmpty()) { @@ -130,6 +132,7 @@ public void updateFromResponseOnDeletePost(Response response) thro String responseContent = null; if (response.body() != null) { responseContent = response.body().string(); + response.body().close(); } this.setResource(mapperAdapter.deserialize(responseContent, resourceType)); setStatus(AzureAsyncOperation.SUCCESS_STATUS); diff --git a/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java b/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java index a7b428382aa6..6a9e302c8391 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java +++ b/client-runtime/src/main/java/com/microsoft/rest/ServiceResponseBuilder.java @@ -181,6 +181,7 @@ public ServiceResponse buildEmpty(Response response) throws E, IOExcept try { E exception = (E) exceptionType.getConstructor(String.class).newInstance("Invalid status code " + statusCode); exceptionType.getMethod("setResponse", response.getClass()).invoke(exception, response); + response.errorBody().close(); throw exception; } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new IOException("Invalid status code " + statusCode + ", but an instance of " + exceptionType.getCanonicalName() @@ -213,6 +214,7 @@ public ServiceResponseWithHeaders buildWithHeaders(Respons THeader headers = mapperAdapter.deserialize( mapperAdapter.serialize(response.headers()), headerType); + response.errorBody().close(); return new ServiceResponseWithHeaders<>(bodyResponse.getBody(), headers, bodyResponse.getResponse()); } @@ -238,6 +240,7 @@ public ServiceResponseWithHeaders buildEmptyWithHeaders(Re THeader headers = mapperAdapter.deserialize( mapperAdapter.serialize(response.headers()), headerType); + response.errorBody().close(); return new ServiceResponseWithHeaders<>(headers, bodyResponse.getHeadResponse()); } @@ -277,6 +280,7 @@ else if (type == InputStream.class) { // Deserialize else { String responseContent = responseBody.string(); + responseBody.close(); if (responseContent.length() <= 0) { return null; } diff --git a/client-runtime/src/main/java/com/microsoft/rest/retry/RetryHandler.java b/client-runtime/src/main/java/com/microsoft/rest/retry/RetryHandler.java index e78704248c93..e56c061de6a0 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/retry/RetryHandler.java +++ b/client-runtime/src/main/java/com/microsoft/rest/retry/RetryHandler.java @@ -67,6 +67,7 @@ public Response intercept(Chain chain) throws IOException { // try the request Response response = chain.proceed(request); + response.body().close(); int tryCount = 0; while (retryStrategy.shouldRetry(tryCount, response)) {