Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Wait.pool() method does not automatically destroy the thread pool #3188

Open
JifeiMei opened this issue Mar 11, 2024 · 0 comments
Open

Comments

@JifeiMei
Copy link

JifeiMei commented Mar 11, 2024

Describe the bug
I created a job.batch and used Wait. pool() to monitor the running status of the pod created by my job. batch, when pod.status.phase is "Succeeded", save full logs and exit. But my main function does not exit, I try use jconsole to monitor my jvm, I found that one thread pool was not destroyed. I use a NamedThreadFactory to init ScheduledExecutorService, it is indeed this thread pool.

Client Version
19.0.0

Kubernetes Version
1.27.0

Java Version
Java 17

To Reproduce

Wait.poll(
        Duration.ofSeconds(5),
        Duration.ofSeconds(5),
        Duration.ofMinutes(5),
        () -> {
            try {
                V1Pod = findPod(namespace, jobName);
                String phase = pod.getStatus().getPhase();
                if("Succeeded".equals(phase)) {
                    // save the container log
                    return true;
                }
            } catch () {
                // save Exception log
                return false;
            }
        }
);

Expected behavior

I use executorService.shutdown(); to destory the thread pool, the problem has been resolved. This is my code, I added a finally code block.

public static boolean poll(
            Duration initialDelay, Duration interval, Duration timeout, Supplier<Boolean> condition) {
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("k8sWailPool"));
        AtomicBoolean result = new AtomicBoolean(false);
        long dueDate = System.currentTimeMillis() + timeout.toMillis();
        ScheduledFuture<?> future =
                executorService.scheduleAtFixedRate(
                        () -> {
                            try {
                                result.set(condition.get());
                            } catch (Exception e) {
                                result.set(false);
                            }
                        },
                        initialDelay.toMillis(),
                        interval.toMillis(),
                        TimeUnit.MILLISECONDS);
        try {
            while (System.currentTimeMillis() < dueDate) {
                if (result.get()) {
                    future.cancel(true);
                    return true;
                }
            }
        } catch (Exception e) {
            return result.get();
        } finally {
            executorService.shutdown();
        }
        future.cancel(true);
        return result.get();
    }

Server (please complete the following information):

  • OS: Windows
  • Environment: My develop environment, pc
  • Cloud: Not

Additional context

This is my jvm thread dumps, if don't use executorService.shutdown();

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant