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

Gracefull shutdown don't throw interrupted exception after timeout #34476

Open
deblockt opened this issue Feb 24, 2025 · 0 comments
Open

Gracefull shutdown don't throw interrupted exception after timeout #34476

deblockt opened this issue Feb 24, 2025 · 0 comments
Labels
status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@deblockt
Copy link
Contributor

Using graceful shutdown with async task ran with

CompletableFuture
                .runAsync(() -> {
                    
                }, executor)

Using @EnableScheduling and the executor applicationTaskExecutor the graceful shutdown don't take in account the currently running tasks, when the server shutdown the task is silently aborted (no InteruptedException).

Using a custom Executor:

ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(2);
        taskExecutor.setMaxPoolSize(2);
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(1);
        taskExecutor.setVirtualThreads(true);
        taskExecutor.initialize();

The graceful shutdown wait 1 second before the end of the task, and the log [WARN] - Forcing shutdownNow() - Tasks did not stop in time. is displayed. But no InterruptedException is thrown in the Runnable.

To have an InterruptedException I should create a scheduler like that

 ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(2);
        taskExecutor.setMaxPoolSize(2);
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(1);
        taskExecutor.setVirtualThreads(true);
        taskExecutor.initialize();

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                log.info("Waiting for executor to terminate...");
                ThreadPoolExecutor executor = taskExecutor.getThreadPoolExecutor();
                if (executor != null) {
                    if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
                        log.warn("Forcing shutdownNow() - Tasks did not stop in time.");
                        executor.shutdownNow();
                    }
                }
            } catch (InterruptedException e) {
                log.error("Shutdown hook interrupted!", e);
                Thread.currentThread().interrupt();
            }
        }));

Doing that the graceful shutdown wait 1 second and stop the task with an InterruptedException.

The goal of the InterruptedException is to be able to log the stopped task, to know that something should be retry.

Why the default ThreadPoolTaskExecutor don't offer a way to have an InterruptedException. Is it possible to add this feature? To avoid to write it manually ?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

2 participants