From 6a3ccacc879c14da8938613ca327b79d89a57c32 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 --- .../client/okhttp/OkHttpClientImpl.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) 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();