From 53c78a5675d3b1254ffeaed5a717bccd8a384dc1 Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Fri, 14 Oct 2022 07:20:35 -0400 Subject: [PATCH] fix #4491: adding a more explicit shutdown exception --- CHANGELOG.md | 1 + .../client/okhttp/OkHttpClientImpl.java | 32 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0a31e67b39..9afa3e03476 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ #### Improvements * Fix #4348: Introduce specific annotations for the generators * Refactor #4441: refactoring `TokenRefreshInterceptor` +* Fix #4491: added a more explicit shutdown exception for okhttp * Fix #4365: The Watch retry logic will handle more cases, as well as perform an exceptional close for events that are not properly handled. Informers can directly provide those exceptional outcomes via the SharedIndexInformer.stopped CompletableFuture. * Fix #4396: Provide more error context when @Group/@Version annotations are missing * Fix #4384: The Java generator now supports the generation of specific annotations (min, max, pattern, etc.), as defined by #4348 diff --git a/httpclient-okhttp/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientImpl.java b/httpclient-okhttp/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientImpl.java index 820fdf54c43..8edd42e2283 100644 --- a/httpclient-okhttp/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientImpl.java +++ b/httpclient-okhttp/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientImpl.java @@ -16,6 +16,7 @@ package io.fabric8.kubernetes.client.okhttp; +import io.fabric8.kubernetes.client.KubernetesClientException; import io.fabric8.kubernetes.client.http.HttpClient; import io.fabric8.kubernetes.client.http.HttpRequest; import io.fabric8.kubernetes.client.http.HttpResponse; @@ -40,6 +41,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; +import java.util.concurrent.RejectedExecutionException; import java.util.function.Function; public class OkHttpClientImpl implements HttpClient { @@ -238,22 +240,28 @@ private CompletableFuture> sendAsync(HttpRequest request Function handler) { CompletableFuture> future = new CompletableFuture<>(); Call call = httpClient.newCall(((OkHttpRequestImpl) request).getRequest()); - call.enqueue(new Callback() { + try { + call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) throws IOException { - BufferedSource source = response.body().source(); + @Override + public void onResponse(Call call, Response response) throws IOException { + BufferedSource source = response.body().source(); - AsyncBody asyncBody = handler.apply(source); + AsyncBody asyncBody = handler.apply(source); - future.complete(new OkHttpResponseImpl<>(response, asyncBody)); - } + future.complete(new OkHttpResponseImpl<>(response, asyncBody)); + } - @Override - public void onFailure(Call call, IOException e) { - future.completeExceptionally(e); - } - }); + @Override + public void onFailure(Call call, IOException e) { + future.completeExceptionally(e); + } + }); + } catch (RejectedExecutionException e) { + throw new KubernetesClientException("The okhttp client executor has been shutdown. " + + "More than likely this is because the KubernetesClient.close method has been called " + + "- please ensure that is intentional.", e); + } future.whenComplete((r, t) -> { if (future.isCancelled()) { call.cancel();