java icon indicating copy to clipboard operation
java copied to clipboard

io.kubernetes.client.util.wait.Wait Executor never shutdown.

Open LDHistory opened this issue 2 years ago • 4 comments
trafficstars

Describe the bug

Hi. I am developing a service with the below sample.

https://github.com/kubernetes-client/java/blob/master/examples/examples-release-15/src/main/java/io/kubernetes/client/examples/DeployRolloutRestartExample.java

When I run the ScheduledExecutorService via the Wait class, the okhttp Thread never shutdown even when the service ends.

public static void main(String[] args) throws Exception {
	String path = "/Users/Desktop/Downloads/config";
	
	ApiClient client = ClientBuilder.kubeconfig(
			KubeConfig.loadKubeConfig(new FileReader(path)))
			.build();
	
	Configuration.setDefaultApiClient(client);
	waitUntilDeployment();
}

public static void waitUntilDeployment() throws ApiException, InterruptedException {
	AppsV1Api appsV1Api = new AppsV1Api();
	
	Wait.poll(
		Duration.ofSeconds(3),
		Duration.ofSeconds(10),
		() -> {
			try {
				System.out.println("Waiting until example deployment is ready...");
				int count = appsV1Api.readNamespacedDeployment("example-deployment", "example", null, null, null)
						.getStatus()
						.getReadyReplicas();
				
				return count > 0;
			} catch (ApiException e) {
				return false;
			}
		});
	System.out.println("Created example deployment!");
}

image

So, when the shutdown() method is called after the ScheduledExecutorService is finished, the okhttp thread is normally terminated.

public class Wait {
	...

	public static boolean poll(
	      Duration initialDelay, Duration interval, Duration timeout, Supplier<Boolean> condition) {
	    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
	   ...
	    try {
	      while (System.currentTimeMillis() < dueDate) {
	        if (result.get()) {
	          future.cancel(true);
	          return true;
	        }
	      }
	    } catch (Exception e) {
	      return result.get();
            /* Modified Code */
	    } finally {
	    	executorService.shutdown();
	    }
            /* ************* */
	    
	    future.cancel(true);
	    return result.get();
	  }
}

I wonder if it's ok to not call the shutdown() method on the ScheduledExecutorService in the Wait class.

Client Version 15.0.1

Java Version Java 8

LDHistory avatar May 31 '23 06:05 LDHistory