-
Notifications
You must be signed in to change notification settings - Fork 144
Fix to prevent override a implicit AsyncTaskExecutor (#313) #314
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
Open
single-wolf
wants to merge
5
commits into
opentracing-contrib:master
Choose a base branch
from
single-wolf:fix_async_override_implicit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4eb9a64
Fix to prevent override a implicit AsyncTaskExecutor (#313)
single-wolf f8862fc
Add a test case (#313)
single-wolf 71ad8dd
format test case (#313)
single-wolf 5379660
Merge branch 'master' into fix_async_override_implicit
lucacome f9da0e9
Merge branch 'master' into fix_async_override_implicit
lucacome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...ud-core/src/test/java/io/opentracing/contrib/spring/cloud/async/AsyncImplicitOneTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** | ||
| * Copyright 2017-2021 The OpenTracing Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| * or implied. See the License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package io.opentracing.contrib.spring.cloud.async; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
|
|
||
| import io.opentracing.contrib.spring.cloud.MockTracingConfiguration; | ||
|
|
||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
| import org.springframework.scheduling.annotation.AsyncResult; | ||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
|
||
| /** | ||
| * Prevent overriding a implicit AsyncTaskExecutor {@linkplain DefaultAsyncAutoConfiguration} | ||
| * | ||
| * @author Jerry Zhong | ||
| */ | ||
| @SpringBootTest(classes = {AsyncImplicitOneTest.Configuration.class, MockTracingConfiguration.class, DefaultAsyncAutoConfiguration.class}) | ||
| @RunWith(SpringJUnit4ClassRunner.class) | ||
| public class AsyncImplicitOneTest { | ||
|
|
||
| public static final String IMPLICIT_THREAD_GROUP = "implicit-thread-group"; | ||
|
|
||
| @Autowired(required = false) | ||
| private AsyncConfigurer asyncConfigurer; | ||
| @Autowired | ||
| private AsyncService asyncService; | ||
|
|
||
| @org.springframework.context.annotation.Configuration | ||
| static class Configuration { | ||
|
|
||
| @Bean | ||
| public Executor threadPoolTaskExecutor() { | ||
| ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
| executor.setThreadGroupName(IMPLICIT_THREAD_GROUP); | ||
| executor.initialize(); | ||
| return executor; | ||
| } | ||
|
|
||
| @Bean | ||
| public AsyncService asyncService() { | ||
| return new AsyncService(); | ||
| } | ||
| } | ||
|
|
||
| static class AsyncService { | ||
|
|
||
| @Async | ||
| public Future<String> asyncThreadGroupName() { | ||
| return new AsyncResult<>(Thread.currentThread().getThreadGroup().getName()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoOverrideImplicitOne() throws ExecutionException, InterruptedException { | ||
| assertNull(asyncConfigurer); | ||
| Future<String> asyncFuture = asyncService.asyncThreadGroupName(); | ||
| assertEquals(IMPLICIT_THREAD_GROUP, asyncFuture.get()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: opentracing-contrib/java-spring-cloud
Length of output: 24865
🏁 Script executed:
Repository: opentracing-contrib/java-spring-cloud
Length of output: 48095
🌐 Web query:
Spring Framework EnableAsync default executor selection unique TaskExecutor bean Executor named taskExecutor source💡 Result:
When using
@EnableAsync, the Spring Framework resolves the TaskExecutor to be used for asynchronous method execution through a specific search order [1][2]. The selection logic, implemented in AsyncExecutionAspectSupport, follows this sequence [3]: 1. Unique TaskExecutor Bean: Spring first searches for a unique bean of type TaskExecutor in the application context [3][1]. 2. Bean Named "taskExecutor": If a unique TaskExecutor bean is not found, it then looks for an Executor bean specifically named "taskExecutor" [3][1]. 3. Local Default: If neither of the above is resolvable, Spring falls back to using a SimpleAsyncTaskExecutor as the default [1][2]. It is important to note that if multiple beans of type TaskExecutor are present and none are named "taskExecutor", or if there is no@Primarybean configured, this will trigger a NoUniqueBeanDefinitionException or result in the lookup failing back to the "taskExecutor" search (which may also fail) [3]. In the context of Spring Boot, these behaviors are often influenced by auto-configuration [4][5]. Spring Boot may register its own beans (such as applicationTaskExecutor) which can override or interact with this default lookup mechanism [4][5]. To explicitly control which executor is used, you can implement the AsyncConfigurer interface or mark a specific bean as@Primary[4][5].Citations:
🌐 Web query:
Spring Boot ConditionalOnMissingBean multiple types semantics all requirements must be met💡 Result:
The semantics of
@ConditionalOnMissingBeanregarding multiple types are based on a "logical AND" requirement for the absence of beans [1][2]. When you specify multiple types, the condition only matches if none of the specified requirements are met in the BeanFactory [1][2]. In other words, for the condition to be true, every single specified type must be missing from the context [3]. Key semantic details include: 1. Requirements are independent: The requirements do not need to be met by the same bean; the condition simply verifies that no bean matching any of the specified types exists [1][2]. 2. Collective Absence: If even one bean exists in the BeanFactory that matches any of the specified types, the@ConditionalOnMissingBeancondition will fail (i.e., it will not match, and the associated bean will not be created) [3]. 3. Multiple Attributes: When multiple attributes are used within the same annotation (such as type, name, and annotation), they are also evaluated together. The bean will only be created if the conditions for all specified requirements are met—meaning the "missing" criteria for each attribute must be satisfied [1][2]. 4. Generics Note: For recent versions of Spring Boot (3.5+), note that generic type parameters are considered by default [4]. If you need to match without regard to generic type parameters, you may need to use raw types or specific workarounds like the wildcard (?) type [4]. In summary,@ConditionalOnMissingBeanacts as a collective guard: it requires the total absence of all listed beans or types to trigger the creation of the bean it protects [1][3]. If any conflicting bean is found, the condition is not met [3].Citations:
🏁 Script executed:
Repository: opentracing-contrib/java-spring-cloud
Length of output: 710
🌐 Web query:
site:github.com/spring-projects/spring-framework v5.2.0.RELEASE AsyncExecutionAspectSupport getDefaultExecutor💡 Result:
In the Spring Framework, including version 5.2.0.RELEASE, the getDefaultExecutor method is a protected method defined in the AsyncExecutionAspectSupport class [1][2]. It is responsible for resolving the default Executor to be used for asynchronous task execution [1][3]. The resolution logic implemented in AsyncExecutionAspectSupport.getDefaultExecutor follows these steps [1][2]: 1. It attempts to find a unique bean of type TaskExecutor in the provided BeanFactory [1][2]. 2. If no unique TaskExecutor bean is found, it attempts to find an Executor bean explicitly named "taskExecutor" [1][2]. 3. If neither of these can be resolved, the method returns null [1][2]. In practice, this method is typically overridden by subclasses such as AsyncExecutionInterceptor [3][4]. The override in AsyncExecutionInterceptor invokes the superclass implementation and, if the result is null, falls back to creating a new SimpleAsyncTaskExecutor instance to ensure an executor is available for local use [3][5][4].
Citations:
🌐 Web query:
site:github.com/spring-projects/spring-boot v2.2.0.RELEASE OnBeanCondition ConditionalOnMissingBean💡 Result:
In Spring Boot 2.2.0.RELEASE,
@ConditionalOnMissingBeanis a conditional annotation that ensures a bean is only created if no existing beans meeting the specified requirements are already present in the BeanFactory [1]. It is implemented using the OnBeanCondition class [1]. Key behavioral characteristics and usage recommendations include: 1. Recommended Use: It is strongly recommended to use this condition only on auto-configuration classes [2][3][4]. Because the condition evaluates against bean definitions processed by the application context at that specific moment, usage on standard user-defined configuration classes can lead to unpredictable behavior depending on the order of bean definition loading [2][4]. 2. Mechanism: - The condition checks for the presence of beans based on class types, annotation types, or specific bean names [1]. - It only considers beans that are autowire candidates or default candidates; other beans are ignored [1]. - When multiple attributes are provided, the condition behaves as a logical OR in terms of its target evaluation, but the cumulative effect is that it verifies the absence of the specified criteria [5][6]. 3. Limitations and Nuances: - Timing: The condition can only evaluate bean definitions that have been processed by the context so far [2][3]. If a bean is created by another auto-configuration, the configuration using@ConditionalOnMissingBeanmust be configured to run after that auto-configuration [2][3]. - Implicit Type Defaults: When used on a@Beanmethod, the annotation may implicitly use the method's return type as a constraint if not explicitly specified, which can sometimes lead to unexpected matching behavior [5][7]. - Type Information: The condition relies on available type information within the BeanFactory [8]. It avoids triggering early bean initialization where possible, preferring to inspect bean definitions (e.g., factory method return types) before full bean instantiation [8]. Providing explicit type information in bean definitions is recommended for consistent results [8][9].Citations:
annotationcorrectly spring-projects/spring-boot#16907🏁 Script executed:
Repository: opentracing-contrib/java-spring-cloud
Length of output: 13933
🏁 Script executed:
Repository: opentracing-contrib/java-spring-cloud
Length of output: 11729
Restrict the condition to default async candidates
A plain
Executorbean that is not aTaskExecutorand is not namedtaskExecutorpreventsDefaultTracedAsyncConfigurerSupportfrom loading. Spring then creates an uninstrumented localSimpleAsyncTaskExecutorfor@Asyncmethods. Check for the absence ofAsyncConfigurer,TaskExecutor, and anExecutornamedtaskExecutor, and add a regression test.🤖 Prompt for AI Agents