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

@Async annotations are not working with AdviceMode.ASPECTJ and lazy-initialization=true #39416

Closed
kicktipp opened this issue Feb 6, 2024 · 2 comments
Labels
status: superseded An issue that has been superseded by another type: bug A general bug

Comments

@kicktipp
Copy link

kicktipp commented Feb 6, 2024

This project reproduces the problem:

https://github.com/kicktipp/lazyasync

The included test does time out as @Async is not working correctly with AdviceMode.ASPECTJ and lazy-initialization=true .

@Configuration
@EnableAsync(mode = AdviceMode.ASPECTJ)
public class AsyncConfig {
}

This works fine usually. To speed up our development server we added this recently

spring.main.lazy-initialization=true

With lazy-initialization set to true, the ThreadPoolTaskExecutor is not configured, therefore all @Async methods are called synchronously which can lead to subtle bugs with @Transactional annotations which are difficult to understand in development mode.

  • Test runs fine with @EnableAsync(mode = AdviceMode.PROXY)
  • Test still fails if I add @Lazy(false) to AsyncConfig

I don't know if you call it a bug. But at least its a pitfall for developers.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 6, 2024
@wilkinsona wilkinsona self-assigned this Feb 8, 2024
@wilkinsona
Copy link
Member

Thanks very much for the sample. It made reproducing the problem straightforward. The problem is that the aspect that honours @Async is lazy and isn't applied. You can work around this by excluding it from lazy initialization:

@Bean
static LazyInitializationExcludeFilter eagerAnnotationAsyncWithAspectJ() {
    return LazyInitializationExcludeFilter.forBeanTypes(AnnotationAsyncExecutionAspect.class);
}

We don't have any auto-configuration for @EnableAsync so there's no immediately obvious place for us to put this exclude filter in Boot. Hopefully the above work around will help for now. We'll discuss this as a team to see if we can agree on somewhere to configure it in Boot itself.

@wilkinsona wilkinsona removed their assignment Feb 27, 2024
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 27, 2024
@wilkinsona wilkinsona added this to the 3.1.x milestone Feb 27, 2024
@wilkinsona wilkinsona added the for: team-meeting An issue we'd like to discuss as a team to make progress label Feb 27, 2024
@wilkinsona
Copy link
Member

We're going to make a change in 3.3 so that all infrastructure beans are excluded from lazy initialization by default (#39831). In the meantime, the filter-based workaround above should be used. Thanks for bringing this to our attention.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2024
@wilkinsona wilkinsona removed this from the 3.1.x milestone Mar 4, 2024
@wilkinsona wilkinsona added status: superseded An issue that has been superseded by another and removed for: team-meeting An issue we'd like to discuss as a team to make progress labels Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants