Skip to content

Closing an ApplicationContext leads to Exception at ExecutorServiceAdapter #29892

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

Closed
Spunc opened this issue Jan 27, 2023 · 3 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@Spunc
Copy link

Spunc commented Jan 27, 2023

Affects: 6.0.4

Problem

When an ApplicationContext with a org.springframework.core.task.support.ExecutorServiceAdapter bean present is closed, the shutdown() method of ExecutorServiceAdapter gets called which throws an IllegalStateException leading to a warning:

Jan. 27, 2023 1:41:44 PM org.springframework.beans.factory.support.DisposableBeanAdapter invokeCustomDestroyMethod
WARNUNG: Custom destroy method 'shutdown' on bean with name 'executorService' threw an exception: java.lang.IllegalStateException: Manual shutdown not supported - ExecutorServiceAdapter is dependent on an external lifecycle

Steps to reproduce

  1. Configure an ApplicationContext with a ExecutorServiceAdapter bean
@Configuration
public class Config {

    @Bean
    public TaskExecutor taskExecutor() {
        return new ThreadPoolTaskExecutor();
    }

    @Bean
    public ExecutorService executorService(TaskExecutor taskExecutor) {
        return new ExecutorServiceAdapter(taskExecutor);
    }
}
  1. Start and stop the ApplicationContext:
  public class Main {

      public static void main(String[] args) {
          ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
          applicationContext.getBean("executorService", ExecutorService.class);
          applicationContext.close();
      }
  }

Or have a look at a sample project that reproduces the error.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 27, 2023
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Jan 27, 2023
@jhoeller jhoeller added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 30, 2023
@jhoeller jhoeller added this to the 6.0.5 milestone Jan 30, 2023
@jhoeller jhoeller self-assigned this Jan 30, 2023
@jhoeller
Copy link
Contributor

I suppose this is caused by the introduction of a default AutoCloseable.close() implementation in the ExecutorService interface in JDK 19? Are you experiencing this on JDK 19 only? In this case, we can probably override close() in a custom fashion to avoid that effect.

@sbrannen
Copy link
Member

sbrannen commented Jan 30, 2023

This actually happens on any JDK.

With the ExecutorServiceAdapter configured via a @Bean factory method, the destroyMethod attribute defaults to (inferred), and DisposableBeanAdapter#inferDestroyMethodsIfNecessary() registers the shutdown method as the destroy-method.

@jhoeller
Copy link
Contributor

Ah yes, I should have noticed the "shutdown" name in the exception message. In any case, there is a difference in the bean lifecycle for such beans on JDK 19+ now since we'll prefer the new close method over shutdown, waiting for tasks to terminate according to the default close implementation. Makes no difference for this adapter case but generally worth keeping in mind.

@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x labels Jan 31, 2023
jhoeller added a commit that referenced this issue Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants