-
Notifications
You must be signed in to change notification settings - Fork 41.5k
ApplicationAvailabilityBean is not thread-safe #30489
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
Conversation
In which case would this method be invoked concurrently? The JavaDoc from EventListener states that concurrency is used if the method is annotated with |
spring boot allow developer inject |
It's not expected that users inject the bean and call |
I think we should accept this. While |
Ah, yeah, you're right. I missed that case. Will merge the PR. |
@mhalbritter I trace the code from AvailabilityChangeEvent, and find there is nothing to ensure that listeners are not called concurrently —— I can't find any lock or things like // 1. multiple thread invoke this static method concurrently and publish different state.
AvailabilityChangeEvent.publish(this.eventPublisher, ex, LivenessState.BROKEN);
// 2. call multicastEvent in ‘AbstractApplicationContext#publishEvent(Object, ResolvableType)‘
// still don't have any lock,
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType)
// 3. multiple thread could invoke 'SimpleApplicationEventMulticaster#multicastEvent' at same time and publish different event concurrently.
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
...
doInvokeListener(listener, event);
}
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
listener.onApplicationEvent(event);
}
...
} |
ApplicationAvailabilityBean#onApplicationEvent
could be invoked concurrently, we should replace HashMap(event map) with ConcurrentHashMap.Details in jdk -
HashMap.java
- L89