Skip to content

fix 并行调用多例Bean时 annotationWrapper 导致的线程安全问题 #1354

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@
public class ReferenceAnnotationBeanPostProcessor implements BeanPostProcessor,
ApplicationContextAware, PriorityOrdered {

private static final Logger LOGGER = SofaBootLoggerFactory
.getLogger(ReferenceAnnotationBeanPostProcessor.class);
private static final Logger LOGGER = SofaBootLoggerFactory
.getLogger(ReferenceAnnotationBeanPostProcessor.class);

private final SofaRuntimeContext sofaRuntimeContext;
private final SofaRuntimeContext sofaRuntimeContext;

private final BindingAdapterFactory bindingAdapterFactory;
private final BindingAdapterFactory bindingAdapterFactory;

private final BindingConverterFactory bindingConverterFactory;
private final BindingConverterFactory bindingConverterFactory;

private ApplicationContext applicationContext;
private ApplicationContext applicationContext;

private AnnotationWrapper<SofaReference> annotationWrapper;
private final ThreadLocal<AnnotationWrapper<SofaReference>> annotationWrapper = new ThreadLocal<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Using ThreadLocal for annotationWrapper.
This prevents shared state between threads, addressing concurrency issues. However, be cautious of potential memory leaks in environments reusing threads, as ThreadLocals can retain references if not removed. After usage, consider calling 'annotationWrapper.remove()' or reinitializing it in known lifecycle points to avoid stale data.


/**
* To construct a ReferenceAnnotationBeanPostProcessor via a Spring Bean
Expand Down Expand Up @@ -97,7 +97,10 @@
return;
}

sofaReferenceAnnotation = annotationWrapper.wrap(sofaReferenceAnnotation);
if (annotationWrapper.get() == null) {
annotationWrapper.set(createAnnotationWrapper());

Check warning on line 101 in sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java

View check run for this annotation

Codecov / codecov/patch

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java#L101

Added line #L101 was not covered by tests
}
sofaReferenceAnnotation = annotationWrapper.get().wrap(sofaReferenceAnnotation);

Class<?> interfaceType = sofaReferenceAnnotation.interfaceType();
if (interfaceType.equals(void.class)) {
Expand Down Expand Up @@ -130,7 +133,10 @@
return;
}

sofaReferenceAnnotation = annotationWrapper.wrap(sofaReferenceAnnotation);
if (annotationWrapper.get() == null) {
annotationWrapper.set(createAnnotationWrapper());

Check warning on line 137 in sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java

View check run for this annotation

Codecov / codecov/patch

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ReferenceAnnotationBeanPostProcessor.java#L137

Added line #L137 was not covered by tests
}
sofaReferenceAnnotation = annotationWrapper.get().wrap(sofaReferenceAnnotation);

Class<?> interfaceType = sofaReferenceAnnotation.interfaceType();
if (interfaceType.equals(void.class)) {
Expand Down Expand Up @@ -182,8 +188,12 @@
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
this.annotationWrapper = AnnotationWrapper.create(SofaReference.class)
.withEnvironment(applicationContext.getEnvironment())
this.annotationWrapper.set(createAnnotationWrapper());
}

private AnnotationWrapper<SofaReference> createAnnotationWrapper() {
return AnnotationWrapper.create(SofaReference.class)
.withEnvironment(this.applicationContext.getEnvironment())
.withBinder(DefaultPlaceHolderBinder.INSTANCE);
}
}
Loading